Mastering Disruptor: High‑Performance In‑Memory Queue for Low‑Latency Java Apps
This article introduces the Disruptor in‑memory message queue—its architecture, core concepts such as Ring Buffer, Sequence, Sequencer and Wait Strategies, key features like multicast and gating, tuning options for producers and wait strategies, and provides a complete Java example to help developers build ultra‑low‑latency systems.
Overview
Disruptor is an in‑memory message queue designed for ultra‑low latency, originally developed by LMAX to explore high‑performance, non‑blocking concurrency algorithms.
Core Concepts
Ring Buffer
The Ring Buffer stores and updates data passed through Disruptor; from version 3.0 it can be replaced by users in advanced scenarios.
Sequence
Each consumer holds a Sequence to identify its position; it behaves like an AtomicLong without false sharing.
Sequencer
The Sequencer is the heart of Disruptor, with SingleProducerSequencer and MultiProducerSequencer implementations, ensuring correct data transfer between producers and consumers.
Sequence Barrier
A Sequence Barrier combines the Sequencer’s sequences with consumer sequences to determine when events are ready for processing.
Wait Strategy
Disruptor provides several wait strategies (Blocking, Sleeping, Yielding, BusySpin) to balance latency and CPU usage.
Event Processor & Event Handler
Event Processors loop over events, owning the consumer’s Sequence; the Event Handler is implemented by the user to process each event.
Key Features
Multicast events allow multiple consumers to receive the same event.
Gating (consumer dependency) ensures downstream consumers wait for upstream processing to complete.
Memory pre‑allocation reduces garbage‑collection pauses.
Lock‑free concurrency with CAS and memory barriers; only BlockingWaitStrategy uses locks.
Tuning Options
Producer Type
Choosing ProducerType.SINGLE creates a single‑producer Sequencer, while ProducerType.MULTI enables multiple producers.
Wait Strategies
BlockingWaitStrategy – uses locks and conditions.
SleepingWaitStrategy – uses LockSupport.parkNanos(1).
YieldingWaitStrategy – busy‑spins with Thread.yield().
BusySpinWaitStrategy – highest performance, suitable for low‑latency systems.
Official Java Example
public class LongEvent {
private long value;
public void set(long value) { this.value = value; }
@Override public String toString() { return "LongEvent{value=" + value + '}'; }
}
public class LongEventFactory implements EventFactory<LongEvent> {
@Override public LongEvent newInstance() { return new LongEvent(); }
}
public class LongEventHandler implements EventHandler<LongEvent> {
@Override public void onEvent(LongEvent event, long sequence, boolean endOfBatch) {
System.out.println("Event: " + event);
}
}
public class LongEventMain {
public static void main(String[] args) throws Exception {
int bufferSize = 1024;
Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE);
disruptor.handleEventsWith((event, sequence, endOfBatch) -> System.out.println("Event: " + event));
disruptor.start();
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; ; l++) {
bb.putLong(0, l);
ringBuffer.publishEvent((event, seq, buffer) -> event.set(buffer.getLong(0)), bb);
Thread.sleep(1000);
}
}
}Conclusion
Disruptor’s design—memory pre‑allocation, lock‑free concurrency, and a simple API—makes it a powerful choice for building high‑performance Java applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
