Launch offer: the first 1,000 users get Settl free for a year*Claim your spot
settlbuilding in public

Token-Bucket Rate Limiting Pack

A fixed window can honestly report that every minute stayed under the limit while allowing twice the intended traffic in one second.

Sixty requests at 12:00:59 and sixty more at 12:01:00 fit into separate minute buckets. Your dashboard looks clean. The backend receives 120 requests across the seam.

The token-bucket model

The bucket has:

For each request, calculate tokens earned since the previous update, cap the result at capacity, then consume the request cost if enough tokens remain. A quiet client accumulates burst room. After the burst, it is held to the refill rate.

In a distributed system, refill and consume must be one atomic operation, commonly a server-side script or transactional command in the shared store. A read followed by a write can over-admit when several instances race.

Choose the identity and failure rule

Key the limiter by the real boundary: tenant, user, API key, or endpoint plus tenant. A proxy IP can group thousands of unrelated users. A user-only key can let one expensive endpoint consume the budget intended for everything.

Return enough information for the caller to behave well: limit, remaining capacity, and a retry time based on the next available token. Use a server-controlled or monotonic clock where possible.

Write down what happens when the limiter store is unavailable. A login or expensive AI endpoint may need to fail closed or use a small local emergency budget. A low-risk read may accept bounded fail-open behaviour. Do not let a timeout make the choice.

Verification checklist

  1. Send traffic on both sides of a fixed-window boundary. The token bucket never grants an accidental double burst.
  2. Empty the bucket and prove requests resume only at the refill rate.
  3. Release concurrent requests from several instances against one key. Atomic accounting holds.
  4. Run two tenant keys and prove neither consumes the other's tokens.
  5. Simulate clock movement and limiter-store failure.
  6. Check response headers and retry timing against the actual next token.
  7. Load-test the maximum documented burst plus sustained traffic.

The starter accepts the policy, identity rules, capacity, refill count, distributed-store model, and request events. It returns the algorithm, atomic operation, header contract, retry timing, burst tests, and observability plan. Dry-run sends no traffic and changes no limiter state.

Download the runnable pack

Comment BUCKET for the pack. You are metering traffic, not counting calendar boxes.

Get the next one in your inbox