//polyglot programmer /
Sharing Data Among Threads
Without Contention
Use the Disruptor framework to do the heavy lifting for concurrent programming.
TRISHA GEE
JAVA IN ACTION
The London Multi-Asset Exchange (LMAX) Disruptor
is an open source concurrency
framework that recently won
the 2011 Duke’s Choice Award
for Innovative Programming
Framework. In this article, I use
diagrams to describe what the
Disruptor is; what it does; and, to
some extent, how it works.
the Disruptor as part of its reliable messaging architecture and
developed it into an extremely
fast way of handing off data
between different components.
The Disruptor
is a 2011
Duke’s Choice
Award winner.
Using mechanical sympathy
(an understanding of how the
underlying hardware works),
fundamental computer science,
and domain-driven design, the
Disruptor has evolved into a
framework that developers can
use to do much of the heavy lifting
for concurrent programming.
(most recent) sequence number,
which is the index pointing to the
last item in the RingBuffer. This
sequence number continually
increases as more data is added
to the ring.
What Is the Disruptor?
The Disruptor is a framework
for interthread communication
(ITC), that is, the sharing of data
among threads. LMAX created
ing thread (Stage 1 in Figure 1) to
move on to the next piece of work
if Stage 2 is too busy to accept the
data immediately, which provides
a way to deal with bursts of traffic
in a system. The queue acts as a
buffer between the threads.
ABOUT US
Figure 1
In many architectures, it is
common to use a queue to share
data (that is, pass
messages) among
threads. Figure 1
shows an example
of passing mes-
sages between
stages using a
queue. (Each little
blue spinner is
meant to represent
a thread.)
In its simplest form, the
Disruptor can be used in place
of the queue in the architecture
shown in Figure 2, in which messages are passed between stages
using the Disruptor.
The key thing about the
Disruptor is that it was designed
to have zero contention within
the framework. This is achieved
by following the single-writer
principle—only one thing can
ever write to a single piece of
data. Following this principle
eliminates the need for expensive
lock or compare and swap (CAS)
operations, which is one of the
reasons the Disruptor is so fast.
The data structure storing
the messages is a RingBuffer,
implemented as an array. Stage 1
places items into the RingBuffer,
and Stage 2 reads items from
the RingBuffer.
blog
This architecture
allows the produc-
You’ll see in Figure 2 that each
spot in the RingBuffer is indexed
by a sequence number, and the
RingBuffer tracks the highest
One source of contention has
been removed by having the
RingBuffer and each EventProcessor
track their own sequence numbers. This way, the only thing that
ever updates a sequence number is the thing that owns the
sequence number. This concept
is covered in more detail when I
describe writing to and reading
from the RingBuffer.
Go to http://medianetwork.oracle.com/video/player/1203095587001