Lock-Free Queues And Ring Buffers
Data structures used in low-latency trading systems that let one thread hand off data to another without ever making either thread wait on a lock.
Passing data between threads normally uses a lock: a thread wanting to read or write a shared queue grabs a lock first, does its work, then releases it, so two threads never touch the queue at the same instant. Locks are simple and correct, but they cost time — acquiring one can force a thread to sleep and be woken later by the operating system, introducing delays of microseconds that are unacceptable in a system where every microsecond of latency is money. Low-latency trading systems instead use lock-free queues, where threads coordinate using atomic hardware instructions (compare-and-swap operations that succeed or fail instantly, never blocking) rather than ever waiting on a lock.
A ring buffer is the concrete structure most often used to build one: a fixed-size array treated as circular, where a producer thread writes new items advancing a write pointer and a consumer thread reads items advancing a read pointer, wrapping both pointers back to the start once they reach the end of the array. Because the array's size is fixed and never resized during operation, there's no memory allocation happening on the hot path — allocation is itself a latency hazard, since it can trigger unpredictable delays.
The famous production example is the LMAX Disruptor, a ring-buffer-based queue built for a financial exchange specifically to avoid lock contention between the many threads processing incoming orders, and it remains a widely cited reference design for anyone building a low-latency trading pipeline.
Lock-free structures trade the simplicity of locks for atomic hardware operations, avoiding the unpredictable pauses that locks introduce — essential when microseconds of jitter matter.
Related concepts
Practice in interviews
Further reading
- Fowler, LMAX Disruptor: High Performance Alternative to Bounded Queues