2. About
me
• Call
me
Azrul
• Solu'on
Architect
at
Experian
Decision
Analy'cs
• In
charge
of
Experian’s
stuff:
– BI
– Business
Ac'vity
Monitoring
– Integra'on
• OSGi
stuff
• Love
to
read
• Black
belt
in
Aikido
• No
concurrency
expert,
–
just
a
wild
enthusiast!!
3. Menu
of
the
day
• Why
concurrency?
• Concurrency
is
hard
• Producer
consumer
• Using
disruptor
• PaUerns
4. Why
concurrency?
• Cloud
• Mul'-‐core
• Mul'-‐channel
• Complex
business
process
5. Concurrency
is
hard!
Wait
for
resource
A
to
be
available
Reserve
resource
A
Wait
for
resource
B
to
be
available
Reserve
resource
B
Use
A
and
B
Release
A
and
B
6. Concurrency
is
hard!
Wait
for
resource
A
Wait
for
resource
B
to
be
available
to
be
available
Reserve
resource
A
Reserve
resource
B
Wait
for
resource
B
Wait
for
resource
A
to
be
available
to
be
available
Reserve
resource
B
Reserve
resource
A
Use
A
and
B
Use
A
and
B
Release
A
and
B
Release
A
and
B
7. Concurrency
is
hard!
Wait
for
resource
A
Wait
for
resource
B
to
be
available
to
be
available
Reserve
resource
A
Reserve
resource
B
Wait
for
resource
B
Stuck
here!
Wait
for
resource
A
to
be
available
to
be
available
Reserve
resource
B
Reserve
resource
A
Use
A
and
B
Use
A
and
B
Release
A
and
B
Release
A
and
B
8. Concurrency
is
hard!
• Random
decisions
in
different
places
influence
each
other
• Influence
each
other
BADLY!
9. Solving
concurrency
with
locking
Wait
for
resource
A
Wait
for
resource
B
to
be
available
to
be
available
Reserve
resource
A
Reserve
resource
B
Monitor
Wait
for
resource
B
Wait
for
resource
A
to
be
available
to
be
available
Reserve
resource
B
Reserve
resource
A
Use
A
and
B
Use
A
and
B
Release
A
and
B
Release
A
and
B
10. Solving
concurrency
with
locking
• Locking
is
done
through
‘monitors’
• OS
or
VM
level
• Like
a
toilet
– If
someone
is
using
it,
everyone
else
has
to
wait
– Acquiring
a
lock
is
generally
SLOW!
11. CAS
to
the
rescue
• CAS
=
Compare-‐And-‐Swap
• ‘Hardware’
level
locking
• Atomic
opera'ons
• Crazy
fast
–
mul'-‐core
friendly
12. CAS
to
the
rescue
–
incremen'ng
a
value
Shared
memory
Get
‘input’
Input
=
21
21
Changed
by
another
Increment
Input
=
21
result
=
22
thread
Compare
Input
=
21
Different
Same
Set
result
=
22
13. CAS
to
the
rescue
• Java
example
AtomicInteger
counter
=
new
AtomicInteger(1);
…
int
currentValue
=
counter.getAndIncrement();
18. Producer
-‐
consumer
• Other
examples
Industry
Producer
Consumer
Banking
Credit
card
User
“credit
applica'on
worthiness”
verifica'on
Insurance
Insurance
Underwri'ng
applica'on
process
Manufacturing
Raw
material
Factory
worker
delivery
19. Producer
-‐
consumer
• Problems
– Producer
is
producing
faster
than
consumer
can
consumer
20. Solu'on
1:
Array
blocking
queue
• Classic
solu'on:
Using
array
blocking
queue
– Producer
would
queue
things
if
consumer
is
slow
– Consumer
can
consume
at
its
own
pace
– If
queue
is
full,
producer
will
be
blocked
21. Solu'on
1:
Array
blocking
queue
• Classic
solu'on:
Using
array
blocking
queue
– Advantage:
Scalability
=
many
consumers
22. Solu'on
1:
Array
blocking
queue
• Main
problem:
– Queue
need
to
be
locked
• For
consumer
to
read
• For
producer
to
write
– Producer
is
blocked
if
queue
is
full
– Lock
=>
Slow
23. Solu'on
2:
Using
ring
buffer
&
CAS
• Producer
will
never
be
blocked
–
we
just
“wrap
around”
the
circular
queue
(write
over
older
entry)
• Elements
of
ring
buffer
are
set
using
CAS
• =>
Ring
buffer
is
very
very
fast
24. Solu'on
2:
Using
ring
buffer
&
CAS
• Ring
buffer
+
CAS
=
Disruptor
• Create
by
LMAX
– Doing
High
frequency
trading
• “100
k
TPS
at
1
ms
latency”
• Disruptor
also
includes
a
few
other
op'miza'ons
26. Loan
applica'on
processing
Applica'on
by
user
Pre-‐bureau
processing
Call
credit
bureau
Approved
/
refer
/
reject
Save
data
27. Loan
applica'on
processing
Applica'on
by
user
Applica'on
by
user
Pre-‐bureau
processing
Pre-‐bureau
processing
Call
credit
bureau
Call
credit
bureau
Approved
/
refer
/
reject
Approved
/
refer
/
reject
Save
data
Save
data
28. Loan
applica'on
processing
Applica'on
by
user
Pre-‐bureau
processing
X
100
every
Call
credit
bureau
second
Approved
/
refer
/
reject
Save
data
29. Loan
applica'on
processing
Applica'on
by
user
Pre-‐bureau
processing
Call
credit
bureau
Approved
/
refer
/
reject
Save
data
30. Loan
applica'on
processing
Applica'on
by
user
Pre-‐bureau
processing
Tedious
in
your
Call
credit
bureau
typical
applica'on
server
(resort
to
JMS)
Approved
/
refer
/
reject
Save
data
31. Loan
applica'on
processing
–
with
Disruptor
Applica'on
by
user
Disruptor
Pre-‐bureau
processing
Call
credit
bureau
Approved
/
refer
/
reject
Save
data
32. Loan
applica'on
processing
–
with
Disruptor
Applica'on
by
user
Disruptor
Handle
‘forking’
Pre-‐bureau
processing
Handle
parallel
users
Handle
‘workflow’
Call
credit
bureau
Approved
/
refer
/
reject
Save
data
33. Loan
applica'on
processing
–
with
Disruptor
Disruptor<MyEvent>
disruptor
=
new
Disruptor<MyEvent>(…);
//read
data
disruptor.handleEventsWith(preBureauProcessing).
//first
do
this
then(callBureau).
//next
do
this
then(
approvedReferReject,
saveData);
//arer
that,
do
these
in
parallel
//run
disruptor
RingBuffer<ValueEvent>
ringBuffer
=
disruptor.start();
//write
(applica'on
by
user)
long
sequence
=
ringBuffer.next();
MyEvent
event
=
ringBuffer.get(sequence);
event.setValue(x);
ringBuffer.publish(sequence);
43. Queue
replacement
• Use
WorkerPool
WorkerPool<ValueEvent>
workerPool
=
new
WorkerPool<ValueEvent>(…);
workerPool.start(…)
;
44. Actors
• A
unit
of
concurrent
computa'on
• From
wikipedia:
– “…
in
response
to
a
message
that
it
receives,
an
actor
can
make
local
decisions,
create
more
actors,
send
more
messages,
and
determine
how
to
respond
to
the
next
message
received”
51. Conclusion
• Disruptor
–
interes'ng
and
‘disrup've’
framework
• Revenge
of
the
“object
oriented
programming”
model
J
• Do
your
own
experiments
• Check
out:
– hUp://code.google.com/p/disruptor/
– hUp://mar'nfowler.com/ar'cles/lmax.html
– hUp://mechani's.blogspot.com/2011/07/dissec'ng-‐
disruptor-‐wiring-‐up.html