Lock-Free Single-Producer Single-Consumer Ring Between Hot-Path Threads
Last updated: November 29, 2026
Quick Overview
Implement a bounded single-producer single-consumer ring buffer for passing decoded market data from a feed thread to a strategy thread, and explain exactly which memory ordering each index access needs in C++.
Virtu Financial
November 29, 20260
0
0 solved
Implement a bounded single-producer single-consumer ring buffer for passing decoded market data from a feed thread to a strategy thread, and explain exactly which memory ordering each index access needs in C++.
Virtu's latency-sensitive C++ systems pass messages between pinned threads without locks, so engineers are expected to understand ring buffers and the memory model. The screen checks index arithmetic, full and empty detection, and acquire and release semantics rather than just a working queue.
What the Interviewer Expects
- Use monotonically increasing head and tail with a power-of-two mask
- Detect full as tail - head == capacity and empty as head == tail
- Explain why only one writer per index makes this lock-free without CAS
- State the acquire and release pairing on each index
- Mention false sharing and cached-index optimizations
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 one producer and many consumers that each see every message?
- Why can relaxed ordering on the other thread's index break correctness on ARM but often appear to work on x86?
- How would you cache the opposite index to reduce cache-line traffic?
- What should the producer do when the ring is full on a market data path?
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 calls try_push, one consumer calls try_pop. Both are non-blocking and wait-free. Capacity is fixed at construction and a power of two so index wrap is a mask....
High-Level Design
head (next read) is written only by the consumer, tail (next write) only by the producer. Because each index has a single writer, no compare-and-swap is needed: the producer writes the slot then p...