Back to Field Notes

The rate limiter that never fired

Our site chat got a limit of 10 requests a minute per address, and on the live site it never refused one. Cloudflare's rate-limiting binding counts per machine, so we moved the count into one Durable Object per client.

Covers
Written
Repositories
infinary.io
Pull requests
2
Status
Shipped
Reading time
4 min
#cloudflare#rate-limiting#durable-objects#site-chat
Contents
requests from one address
68
refused by the first limiter
0
the limit
10 / min
first request the fix refuses
11th

What we built

On October 20 we moved Ask Infinary, the chat on this site, to Claude Haiku 5.5. The same change added abuse limits, because a public chat box that calls a paid model is an obvious target. The main one was a per-address limit of 10 requests a minute, using Cloudflare’s (Cloudflare's built-in Workers API for rate limits. Each machine checks a locally cached counter, synced with the location in the background, so it's approximate by design.).

The binding is a few lines of config and one call, and we read it as “10 a minute per Cloudflare location”:

src/pages/api/chat.ts (before)
return !(await limiter.limit({ key: `chat:${ip}` })).success;

It passed its tests, it returned a 429 on the 11th request when we ran the built Worker locally under (Cloudflare's open-source Workers runtime. Wrangler runs it locally for development, so local tests use the same engine as production.), and it shipped that evening.

What broke

The probes sent empty message lists, which the limiter counts before the chat refuses them, so they cost no model calls.

0102030requests14 quick30 in 4 s24 over 39 s15, 2.5 s apartwent through, 14 quick: 14went through, 30 in 4 s: 30went through, 24 over 39 s: 24went through, 15, 2.5 s apart: 10refused with 429, 14 quick: 0refused with 429, 30 in 4 s: 0refused with 429, 24 over 39 s: 0refused with 429, 15, 2.5 s apart: 5limit: 10 a minutewent throughrefused with 429
0102030requests14 quick30 in 4 s24 over 39 s15, 2.5 s apartwent through, 14 quick: 14went through, 30 in 4 s: 30went through, 24 over 39 s: 24went through, 15, 2.5 s apart: 10refused with 429, 14 quick: 0refused with 429, 30 in 4 s: 0refused with 429, 24 over 39 s: 0refused with 429, 15, 2.5 s apart: 5limit: 10 a minutewent throughrefused with 429
What the two limiters did with the same kind of probe Against the first limiter, every request in all three probes went through: 14, 30 and 24, with none refused. Against the Durable Object, 10 of 15 requests went through and 5 were refused.
Show data
went throughrefused with 429
14 quick140
30 in 4 s300
24 over 39 s240
15, 2.5 s apart105

The first three probes ran against the binding; the last one ran against the fix, described below. We ruled out the boring causes first:

  • The binding was there. The live Worker version listed it.
  • The code called it correctly. The compiled bundle awaited limit() and read success, and no error was logged.
  • The same bundle worked locally. Under wrangler dev it refused the 11th request, every time.

Why

One client’s requests are spread across many machines in a location, so no single machine’s view of the count reached 10 before the minute was up. Locally there is only one machine, which is why every local test passed.

We didn’t instrument production to watch the split happen. So that part is the documented behaviour plus what we measured, not something we observed directly.

What we changed

We replaced the binding with a (A Cloudflare Workers feature: a single, named instance with its own storage. Every request for the same name reaches the same object, wherever it comes from.), one per client. Every request from an address goes to the object named for it, whichever Worker (One running copy of a Worker inside the Workers runtime. A busy site runs many at once, on many machines, and they share no memory.), machine or location handles the request, so the count is exact: a per-client limit with an exact window.

A sequence diagram. The visitor posts to the chat. The Worker, in whichever isolate takes the request, asks the visitor's own Durable Object whether the request is over the limit. The object counts 10 requests in any 60 seconds, in storage. If the request is within the limit, the Worker streams Claude's reply back. If it's the 11th or later, the Worker answers 429 Too many requests.
One object per client: every isolate asks the same counter.
src/pages/api/chat.ts
return !(await limiter.limit({ key: `chat:${ip}` })).success;
return await limiters.get(limiters.idFromName(chatRateLimitKey(ip))).isLimited();

The window is a sliding one. A request at time tt is allowed when fewer than 10 counted requests fall in the minute before it:

allow(t)  ⟺  ∣{ ti∈H:t−60 s<ti≤t }∣<10\text{allow}(t) \iff \bigl|\{\, t_i \in H : t - 60\,\text{s} < t_i \le t \,\}\bigr| < 10

where HH is the stored list of requests the object has let through. A refused request is never added to HH, so hammering the endpoint doesn’t push the reset further away. The object itself is short:

src/lib/chat-rate-limiter.ts
async isLimited(now = Date.now()): Promise<boolean> {
const cutoff = now - CHAT_RATE_WINDOW_MS;
const hits = ((await this.ctx.storage.get<number[]>(HITS_KEY)) ?? []).filter((t) => t > cutoff);
if (hits.length >= CHAT_RATE_LIMIT) return true;
hits.push(now);
await this.ctx.storage.put(HITS_KEY, hits);
await this.ctx.storage.setAlarm(now + CHAT_RATE_WINDOW_MS);
return false;
}

The tests cover a 12-request burst from one address: 10 requests through, then two 429s, all counted in one object, while a second address is unaffected. Locally, the count also survived the object dropping out of memory: after 12 seconds idle, the next request was still refused.

The evening, in order

October 20, Denver time
  1. The chat moves to Claude Haiku 5.5, with the rate-limiting binding. The site deploys within a minute.
  2. The check after the deploy: 68 requests from one address, no 429.
  3. The Durable Object fix goes up for review.
  4. Review finds why the limiter failed on a Windows machine: the path limit (below).
  5. The fix merges and the site deploys.
  6. The live check: 15 requests, 2.5 seconds apart. Ten go through; the 11th gets a 429.
The rate-limiting binding: 68 requests from one address, all 68 went through, none refused. Binding
A Durable Object per client: 15 requests from one address, 10 went through, then 5 were refused with a 429. Durable Object
The two live probes on the same grid, one square per request in the order sent; empty squares are requests the second probe didn't send.

One more surprise, on Windows

wrangler dev, from a deep checkout on Windows
[chat] Rate limiter failed: internal error; reference = …
RuntimePath to the object’s SQLite fileResult
Windows215 characters10 requests through, then 429
Windows250 characters10 requests through, then 429
Windows266 charactersevery call internal error
Linux329 characters10 requests through, then 429

Production storage isn’t a Windows filesystem, so this is local only. The fix is a short --persist-to, such as C:/wst, and a note next to the binding in wrangler.jsonc.

What we’d tell someone else