The Disruptor pattern is a high-performance inter-thread messaging library and concurrency design pattern developed by LMAX—a low-latency financial exchange.
It’s designed to pass data between threads with minimal latency, avoiding the overhead of traditional queues, locks, or concurrent collections like BlockingQueue
. The Disruptor achieves extreme throughput and low latency by using preallocated ring buffers and a lock-free, single-writer design.
Key Concepts
Ring Buffer
- A fixed-size circular array where data is placed by a single producer and consumed by one or more consumers.
- Memory is preallocated, so there's no allocation/deallocation during runtime (which eliminates GC pressure in managed languages like Java).
Sequencer
- Maintains a sequence number for each slot in the ring buffer.
- Producers and consumers coordinate using these sequence numbers instead of locks.
Memory Barriers / Fences
- Instead of using locks, Disruptor uses memory barriers to ensure visibility and ordering across CPU cores.
- This leverages mechanics of modern CPU caches for very fast coordination.
Single Writer Principle
- Only one thread writes to the ring buffer. This prevents contention and eliminates the need for complex synchronization.
Multiple Consumer Patterns
- Work processors (each consumer gets a subset of messages)
- Broadcast processors (each consumer sees every message, like an event bus)
Why It's Fast
- Avoids lock contention
- Preallocates memory
- Uses mechanical sympathy: carefully designed for CPU cache lines, memory barriers, and branch prediction
- Minimizes cache coherence traffic and false sharing
Typical Use Cases
- High-frequency trading systems
- Market data distribution
- Low-latency event pipelines
- Real-time analytics and telemetry
Comparison with Other Queues
Feature | Disruptor | BlockingQueue | ArrayBlockingQueue |
---|---|---|---|
Lock-free | Yes | No | No |
Allocation-free | Yes | No | No |
Throughput | Very high (millions/sec) | Medium | Medium |
Latency | Sub-10μs possible | 100–500μs | 100–500μs |
Complexity | High | Low | Low |
Ecosystem
- Native implementation is in Java, but the concept has been ported to C++, Rust, Go, and more.
- Some libraries inspired by Disruptor include:
- Chronicle Queue (Java)
- Agrona (low-level library behind Disruptor)
- Tranquility (Scala)
Real-World Example
LMAX used Disruptor to build a financial exchange that:
- Runs on a single-threaded business logic engine
- Handles 6 million orders per second
- With sub-millisecond end-to-end latency
If you're working in C++ or another systems language, Disruptor can serve as inspiration for a lock-free ring buffer with memory fences and cache-aware data structures. It's particularly powerful when paired with NUMA-awareness and low-latency OS tuning.