Top Banner
Inter-thread messaging with Disruptor Vladimir Iliev July 4 th , 2015
16

Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

Aug 08, 2015

Download

Documents

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: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

Inter-thread messaging

with Disruptor

Vladimir Iliev

July 4th, 2015

Page 2: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

Agenda

Problem definition1

Current mainstream solution2

Why it doesn’t work3

Why the Disruptor works better4

How much better?5

Q & A6

Page 3: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

What problem are we trying to solve

Retail financial exchange1

Low-latency2

High-throughput3

High availability4

Reliability5

Can’t lose data6

Page 4: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

4CONFIDENTIAL

A reasonable solution

Page 5: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

5CONFIDENTIAL

The problem with queues

• Queues get resized and objects get allocated and released

• Garbage collection pauses cause latency jitter

• Queue implementations do not take advantage of what CPUs have to offer in terms of

optimizing performance

A LOT OF GARBAGE

CPU UNFRIEDNLINESS

Page 6: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

6CONFIDENTIAL

Modern CPU architecture

Page 7: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

7CONFIDENTIAL

The cost of contention

• Atomic conditional updates to a single word

• Significantly more efficient than locks

• A lot harder to get right

CAS2

1

• Provide mutual exclusion and visibility of change

• Require arbitration by the OS kernel

• Caches lose hot data and instructions

Locks

3

• Indicate sections of code where read/write order is important

• Required in a multi-threaded environment for visibility

• In Java – the volatile keyword

Memory

barriers

Page 8: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

8CONFIDENTIAL

The cost of contention (cont’d)

Page 9: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

9CONFIDENTIAL

Why queues don’t get along with CPUs

How many writers to a single queue in a 1P1C scenario1

Either full or empty – head and tail often in the same cache line2

Linked-list backed queues are hard to pre-fetch3

Array-backed queues hard to resize4

Page 10: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

10CONFIDENTIAL

The Disruptor

Page 11: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

11CONFIDENTIAL

The Disruptor (cont’d)

Page 12: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

12CONFIDENTIAL

Throughput comparison vs. ABQ

Page 13: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

13CONFIDENTIAL

Latency comparison vs. ABQ

Page 14: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

14CONFIDENTIAL

Summary

Understanding how the underlying hardware works1

Aim for simplicity – single-writer principle, simple clean code2

Page 15: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

15CONFIDENTIAL

Thank you

Q & A

Page 16: Tech Talks_04.07.15_Session 4_Vladimir Iliev_Inter-thread Messaging With Disruptor

16CONFIDENTIAL

References

https://lmax-exchange.github.io/disruptor/1

http://disruptor.googlecode.com/files/Disruptor-1.0.pdf2

http://www.infoq.com/presentations/LMAX3

http://mechanical-sympathy.blogspot.com/4

https://www.youtube.com/watch?v=DCdGlxBbKU45

http://martinfowler.com/articles/lmax.html6