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 2015-12-22 @ java.il

482 views

Published on

Presentation on LMAX disruptor @ java.il

22/12/2015

Published in: Engineering
  • Be the first to comment

Disruptor 2015-12-22 @ java.il

  1. 1. The LMAX Disruptor Amir Langer - Outbrain (… and ex-LMAX) Pictures courtesy of Trisha Gee (… another ex-LMAX)
  2. 2. Disruptor High Performance Inter-Thread Messaging Library
  3. 3. Disruptor • Why? • What? • Write flow (Event publishing) • Read flow (Event processing) • Behind the scenes
  4. 4. A Typical LMAX Service
  5. 5. A Typical LMAX Service
  6. 6. Why queues suck? (as inter-thread messaging mechanism)
  7. 7. Linked List not much better Usually worse
  8. 8. Cost of Contention Increment a long counter 500M times One Thread 300ms
  9. 9. Cost of Contention Increment a long counter 500M times One Thread 300ms One Thread (Volatile) 4700ms x 15
  10. 10. Cost of Contention Increment a long counter 500M times One Thread 300ms One Thread (Volatile) 4700ms x 15 One Thread (Atomic) 5700ms x 19
  11. 11. Cost of Contention Increment a long counter 500M times One Thread 300ms One Thread (Volatile) 4700ms x 15 One Thread (Atomic) 5700ms x 19 One Thread (Lock) 10000ms x 33
  12. 12. Cost of Contention Increment a long counter 500M times One Thread 300ms One Thread (Volatile) 4700ms x 15 One Thread (Atomic) 5700ms x 19 One Thread (Lock) 10000ms x 33 Two Threads (Atomic) 30000ms x 100
  13. 13. Cost of Contention Increment a long counter 500M times One Thread 300ms One Thread (Volatile) 4700ms x 15 One Thread (Atomic) 5700ms x 19 One Thread (Lock) 10000ms x 33 Two Threads (Atomic) 30000ms x 100 Two Threads (Lock) 224000ms x 746 That’s almost 4 minutes!!!
  14. 14. Disruptor
  15. 15. Building Blocks • RingBuffer • EventFactory • WaitStrategy • Publisher • EventTranslator • EventProcessor • Sequence & SequenceBarrier
  16. 16. • RingBuffer • EventFactory • WaitStrategy • Publisher • EventTranslator • EventProcessor • Sequence & SequenceBarrier Building Blocks
  17. 17. • 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
  18. 18. • RingBuffer • EventFactory • WaitStrategy • Publisher • EventTranslator • EventProcessor • Sequence & SequenceBarrier Building Blocks
  19. 19. • SequenceBarrier • GatingSequence
  20. 20. • RingBuffer • EventFactory • WaitStrategy • Publisher • EventTranslator • EventProcessor • Sequence & SequenceBarrier Building Blocks
  21. 21. Single Writer Principle
  22. 22. Memory Barrier
  23. 23. Memory Barrier
  24. 24. 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
  25. 25. 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.
  26. 26. By Sticking to the Single Writer Principle we can use only volatile counters to synchronize between the producer and eventProcessor
  27. 27. What about Multiple Producers? Multiple Consumers?
  28. 28. Why queues suck? (part 2)
  29. 29. Zero Garbage Oh, and its all Mutable!!!
  30. 30. Why queues suck? (Part 3)
  31. 31. Batching Effect Without any contention cost during it
  32. 32. Going Parallel
  33. 33. 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
  34. 34. How Fast Is It - Throughput 0.0 7500000.0 15000000.0 22500000.0 30000000.0 Unicast Diamond ABQ Disruptor
  35. 35. 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)
  36. 36. Thanks for listening Questions? • https://github.com/LMAX-Exchange/disruptor • http://mechanical-sympathy.blogspot.co.uk/ • https://www.cs.umd.edu/~pugh/java/memoryModel/

×