Understanding the Disruptor: High‑Performance Java Queue and Its Implementation
The Disruptor library provides a lock‑free, CAS‑driven ring‑buffer queue that eliminates lock contention and false sharing, delivering 4–7× higher throughput and nanosecond‑level latency compared to Java’s built‑in queues, and is employed by systems such as Log4j 2, Apache Storm, and Meituan‑Dianping.
Disruptor is a high‑performance, lock‑free ring buffer library originally developed by LMAX to solve the latency problem of in‑memory queues. A single‑threaded Disruptor can handle millions of orders per second, and it has been adopted by projects such as Apache Storm, Camel, Log4j 2, and Meituan‑Dianping.
Java Built‑in Queues
Typical thread‑safe Java queues include ArrayBlockingQueue (lock‑based), LinkedBlockingQueue (lock‑based), ConcurrentLinkedQueue and LinkedTransferQueue (CAS‑based). Lock‑based queues can be bounded, which is useful for preventing memory overflow in high‑stability systems.
Problems of ArrayBlockingQueue
Lock contention and false sharing cause severe performance degradation. An experiment shows that a single‑threaded CAS operation is ~19× slower than a plain loop, and a locked version is ~33× slower. In multi‑threaded scenarios, CAS outperforms locks by about 8×.
Lock vs. CAS
Locks serialize access and can lead to priority inversion, while CAS provides lock‑free atomic updates. The following code shows the lock‑based offer method of ArrayBlockingQueue:
public boolean offer(E e) {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lock();
try {
if (count == items.length) return false;
else {
insert(e);
return true;
}
} finally {
lock.unlock();
}
}And an example of CAS‑based atomic operations:
public final int getAndAdd(int delta) {
for (;;) {
int current = get();
int next = current + delta;
if (compareAndSet(current, next)) return current;
}
}
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}False Sharing
When multiple variables share the same cache line, updates cause cache invalidation, reducing performance. The article demonstrates a benchmark where a cache‑line‑aware layout yields roughly 2× speedup.
Disruptor Design
Ring buffer stored in a power‑of‑two array for fast index calculation via bit‑masking.
Lock‑free coordination using CAS on sequence numbers.
Separate availableBuffer to indicate which slots have been published.
Support for single‑producer and multi‑producer scenarios.
Producer workflow: request a range of slots, write data, then publish the highest sequence. Consumer workflow mirrors this, reading only when the slot is marked available.
Sample producer/consumer code (Disruptor 3.3.4) is provided, wrapped in EventFactory, EventHandler, and a BlockingWaitStrategy.
Performance Results
Benchmarks on two hardware configurations show Disruptor achieving 4–7× higher throughput than ArrayBlockingQueue. Latency measurements reveal average Disruptor latency in the tens of nanoseconds versus tens of microseconds for the lock‑based queue.
Waiting Strategies
Disruptor offers several wait strategies (Blocking, BusySpin, Yielding, Sleeping, PhasedBackoff, TimeoutBlocking) to trade off CPU usage, throughput, and latency.
Log4j 2 Integration
Log4j 2’s asynchronous logging uses Disruptor, providing significant latency and throughput improvements over the traditional AsyncAppender that relies on ArrayBlockingQueue. In high‑concurrency tests (64 threads) the async logger is up to 12× faster.
References to the original Disruptor documentation, performance articles, and Log4j 2 async guide are listed at the end of the source.
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.
Meituan Technology Team
Over 10,000 engineers powering China’s leading lifestyle services e‑commerce platform. Supporting hundreds of millions of consumers, millions of merchants across 2,000+ industries. This is the public channel for the tech teams behind Meituan, Dianping, Meituan Waimai, Meituan Select, and related services.
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.
