Top Banner
The LMAX Disruptor Amir Langer - Outbrain (… and ex-LMAX) Pictures courtesy of Trisha Gee (… another ex-LMAX)
59

Disruptor 2015-12-22 @ java.il

Apr 12, 2017

Download

Engineering

Amir Langer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Disruptor 2015-12-22 @ java.il

The LMAX Disruptor Amir Langer - Outbrain (… and ex-LMAX) Pictures courtesy of Trisha Gee (… another ex-LMAX)

Page 2: Disruptor 2015-12-22 @ java.il

DisruptorHigh Performance Inter-Thread Messaging Library

Page 3: Disruptor 2015-12-22 @ java.il

Disruptor

• Why?

• What?

• Write flow (Event publishing)

• Read flow (Event processing)

• Behind the scenes

Page 4: Disruptor 2015-12-22 @ java.il
Page 5: Disruptor 2015-12-22 @ java.il

A Typical LMAX Service

Page 6: Disruptor 2015-12-22 @ java.il

A Typical LMAX Service

Page 7: Disruptor 2015-12-22 @ java.il

Why queues suck? (as inter-thread messaging mechanism)

Page 8: Disruptor 2015-12-22 @ java.il

Linked List not much betterUsually worse

Page 9: Disruptor 2015-12-22 @ java.il

Cost of ContentionIncrement a long counter 500M times

One Thread 300ms

Page 10: Disruptor 2015-12-22 @ java.il

Cost of ContentionIncrement a long counter 500M times

One Thread 300msOne Thread (Volatile) 4700ms x 15

Page 11: Disruptor 2015-12-22 @ java.il

Cost of ContentionIncrement a long counter 500M times

One Thread 300msOne Thread (Volatile) 4700ms x 15One Thread (Atomic) 5700ms x 19

Page 12: Disruptor 2015-12-22 @ java.il

Cost of ContentionIncrement a long counter 500M times

One Thread 300msOne Thread (Volatile) 4700ms x 15One Thread (Atomic) 5700ms x 19One Thread (Lock) 10000ms x 33

Page 13: Disruptor 2015-12-22 @ java.il

Cost of ContentionIncrement a long counter 500M times

One Thread 300msOne Thread (Volatile) 4700ms x 15One Thread (Atomic) 5700ms x 19One Thread (Lock) 10000ms x 33Two Threads (Atomic) 30000ms x 100

Page 14: Disruptor 2015-12-22 @ java.il

Cost of ContentionIncrement a long counter 500M times

One Thread 300msOne Thread (Volatile) 4700ms x 15One Thread (Atomic) 5700ms x 19One Thread (Lock) 10000ms x 33Two Threads (Atomic) 30000ms x 100Two Threads (Lock) 224000ms x 746

That’s almost 4 minutes!!!

Page 15: Disruptor 2015-12-22 @ java.il

Disruptor

Page 16: Disruptor 2015-12-22 @ java.il

Building Blocks• RingBuffer

• EventFactory

• WaitStrategy

• Publisher

• EventTranslator

• EventProcessor

• Sequence & SequenceBarrier

Page 17: Disruptor 2015-12-22 @ java.il

• RingBuffer

• EventFactory

• WaitStrategy

• Publisher

• EventTranslator

• EventProcessor

• Sequence & SequenceBarrier

Building Blocks

Page 18: Disruptor 2015-12-22 @ java.il

• Events are mutable instances of T• Sequence denotes last available event

• Publisher will publish to the next slot• EventProcessor will read up to including sequence

Page 19: Disruptor 2015-12-22 @ java.il

• RingBuffer

• EventFactory

• WaitStrategy

• Publisher

• EventTranslator

• EventProcessor

• Sequence & SequenceBarrier

Building Blocks

Page 20: Disruptor 2015-12-22 @ java.il
Page 21: Disruptor 2015-12-22 @ java.il
Page 22: Disruptor 2015-12-22 @ java.il
Page 23: Disruptor 2015-12-22 @ java.il
Page 24: Disruptor 2015-12-22 @ java.il
Page 25: Disruptor 2015-12-22 @ java.il
Page 26: Disruptor 2015-12-22 @ java.il
Page 27: Disruptor 2015-12-22 @ java.il

