Lock Free Single Producer Single Consumer Ring Buffer
Last updated: November 29, 2026
Quick Overview
Design a bounded ring buffer that passes messages from exactly one producer thread to exactly one consumer thread without locks. Implement it, then explain what memory ordering and cache layout a C++ version needs to be correct and fast.
DRW
November 29, 20260
0
0 solved
Design a bounded ring buffer that passes messages from exactly one producer thread to exactly one consumer thread without locks. Implement it, then explain what memory ordering and cache layout a C++ version needs to be correct and fast.
SPSC queues connect market data, strategy, and order entry threads in DRW's low latency stack, and interviewers use them to test real understanding of concurrency beyond mutexes. They expect a correct design in any language plus a precise discussion of acquire and release semantics and false sharing.
What the Interviewer Expects
- Explains why single writer per index makes locks unnecessary
- Uses monotonic head and tail counters with a power of two mask
- Orders the payload write before publishing the tail
- Describes acquire loads and release stores in C++
- Pads head and tail to separate cache lines and caches the opposite index
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
- What breaks if there are two producers?
- How would you make the consumer batch reads to cut cache traffic?
- Why is a power of two capacity useful?
- How do you handle a slow consumer: block, drop, or overwrite?
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
Exactly one producer calls try_push, exactly one consumer calls try_pop. Non blocking: return failure when full or empty. FIFO order preserved, no lost or duplicated items....
High-Level Design
A fixed array plus two counters. tail is written only by the producer, head only by the consumer. Each side reads the other's counter to check full or empty. With a single writer per variable ther...