Using LMAX Disruptor as a High‑Performance In‑Memory Message Queue in Java

This article introduces the LMAX Disruptor library, explains its core concepts such as RingBuffer, Sequencer and WaitStrategy, and provides a step‑by‑step Java demo—including Maven dependency, model, event factory, handler, manager, service and test code—to build a fast, lock‑free producer‑consumer queue.

Architecture Digest
Architecture Digest
Architecture Digest
Using LMAX Disruptor as a High‑Performance In‑Memory Message Queue in Java

Background: In a project we needed a message queue and chose Disruptor instead of Kafka or RabbitMQ because of its speed and open‑source nature.

Disruptor Introduction: Disruptor is a high‑performance queue developed by the UK forex trading company LMAX to solve memory‑queue latency. It can handle up to 6 million orders per second and gained industry attention after a 2010 QCon talk.

Core Concepts:

Ring Buffer : a circular buffer that stores events; since version 3.0 its role is limited to storage and update.

Sequence : a monotonically increasing number that tracks the processing progress of each consumer, avoiding false sharing.

Sequencer : the core component with SingleProducerSequencer and MultiProducerSequencer implementations that manage data transfer between producers and consumers.

Sequence Barrier : maintains references to the main published sequence and dependent consumer sequences, deciding whether a consumer can process more events.

Wait Strategy : defines how a consumer waits for the next event (multiple strategies with different performance characteristics).

Event : the data exchanged between producer and consumer, defined by the user.

EventProcessor : holds a consumer’s sequence and runs the event loop.

EventHandler : user‑implemented interface that processes events.

Producer : user code that publishes events to the Disruptor.

Demo – Step‑by‑step implementation:

Add the Maven dependency:

<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.4</version>
</dependency>

Create the message model:

@Data
public class MessageModel {
    private String message;
}

Implement HelloEventFactory that creates MessageModel instances.

public class HelloEventFactory implements EventFactory<MessageModel> {
    @Override
    public MessageModel newInstance() {
        return new MessageModel();
    }
}

Implement the consumer HelloEventHandler.

@Slf4j
public class HelloEventHandler implements EventHandler<MessageModel> {
    @Override
    public void onEvent(MessageModel event, long sequence, boolean endOfBatch) {
        try {
            Thread.sleep(1000);
            log.info("Consumer start processing");
            if (event != null) {
                log.info("Consumed message: {}", event);
            }
        } catch (Exception e) {
            log.info("Consumer processing failed");
        }
        log.info("Consumer end processing");
    }
}

Provide a BeanManager to access Spring beans.

Configure the Disruptor bean ( MQManager) that creates the RingBuffer, sets the event factory, wait strategy, and registers the handler.

Define the service interface DisruptorMqService and its implementation DisruptorMqServiceImpl that publishes messages to the RingBuffer.

Create a test class DemoApplicationTests that sends a sample message and logs the result.

Test Run Result:

2020-04-05 14:31:18.543 INFO 7274 --- [main] DisruptorMqServiceImpl : record the message: 消息到了,Hello world!
2020-04-05 14:31:18.545 INFO 7274 --- [main] DisruptorMqServiceImpl : 往消息队列中添加消息:MessageModel(message=消息到了,Hello world!)
2020-04-05 14:31:18.545 INFO 7274 --- [main] DemoApplicationTests : 消息队列已发送完毕
2020-04-05 14:31:19.547 INFO 7274 --- [pool-1-thread-1] HelloEventHandler : 消费者处理消息开始
2020-04-05 14:31:19.547 INFO 7274 --- [pool-1-thread-1] HelloEventHandler : 消费者消费的信息是:MessageModel(message=消息到了,Hello world!)
2020-04-05 14:31:19.547 INFO 7274 --- [pool-1-thread-1] HelloEventHandler : 消费者处理消息结束

Summary: The producer‑consumer pattern is common, and many message‑queue solutions can achieve similar results, but Disruptor implements it entirely in memory with a lock‑free design, which explains its high efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentconcurrencyMessage QueueDisruptorhigh performance
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.