• SequenceBarrier• GatingSequence

Page 28: Disruptor 2015-12-22 @ java.il

• RingBuffer

• EventFactory

• WaitStrategy

• Publisher

• EventTranslator

• EventProcessor

• Sequence & SequenceBarrier

Building Blocks

Page 29: Disruptor 2015-12-22 @ java.il
Page 30: Disruptor 2015-12-22 @ java.il
Page 31: Disruptor 2015-12-22 @ java.il
Page 32: Disruptor 2015-12-22 @ java.il
Page 33: Disruptor 2015-12-22 @ java.il
Page 34: Disruptor 2015-12-22 @ java.il
Page 35: Disruptor 2015-12-22 @ java.il
Page 36: Disruptor 2015-12-22 @ java.il
Page 37: Disruptor 2015-12-22 @ java.il
Page 38: Disruptor 2015-12-22 @ java.il
Page 39: Disruptor 2015-12-22 @ java.il
Page 40: Disruptor 2015-12-22 @ java.il
Page 41: Disruptor 2015-12-22 @ java.il
Page 42: Disruptor 2015-12-22 @ java.il
Page 43: Disruptor 2015-12-22 @ java.il

Single Writer Principle

Page 44: Disruptor 2015-12-22 @ java.il

Memory Barrier

Page 45: Disruptor 2015-12-22 @ java.il

Memory Barrier

Page 46: Disruptor 2015-12-22 @ java.il

Memory Barrier

• Load Barrier - invalidates CPU cache and forces all load instructions after the barrier to happen after the barrier

• Store Barrier - forces all store instructions prior to the barrier to happen before the barrier and have the store buffers flushed to cache for the CPU on which it is issued.

a type of instruction that causes a central processing unit (CPU) to enforce an ordering constraint on memory operations issued before and after the barrier

instruction

Page 47: Disruptor 2015-12-22 @ java.il

in Java

volatile - a volatile field has a store barrier inserted after a write to it and a load barrier inserted before a read of it.

Page 48: Disruptor 2015-12-22 @ java.il
Page 49: Disruptor 2015-12-22 @ java.il

By Sticking to the Single Writer Principle we can use only volatile counters to synchronize between the producer and eventProcessor

Page 50: Disruptor 2015-12-22 @ java.il

What about Multiple Producers? Multiple Consumers?

Page 51: Disruptor 2015-12-22 @ java.il

Why queues suck? (part 2)

Page 52: Disruptor 2015-12-22 @ java.il

Zero GarbageOh, and its all Mutable!!!

Page 53: Disruptor 2015-12-22 @ java.il

Why queues suck? (Part 3)

Page 54: Disruptor 2015-12-22 @ java.il

Batching EffectWithout any contention cost during it

Page 55: Disruptor 2015-12-22 @ java.il

Going Parallel

Page 56: Disruptor 2015-12-22 @ java.il

How Fast Is It - Latency

ArrayBlockingQueue Disruptor

Min 145 29

Mean 32,757 52

99 Percentile 2,097,152 128

99.99 Percentile 4,194,304 8,192

Max 5,069,086 175,567

Page 57: Disruptor 2015-12-22 @ java.il

How Fast Is It - Throughput

0.0

7500000.0

15000000.0

22500000.0

30000000.0

Unicast Diamond

ABQ Disruptor

Page 58: Disruptor 2015-12-22 @ java.il

When not to use it?

• Events are not deltas but snapshots so that only the latest needs to be dealt with

• No way of separating your domain logic to multiple instances (You’re locked to either having shared state between instances (DB) or one instance with multiple threads)

Page 59: Disruptor 2015-12-22 @ java.il

Thanks for listening

Questions?

• https://github.com/LMAX-Exchange/disruptor

• http://mechanical-sympathy.blogspot.co.uk/

• https://www.cs.umd.edu/~pugh/java/memoryModel/