Every algorithm below sees exactly the same requests. Pick a scenario or shape the traffic yourself, and compare what each one lets through. The simulation runs the same TypeScript as the live limiter.
Admitted (bars up)
Rejected (bars down)
Room left
Window edges
The six algorithms
Fixed window
Counts requests in windows aligned to the clock: each second from :00 to :01, and so on. When a window's count reaches the limit, everything else waits for the next window.
State per key
One counter and its window
Weak spot
A full quota at the end of one window plus a full quota at the start of the next: twice the limit within moments.
Sliding window log
Remembers when every admitted request happened and counts the ones in the last window, whatever the clock says. It is exact: no window of that length ever holds more than the limit.
State per key
One timestamp per admitted request in the window
Weak spot
Memory grows with the limit, which hurts at high limits and many keys.
Sliding window counter
Keeps the counts of the current and previous fixed windows, and estimates the sliding count by weighting the previous window by how much of it still overlaps.
State per key
Two counters
Weak spot
An estimate: it assumes last window's requests were spread evenly, so it can be stricter or looser than the true count.
Token bucket
A bucket holds up to burst tokens and refills at the limit's rate. Each request takes a token; with none left, it is rejected. Short bursts pass, the long-run rate holds.
State per key
Token count and last refill time
Weak spot
Over any window it admits up to burst plus the refill, more than the limit alone suggests.
Leaky bucket (meter)
A bucket of size burst leaks at the limit's rate. Each request pours in one unit, and one that would overflow is rejected. It is the token bucket seen from the other side: water level instead of tokens left.
State per key
Water level and last update time
Note
The queueing leaky bucket delays requests instead of rejecting them; this is the metering one.
GCRA
The Generic Cell Rate Algorithm, from ATM networks. It stores when the next request is due if the client sent at exactly the allowed rate, and rejects requests that arrive too far ahead of that schedule.
State per key
One timestamp
Why use it
Token-bucket behaviour with less state and no refill step. The live API runs it.
Token bucket, leaky bucket and GCRA are three descriptions of one policy: for the same limit, period and burst they make identical decisions, which the property tests check on generated traffic. They differ only in what they store.
The real distributed limiter
These requests go to POST /api/check on this site. The Worker hands each key to its own Durable Object, which runs GCRA one request at a time, so parallel requests cannot over-admit. The response headers below are the real ones.
Send a request to see the status and headers.
Same request with curl
Recent responses
Time
Status
RateLimit
Retry-After
How it works
Throttle is a small rate-limiting toolkit: a dependency-free TypeScript library with six algorithms behind one interface, a strongly consistent limiter on Cloudflare Durable Objects, and this playground.
Client
POST /api/check with a key and a policy
Worker
Checks a per-IP budget with the Rate Limiting binding, validates and caps the body, writes the RateLimit headers
Durable Object for the key
One instance, one request at a time. Runs GCRA from the shared library against its SQLite storage; an alarm deletes the state once it expires
Why a Durable Object per key
A rate limiter is a read, a decision and a write. If two requests read the same count at once, both can be admitted. Cloudflare runs at most one instance of each Durable Object, worldwide, and that instance handles one event at a time. The limiter reads its state through the synchronous SQLite API, decides, and writes back without awaiting anything in between, so no other request can run in the middle. Requests for the key from every Worker, in every location, reach that one instance.
Where else the state could live
Store
Consistency
Cost of a check
Main risk
Durable Object per key
Strong: one instance, serial execution
One round trip to the object's location
A per-object throughput ceiling for very hot keys
Workers KV
Eventual: writes take time to reach other locations
Fast cached reads, slower writes
Over-admits: concurrent readers see the same count, and replicas lag
In-isolate memory
Per isolate only
No I/O at all
Each isolate and location keeps its own count, so the real limit multiplies
Rate Limiting binding
Per Cloudflare location, eventually consistent
Local and cheap
Approximate by design; periods of 10 or 60 s only
This site uses two of them: the Rate Limiting binding to protect the API from abuse, where approximate is fine, and Durable Objects for the limiter itself, where it is not.
Headers
Responses follow draft-ietf-httpapi-ratelimit-headers-12: RateLimit-Policy gives the quota and window, RateLimit gives the available units (a), the seconds within which no more than that can be used (w) and the request's cost (c). Rejections add Retry-After.
The README has the design decisions, the test strategy and measured numbers.