Implement a Per-User Token Bucket for Lightning Deal Requests
Last updated: November 29, 2026
Quick Overview
Implement a thread-safe rate limiter that allows each user a burst of up to C requests and refills at R requests per second, so lightning deal endpoints can reject scripted clients without blocking normal shoppers.
Temu
November 29, 20260
0
0 solved
Implement a thread-safe rate limiter that allows each user a burst of up to C requests and refills at R requests per second, so lightning deal endpoints can reject scripted clients without blocking normal shoppers.
Temu's lightning deals attract bots that hammer the claim endpoint when a deal opens. This fundamentals question checks that you can implement token bucket refill math correctly, keep it thread safe, and reason about where the limiter lives in a real deployment.
What the Interviewer Expects
- Explain token bucket versus fixed window and why bursts matter
- Refill lazily from elapsed time instead of using a background timer
- Cap tokens at capacity
- Guard shared state with a lock
- Discuss moving state to Redis for a fleet of servers
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 do you implement this atomically in Redis across many API servers?
- How do you stop the per-user map growing without bound?
- Would you limit by user, device, IP or payment instrument for flash sales?
- What should the client see when it is rate limited?
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
allow(user, now, cost=1) returns True if the request may proceed. Each user starts full at capacity tokens, earns rate tokens per second up to the cap, and each request spends cost. Time is pa...
High-Level Design
Store (tokens, last_ts) per user. On each call, add (now - last_ts) * rate, cap it, and spend if enough remain. No timers are needed: refill is computed lazily. A fixed-window counter would let a ...