Implement A Thread-Safe Token Bucket Policer For Interface QoS
Last updated: November 29, 2026
Quick Overview
Implement a single-rate two-color policer configured with a committed information rate in bits per second and a burst size in bytes, where offer(size, now) returns conform or exceed and must be safe to call from multiple threads.
Cisco
November 29, 20260
0
0 solved
Implement a single-rate two-color policer configured with a committed information rate in bits per second and a burst size in bytes, where offer(size, now) returns conform or exceed and must be safe to call from multiple threads.
Policing and shaping are standard QoS features on Cisco routers, Meraki MX traffic shaping and Webex media edges, and the token bucket is the model behind them. The interviewer checks unit handling, lazy refill math, burst capping, concurrency, and the policer versus shaper distinction.
What the Interviewer Expects
- Converts bits per second to bytes per second correctly
- Refills lazily based on elapsed time, capped at the burst size
- Does not consume tokens for exceeding packets
- Guards state with a lock or atomic operations
- Explains how a shaper differs (queue and delay instead of drop)
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 extend this to a two-rate three-color marker with CIR and PIR?
- How do you avoid lock contention when many cores police the same aggregate?
- What happens with a packet larger than the burst size, and how should configuration prevent it?
- How would you choose Bc for TCP traffic so throughput is not crushed?
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
Constructor takes cir_bps, bc_bytes, and the current time. offer(size_bytes, now) returns conform if enough tokens exist (and deducts them), else exceed. Multiple threads may call offer co...
High-Level Design
Tokens are bytes. Rather than a timer that adds tokens, refill lazily on each call: tokens = min(capacity, tokens + elapsed * rate). That is exact and costs O(1). A packet conforms if `size <= token...