Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Disruptor –Ultrafast communicationMarch 2012#theedge2012Guy Raz Nir
Disruptor» Introduction» The problem …» The (not so good) alternatives» Architecture» SummaryAgenda
DisruptorHida !(Quiz!)
DisruptorIntroduction
Disruptor» QUEUE !» Communication facility between threads.Disruptor
Disruptor» London Multi Asset Exchange platform» Can handle up to 6,000,000 TPS▪ Dual socket, 3GHz quad-core Nehalem proce...
Disruptor» To learn about LMAX Disruptor abilities andusability.» To practice “different thinking” in order tosolve comple...
Disruptor"Any intelligent fool can make things bigger,more complex, and more violent.It takes a touch of genius,and a lot ...
Disruptor
Disruptor57.3 MB/s-20%
Disruptor“Mechanical Sympathy”Hardware and software working together in harmony ** Martin Thompson’s blog
DisruptorThe problem …
Disruptor» Test case: execution 10,000,000 primitiveincrements.» Single thread execution: ~ 7ms▪ No concurrencyMulti-threa...
Disruptor» Synchronized approach:» Mutual exclusion approach:» AtomicLong:java.util.concurrent.locks.ReentrantLock lock = ...
Disruptor» Single thread, bare execution (value++)▪ About ~7 milliseconds» Single thread, AtomicLong▪ 68 milliseconds (x8....
Disruptor» Single thread, bare execution (value++)▪ About ~7 milliseconds» Two threads, AtomicLong▪ 270 milliseconds (x33....
DisruptorConcurrent execution latency (increment)Time(ms)Number of threadsSynchronized CAS Lock
DisruptorConcurrent execution latency (PI calculation)Time(ms)Number of threadsSynchronized Lock
“A good preliminary designovercomes any lastly patch”Guy (Raz) Nir, The Edge 2012
DisruptorCore #1L1 cacheCore #2L1 cacheCore #3L1 cacheCore #4L1 cacheL2 cache L2 cache L2 cache L2 cacheL3 cache32KB inst....
DisruptorNon-volatile vs volatileNon-volatile Volatile7Time(ms)
DisruptorThe (not so good) alternatives
Disruptor» Linked-list based queue▪ Requires re-allocation of units▪ Memory fragmentation▪ Garbage collection▪ Bad content...
Disruptor» Cyclic array-based queue▪ Bad contention#0 #1 #2 #3 #4HeadTail
Disruptorjava.util.concurrent.ArrayBlockingQueue// Put new element in the queue.public boolean offer(E e, long timeout, Ti...
Disruptor» General purpose assumptions:▪ Multiple readers, multiple writers▪ Queues can run as big as memory▪ Other operat...
DisruptorArchitecture
Disruptor» Barriers» Ring buffer» SequencesMain components
DisruptorsBarriers
DisruptorThe ring buffer1 234MyDataType[] buffer = ...;int offset = sequence % buffer.length;Next readsequenceAvailableseq...
Disruptor» Array-based cyclic buffer.▪ Fast index-based accessed.» Allow us to allocate all entries in advance▪ Save GC ti...
Ring BufferDisruptorBarriers1 2345Producer ConsumersequencenextSequence
Disruptorpublic class StandardProducer {public void offer(Object o) {// ...}}public class DisruptorProducer {private RingB...
Disruptorpublic class DisruptorConsumer {private RingBuffer buffer;int nextSequenceNumber;public Object take() {while (nex...
Ring BufferDisruptorMulti consumers1 2345ConsumersequencenextSequence = 2SequencebarrierConsumernextSequence = 3Consumerne...
Disruptor» Allow us to fetch multiple elements.» Using event processors▪ CallbacksBatches & Events
DisruptorCode sample – Create ring buffer//// Create a new ring buffer.//RingBuffer<MyEvent> ringBuffer = new RingBuffer<M...
DisruptorCode sample - Producer// Request the next available sequence number.long sequence = buffer.next();// Fetch the ob...
DisruptorCode sample - Consumer// Extract a consumers barrier.SequenceBarrier barrier = ringBuffer.newBarrier();// Wait fo...
Disruptor» Disruptor is a smart Queue.» Latest release is 2.8» Exploit hardware acceleration points.» Won the Duke’s 2011 ...
Disruptor» Google code:▪ http://code.google.com/p/disruptor/» Technical paper:▪ http://disruptor.googlecode.com/files/Disr...
Guy Raz Nirguyn@alphacsp.com
Upcoming SlideShare
Loading in …5
×

LMAX Disruptor as real-life example

19,182 views

Published on

LMAX Disruptor as real-life example for out-of-the-box thinking.

Published in: Technology, Education
  • Be the first to comment

LMAX Disruptor as real-life example

  1. 1. Disruptor –Ultrafast communicationMarch 2012#theedge2012Guy Raz Nir
  2. 2. Disruptor» Introduction» The problem …» The (not so good) alternatives» Architecture» SummaryAgenda
  3. 3. DisruptorHida !(Quiz!)
  4. 4. DisruptorIntroduction
  5. 5. Disruptor» QUEUE !» Communication facility between threads.Disruptor
  6. 6. Disruptor» London Multi Asset Exchange platform» Can handle up to 6,000,000 TPS▪ Dual socket, 3GHz quad-core Nehalem processors.» 98% transactions under 38ms.» Average transaction length: 9.22msLMAX
  7. 7. Disruptor» To learn about LMAX Disruptor abilities andusability.» To practice “different thinking” in order tosolve complex problems.Why are we here ?
  8. 8. Disruptor"Any intelligent fool can make things bigger,more complex, and more violent.It takes a touch of genius,and a lot of courageto move in the opposite direction.“Albert Einstein
  9. 9. Disruptor
  10. 10. Disruptor57.3 MB/s-20%
  11. 11. Disruptor“Mechanical Sympathy”Hardware and software working together in harmony ** Martin Thompson’s blog
  12. 12. DisruptorThe problem …
  13. 13. Disruptor» Test case: execution 10,000,000 primitiveincrements.» Single thread execution: ~ 7ms▪ No concurrencyMulti-threading testlong value = 0;while (value < 10000000L) {value++;
  14. 14. Disruptor» Synchronized approach:» Mutual exclusion approach:» AtomicLong:java.util.concurrent.locks.ReentrantLock lock = …lock.lock();value++;lock.unlock();synchronized (syncObj) {value++;AtomicLong value = new AtomicLong(0);long result = value.incrementAndGet();
  15. 15. Disruptor» Single thread, bare execution (value++)▪ About ~7 milliseconds» Single thread, AtomicLong▪ 68 milliseconds (x8.5)» Single thread with lock▪ 125 milliseconds (x15.5)» Single thread with synchronized approach▪ 450 milliseconds (x56)
  16. 16. Disruptor» Single thread, bare execution (value++)▪ About ~7 milliseconds» Two threads, AtomicLong▪ 270 milliseconds (x33.7)» Two thread with lock▪ 298 milliseconds (x37.5)
  17. 17. DisruptorConcurrent execution latency (increment)Time(ms)Number of threadsSynchronized CAS Lock
  18. 18. DisruptorConcurrent execution latency (PI calculation)Time(ms)Number of threadsSynchronized Lock
  19. 19. “A good preliminary designovercomes any lastly patch”Guy (Raz) Nir, The Edge 2012
  20. 20. DisruptorCore #1L1 cacheCore #2L1 cacheCore #3L1 cacheCore #4L1 cacheL2 cache L2 cache L2 cache L2 cacheL3 cache32KB inst.32KB data256KB2M – 16M64-bitregistersModel CPU architecture
  21. 21. DisruptorNon-volatile vs volatileNon-volatile Volatile7Time(ms)
  22. 22. DisruptorThe (not so good) alternatives
  23. 23. Disruptor» Linked-list based queue▪ Requires re-allocation of units▪ Memory fragmentation▪ Garbage collection▪ Bad contention#0 #1 #2 #3 #4
  24. 24. Disruptor» Cyclic array-based queue▪ Bad contention#0 #1 #2 #3 #4HeadTail
  25. 25. Disruptorjava.util.concurrent.ArrayBlockingQueue// Put new element in the queue.public boolean offer(E e, long timeout, TimeUnit unit) {// Consume ‘lock’ for writing.final ReentrantLock lock = this.lock;lock.lock();}// Take one element from the queue.public E poll() {// Consume ‘lock’ for reading.final ReentrantLock lock = this.lock;lock.lock();}Sun (Oracle) JDK 1.7.0_u2
  26. 26. Disruptor» General purpose assumptions:▪ Multiple readers, multiple writers▪ Queues can run as big as memory▪ Other operations that degrade design» No regards to hardwareOther problems
  27. 27. DisruptorArchitecture
  28. 28. Disruptor» Barriers» Ring buffer» SequencesMain components
  29. 29. DisruptorsBarriers
  30. 30. DisruptorThe ring buffer1 234MyDataType[] buffer = ...;int offset = sequence % buffer.length;Next readsequenceAvailablesequence
  31. 31. Disruptor» Array-based cyclic buffer.▪ Fast index-based accessed.» Allow us to allocate all entries in advance▪ Save GC time▪ Continuous block allocation▪ Save new costs at runtime.The ring buffer1 2345
  32. 32. Ring BufferDisruptorBarriers1 2345Producer ConsumersequencenextSequence
  33. 33. Disruptorpublic class StandardProducer {public void offer(Object o) {// ...}}public class DisruptorProducer {private RingBuffer buffer;public void addMessage(String message, long timestamp) {int seq = buffer.writeSequenceNumber++;buffer.data[seq].msg = message;buffer.data[seq].timestamp = timestamp;buffer.availableSequenceNumber = seq;}}X
  34. 34. Disruptorpublic class DisruptorConsumer {private RingBuffer buffer;int nextSequenceNumber;public Object take() {while (nextSequenceNumber < buffer.sequenceNumber) { .. }return buffer.get(nextSequenceNumber++);}}Buffer.sequenceNumberMysequencenumber
  35. 35. Ring BufferDisruptorMulti consumers1 2345ConsumersequencenextSequence = 2SequencebarrierConsumernextSequence = 3ConsumernextSequence = 4
  36. 36. Disruptor» Allow us to fetch multiple elements.» Using event processors▪ CallbacksBatches & Events
  37. 37. DisruptorCode sample – Create ring buffer//// Create a new ring buffer.//RingBuffer<MyEvent> ringBuffer = new RingBuffer<MyEvent>(new MyOwnFactory(),new SingleThreadedClaimStrategy(sizeOfRing),new SleepingWaitStrategy());
  38. 38. DisruptorCode sample - Producer// Request the next available sequence number.long sequence = buffer.next();// Fetch the object at the that location.MyEvent event = buffer.get(sequence);//// ... do something with the event.//// Notify the rest of the world this event is ready to be consumed.buffer.publish(sequence);
  39. 39. DisruptorCode sample - Consumer// Extract a consumers barrier.SequenceBarrier barrier = ringBuffer.newBarrier();// Wait for an event to come.barrier.waitFor(nextSequence);// Take the event (data).MyEvent event = ringBuffer.get(nextSequence);
  40. 40. Disruptor» Disruptor is a smart Queue.» Latest release is 2.8» Exploit hardware acceleration points.» Won the Duke’s 2011 award for innovation !Summary
  41. 41. Disruptor» Google code:▪ http://code.google.com/p/disruptor/» Technical paper:▪ http://disruptor.googlecode.com/files/Disruptor-1.0.pdf» Martin Thompson’s blog:▪ http://mechanical-sympathy.blogspot.com» Trisha Gee’s blog:▪ http://mechanitis.blogspot.com/» InfoQ on Disruptor (session video):▪ http://www.infoq.com/presentations/LMAXReferences
  42. 42. Guy Raz Nirguyn@alphacsp.com

×