Implement a Chat Rate Limiter With Slow Mode and Mod Exemptions
Last updated: November 29, 2026
Quick Overview
Implement `allow(user, now, is_mod)` for a channel's chat that enforces a per-user token bucket (burst capacity plus refill rate) and an optional slow mode requiring a minimum gap between a user's accepted messages, with moderators and the broadcaster exempt.
Twitch
November 29, 20260
0
0 solved
Implement `allow(user, now, is_mod)` for a channel's chat that enforces a per-user token bucket (burst capacity plus refill rate) and an optional slow mode requiring a minimum gap between a user's accepted messages, with moderators and the broadcaster exempt.
Twitch chat lets broadcasters turn on slow mode, and the platform also enforces its own per-user send limits, so the two rules have to compose cleanly. The interviewer checks that you can implement a token bucket lazily, combine two policies without surprising interactions, and reason about where this state lives in a distributed chat service.
What the Interviewer Expects
- Refills tokens lazily from elapsed time instead of using a timer
- Caps tokens at capacity
- Applies slow mode against the last accepted message, not the last attempt
- Exempts mods before touching state
- Explains that rejected messages should not consume tokens
- Discusses sharding state by channel and user
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
- Chat edges are many servers. Where does the bucket state live and how stale can it be?
- How would you return a retry-after value to the client?
- How would you add a stricter limit for accounts younger than one day?
- How do you evict state for users who stop chatting?
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
Per channel config: capacity tokens, refill_per_sec, and slow_mode seconds (0 means off). A message is accepted if the user is a mod, or if slow mode allows it and at least one token is availabl...
High-Level Design
Store per user [tokens, last_refill, last_sent]. On each call, compute refilled tokens as min(capacity, tokens + elapsed * rate) so no background timer is needed. Check slow mode first (cheap and ...