Implement a Thread-Safe Token Bucket for a Streaming API Gateway
Last updated: November 29, 2026
Quick Overview
Implement a token bucket rate limiter that many threads can call concurrently, plus a registry that lazily creates one bucket per account, so a Disney+ API gateway never admits more requests than the configured rate.
Disney
November 29, 20260
0
0 solved
Implement a token bucket rate limiter that many threads can call concurrently, plus a registry that lazily creates one bucket per account, so a Disney+ API gateway never admits more requests than the configured rate.
A concurrency fundamentals question in the style of Disney Streaming technical screens, where edge services protect backends such as profiles and entitlements from bursts. The interviewer checks the refill math, where the lock must sit, and the classic race when creating per-key state.
What the Interviewer Expects
- Explain token bucket refill as rate times elapsed time, capped at capacity
- Make refill plus spend one atomic critical section
- Avoid creating two buckets for the same key under contention
- Inject the clock so the code is testable
- Discuss single-process versus distributed limits
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you enforce this across 200 gateway instances?
- How do you evict buckets for accounts that went idle?
- Would a lock-free approach help here?
- How should the gateway respond when the limit is hit?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
try_acquire(n) returns True if n tokens are available and spends them, else False. Tokens refill continuously at rate per second up to capacity. Many threads call it at once, and the total adm...
High-Level Design
Store tokens and updated per bucket. On each call, compute elapsed time, add elapsed * rate, cap, then try to spend. The read, refill and spend must happen under one lock, or two threads could b...