Google • Meta • Stripe Craft 60-Minute Loop

Designing an Extensible Multi-Tier Rate Limiter

Rate limiters test your ability to structure reusable library code, apply object-oriented design patterns (Strategy, Registry), manage memory efficiency, and perform time-based calculations without spinning background threads.

Step 1: Clarifying Questions to Ask in Minutes 0–5

4 Questions to Establish Leadership Scope

1. Client Identifier:

“Are we throttling based on client IP address, authenticated user ID, or an API token header?”

2. Algorithm Flexibility:

“Should the architecture allow plugging in different algorithms (e.g. Token Bucket vs. Sliding Window) via an interface?”

3. Action on Exceeded Limit:

“Should exceeded requests be immediately rejected with an HTTP 429 status code, or queued for deferred execution?”

4. Multi-Node Distributed Scope:

“Is this an in-memory library for a single edge gateway process, or should we design the storage interface for Redis clustering?”

Step 2: Algorithm Comparison & Senior Trade-Offs
Token Bucket (Recommended)O(1) Space

Stores only 2 numbers per client: `currentTokens` and `lastRefillTimestamp`. Tokens are refilled lazily on each request. Zero background threads needed. Handles sudden traffic bursts gracefully.

Sliding Window LogO(Requests) Space

Stores an array of timestamps for every request in the rolling window. Perfectly accurate, but under high traffic (e.g. 5,000 requests/sec), memory footprint explodes.

Interactive Token Bucket Simulator (Live Refill)
Bucket Level:5.0 / 5 Tokens
Refill Rate: +0.5 token/secContinuous Lazy Math
Click "Send 1 Request" to test limit enforcement
Step 3: Complete Executable Implementation
TYPESCRIPT Implementation • Full Runnable Class & Test Suite

Full 200-line code is collapsed to preserve screen focus. Click expand to inspect token bucket math, concurrency locks, and unit tests.