How do you enforce token buckets in a distributed system?
Use Redis or Memcached to store per-client token state (tokens_available, last_refill_time). Each API gateway queries and updates this state atomically. Return `429 Too Many Requests` with a `Retry-After` header when the bucket is empty. Distribute tokens across gateways by keeping the refill rate low enough that skew is acceptable.
Answered in
Token Buckets: Bounding Rate Limits at the EdgeFixed-window counters leak at boundaries. Token buckets refill steadily, absorb bursts, and bound the sustained rate strictly—the algorithm Stripe uses.
Read the full analysisOther questions this article answers
More system design questions
- Why doesn't Google just run Dijkstra faster?
- What is a shortcut edge and when is it precomputed?
- How much space do shortcut edges take compared to the original graph?
- Can Contraction Hierarchies handle dynamic graphs like traffic or road closure?
- Why contract low-degree nodes first instead of high-degree ones?
- What is a CRDT and why does it matter for real-time collaboration?
- How do CRDTs handle concurrent edits without a central server referee?
- Why did Figma move from operational transforms to CRDTs?
Every answer on Crashtech is written by the editor of the article it comes from — never auto-summarised. Browse all answers or the System Design beat.