Build a Single-Producer Single-Consumer Ring Buffer for Market Data
Last updated: November 29, 2026
Quick Overview
Implement a bounded, lock-free ring buffer that passes decoded market data updates from a feed handler thread to a strategy thread, and explain the memory ordering and cache layout that make it correct and fast in C++.
Imc
November 29, 20260
0
0 solved
Implement a bounded, lock-free ring buffer that passes decoded market data updates from a feed handler thread to a strategy thread, and explain the memory ordering and cache layout that make it correct and fast in C++.
IMC's low-latency trading stack hands messages between pinned threads millions of times per second, and SPSC queues are the standard tool for that hop. The interviewer checks index arithmetic, full versus empty detection, acquire and release semantics, and awareness of false sharing.
What the Interviewer Expects
- Explains why one writer per index removes the need for locks or CAS
- Uses power-of-two capacity and masking
- Distinguishes full from empty with monotonic counters
- Places acquire loads and release stores correctly
- Pads head and tail onto separate cache lines
- Caches the other side's index to cut cross-core traffic
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 a second producer thread is added?
- How would you avoid copying large messages into the buffer?
- What should the consumer do when the queue is empty: spin, yield or block?
- How would you detect and report that the consumer is falling behind?
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
One producer thread (feed handler) and one consumer thread (strategy). Bounded capacity, non-blocking try_push and try_pop, no locks, no heap allocation on the hot path. Must be correct under the ...
High-Level Design
Fixed array of capacity slots, capacity a power of two so index & mask replaces modulo. Two monotonically increasing 64-bit counters: tail written only by the producer, head written only by th...