Backend Development 11 min read

Introducing Disruptor: A High‑Performance In‑Memory Queue with a Complete Java Demo

This article introduces the open‑source Java library Disruptor, explains its core concepts such as RingBuffer, Sequence, and WaitStrategy, and provides a step‑by‑step demo with Maven dependency, event model, factory, handler, and a Spring‑Boot test illustrating high‑throughput producer‑consumer messaging.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Introducing Disruptor: A High‑Performance In‑Memory Queue with a Complete Java Demo

Disruptor is an open‑source Java framework originally developed by LMAX Exchange to achieve ultra‑low latency and high throughput for producer‑consumer scenarios. It replaces traditional blocking queues with a lock‑free ring buffer, enabling millions of operations per second with minimal CPU overhead.

Core Concepts

Ring Buffer : A circular buffer that stores events; from version 3.0 its responsibility is limited to storing and updating data.

Sequence : Monotonically increasing numbers that identify each event and track consumer progress, helping avoid false sharing.

Sequencer : Interface with implementations SingleProducerSequencer and MultiProducerSequencer that coordinate data transfer between producers and consumers.

Sequence Barrier : Determines whether a consumer can process the next event based on published sequences.

Wait Strategy : Defines how a consumer waits for new events (e.g., BlockingWaitStrategy , BusySpinWaitStrategy , etc.).

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

EventProcessor and EventHandler : Interfaces that implement the consumer logic.

Producer : User code that publishes events to the ring buffer.

Demo Implementation (8 steps)

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

Create the message model: /** * Message body */ @Data public class MessageModel { private String message; }

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

Implement the consumer ( EventHandler ): @Slf4j public class HelloEventHandler implements EventHandler<MessageModel> { @Override public void onEvent(MessageModel event, long sequence, boolean endOfBatch) { try { Thread.sleep(1000); // simulate async processing 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 bean manager to access Spring context: @Component public class BeanManager implements ApplicationContextAware { private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { this.applicationContext = ctx; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Object getBean(String name) { return applicationContext.getBean(name); } public static T getBean(Class clazz) { return applicationContext.getBean(clazz); } }

Configure the Disruptor bean: @Configuration public class MQManager { @Bean("messageModel") public RingBuffer<MessageModel> messageModelRingBuffer() { ExecutorService executor = Executors.newFixedThreadPool(2); HelloEventFactory factory = new HelloEventFactory(); int bufferSize = 1024 * 256; // must be power of 2 Disruptor<MessageModel> disruptor = new Disruptor<>(factory, bufferSize, executor, ProducerType.SINGLE, new BlockingWaitStrategy()); disruptor.handleEventsWith(new HelloEventHandler()); disruptor.start(); return disruptor.getRingBuffer(); } }

Define the service interface and implementation (producer): public interface DisruptorMqService { void sayHelloMq(String message); } @Slf4j @Component @Service public class DisruptorMqServiceImpl implements DisruptorMqService { @Autowired private RingBuffer<MessageModel> messageModelRingBuffer; @Override public void sayHelloMq(String message) { log.info("record the message: {}", message); long sequence = messageModelRingBuffer.next(); try { MessageModel event = messageModelRingBuffer.get(sequence); event.setMessage(message); log.info("Added message to queue: {}", event); } catch (Exception e) { log.error("failed to add event", e); } finally { messageModelRingBuffer.publish(sequence); } } }

Create a Spring‑Boot test to publish a message: @Slf4j @RunWith(SpringRunner.class) @SpringBootTest(classes = DemoApplication.class) public class DemoApplicationTests { @Autowired private DisruptorMqService disruptorMqService; @Test public void sayHelloMqTest() throws Exception { disruptorMqService.sayHelloMq("Message arrived, Hello world!"); log.info("Message queue sent"); Thread.sleep(2000); // wait for async consumer } }

The test output shows the producer logging the message, the consumer receiving it after a short delay, and the overall flow completing without blocking, demonstrating Disruptor’s lock‑free, high‑throughput capabilities.

Conclusion

Disruptor implements the classic producer‑consumer pattern entirely in memory with a lock‑free design, delivering far higher performance than traditional message brokers. Its core components—RingBuffer, Sequence, Sequencer, and various wait strategies—make it a valuable tool for latency‑sensitive backend systems.

Javabackend developmentDisruptorHigh Performanceproducer-consumerConcurrent Queue
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login 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.