2. Agenda
• Introduction
• What is LMAX?
• Problems they were trying to solve
• Concurrent Programming
• Locks
• Queues
• The Disruptor Pattern
• LMAX Architecture
• Live Demo
• Q&A
3. LMAX
• London Multi-Asset eXchange
• Build the fastest trading platform in the world
• Retail platform, it allows anyone to trade without
going through a broker
• They have an order matching engine, real-time risk
management, in-memory transaction processing
system, etc.
4. Problems to Solve
• Extreme transaction processing (XTP)
• High-throughput, low-latency / predictable
latency
• Scale to potentially millions of users
5. They tried…
• RDBMS
• JEE
• SEDA
• Actors
… And none of the above offered the predictable low
latency they were after
6. Concurrent Programming
• Protect access to contended resources (mutual
exclusion)
• Make the results public in the right order
(visibility of changes)
• Locks (standard synchronized blocks in Java)
• Atomic / CAS Instructions – boils down to
machine instructions
7. Locks? No…
• Context switch to the OS kernel for arbitration
(even worse in a virtualized environment).
• It’s taking your thread quantum away and might
decide to do something else…
• When your thread is scheduled to run again it
might end up on another core and it will have to
reload all the execution context.
8. Queues? No…
• Either full or empty, mostly empty in a well
running system, cannot resize easily
• 1 Producer, 1 Consumer… 2 threads writing into
the queue
• High contention for head and tail
• Create garbage (put and take) so GC has a lot of
cleanup to do, impacts latency
11. In production…
• 6 million TPS (3Ghz dual-socket quad core
Intel on a Dell Server 32 Gb of RAM) in
2011…
• Single threaded, in memory
• 20 million items on the input ring buffer
• 4 million items the output ring buffer
13. Key Takeaways
• Mechanical Sympathy
• Keep the working sets in memory
• Write clean compact code
• Invest in modeling your domain (SRP – one
class one thing, one method one thing, etc.)
• Take the right approach to concurrency