Skip to main content
Concepts/Security & Distributed Concurrency/rate-limiting

Rate Limiting & Token Buckets

Protect downstream APIs and services from Denial-of-Service spikes using Token Bucket and Sliding Window Log algorithms.

XLinkedInReddit
intermediate

๐Ÿ’ก Why It Matters

Uncontrolled API traffic can crash databases, exhaust connection pools, and burn cloud infrastructure budgets.

๐ŸŒ Real-World Production Usages & Examples

GitHub API Tier Limiting

Scenario: Enforcing 5,000 requests/hour for authenticated users.

Outcome: Returns HTTP 429 Too Many Requests.

โœ“ When to Use

  • โ€ขPublic API endpoints exposed to third-party developers.
  • โ€ขAuthentication / login routes.
  • โ€ขCostly AI LLM inference endpoints.

โœ• When NOT to Use

  • โ€ขInternal microservice IPC where mesh circuit breaking is preferred.

๐Ÿ”‘ Core Architecture & Rules

  • 1Token Bucket fills tokens at a constant rate; permits short bursts.
  • 2Sliding Window Counter combines window counters with weighted overlap percentages.

โš–๏ธ Architectural Trade-Off Analysis

Algorithm Selection

PROToken bucket permits bursts while keeping average throughput predictable.
CONSliding window log requires more memory.

โš ๏ธ Failure Modes & Mitigations

Race Condition in Counter

Non-atomic counter updates across gateway nodes.

Mitigation: Execute Redis Lua scripts for atomic counter updates.

๐Ÿ› ๏ธ Example Usages

Reviewed practical implementations and concrete code walkthroughs for Rate Limiting & Token Buckets.

1 Reviewed Example
pattern applicationexecutable tested

Atomic Redis Lua Token Bucket Rate Limiter

Applies the Token Bucket design pattern atomically across distributed gateway instances using Redis Lua scripts.

โฌกExplore Related Concepts & Guides

Curated technical reading and direct concept relationships.

โšก Next Step ยท Apply Concept

Ready to apply Rate Limiting & Token Buckets?

Test your understanding with a short scenario about real system constraints and trade-offs.

Share this concept

Share with fellow engineers or bookmark for system design discussions.

Share:XLinkedInReddit