550 likes | 659 Views
Virtual Queues as a Trade Processing Pattern. Uri Cohen @uri1803 | github.com /uric Head of Product @ GigaSpaces. Event Processing at Massive Scale Approaches to Concurrency . Uri Cohen @uri1803 | github.com /uric Head of Product @ GigaSpaces. This is What It Used to Be Like.
E N D
Virtual Queues as a Trade Processing Pattern Uri Cohen @uri1803 | github.com/uric Head of Product @ GigaSpaces
Event Processing at Massive Scale Approaches to Concurrency Uri Cohen @uri1803 | github.com/uric Head of Product @ GigaSpaces
15 Billion Trades / Day on NYSE alone Some Numbers http://www.nytimes.com/2011/08/27/business/as-trade-volumes-soar-exchanges-cash-in.html
That’s 641K Trades / Second Some Numbers http://www.nytimes.com/2011/08/27/business/as-trade-volumes-soar-exchanges-cash-in.html
12 Billion Shares change hands every day Some Numbers http://www.bloomberg.com/news/2012-01-23/stock-trading-is-lowest-in-u-s-since-2008.html
$4 Million The cost of 1 millisecond of latency to a broker Some Numbers http://www.tabbgroup.com/PublicationDetail.aspx?PublicationID=346
Massive stream of events Time is money, literally Can’t lose a single message Fairness is a must The Problem
Price: $10 Order Book -Simplistic Example
Order Book -Simplistic Example Price: $10
Order Book -Simplistic Example Price: $10
Order Book -Simplistic Example Price: $10
Price: $11 Order Book -Simplistic Example
Order Book -Simplistic Example Price: $11
Low latencyIn memory, GC tuning Scalability Multi-core Multi-node Ordering By price, order time Exclusivity Resiliency What it Really Means
All things FCFS, with a limited stock Flight booking Betting Online Auctions Cloud Spot Instances eCommerce Trading is Just One Use Case
Queue (SEDA/Actor Style) Not Validated Validated Processed Validator Processor
The Good: Ordered (Is it fair?) Multi-threaded The Bad: Not very scalable Locking Context switching Transient Queue (SEDA /Actor Style)
The Cost of Locking http://disruptor.googlecode.com/files/Disruptor-1.0.pdf
Queue (Lack of) Fairness 50 60 Consumer Thread 1 Consumer Thread 2
Queue (Lack of) Fairness 50 60 Consumer Thread 1 Consumer Thread 2
Queue (Lack of) Fairness Can you tell which order will be executed 1st? 50 60 Consumer Thread 1 Consumer Thread 2
Single-Threaded Queue Validator Processor
The Good: Fast, very fastNo contention No context switches Always fair The Bad: Multi-core? Not fit for Intense compute & I/ONeed to be async. Transient Single- Threaded Queue
They do it… Single- Threaded Queue
Segmented Queue Processor thread pool per segment Symbol=A-H Symbol=I-S Symbol=T-Z Validator Processor Processor
Segmented Queue - Optimization Single Processor thread pool, pick random segment Symbol=A-H Symbol=I-S Symbol=T-Z Processor
The Good: ScalableBut segments can get hot Minimizes contention The Bad: Not trivial to implement Still unfairIs total ordering needed? Transient Segmented Queue
Exclusivity is Key Process one message for each segment at the same time No exclusivity across segments What about Fairness?
Implicit Exclusivity Single processor thread per segment Symbol=A-H Symbol=I-S Symbol=T-Z Processor Processor Processor
Explicit Exclusivity Shared thread pool, mark segments under processing (CAS) Segment 1 Segment 2 Segment 3 Processor
Explicit Exclusivity Shared thread pool, mark segments under processing (CAS) Segment 1 Segment 2 Segment 3 Processor
Explicit Exclusivity Shared thread pool, mark segments under processing (CAS) Segment 1 Segment 2 Segment 3 Processor
Explicit Exclusivity Shared thread pool, mark segments under processing (CAS) Segment 1 Segment 2 Segment 3 Processor
Num. of segments is keyToo few: little concurrency Too many: wasting memory Explicit Exclusivity
Dynamic Segmentation Segments are created and removed as needed Processor
Dynamic Segmentation Segments are created and removed as needed “GOOG” Processor
Dynamic Segmentation Segments are created and removed as needed “GOOG” Processor
Dynamic Segmentation Segments are created and removed as needed “GOOG” “AAPL” Processor
Dynamic Segmentation Segments are created and removed as needed “AMZN” “GOOG” “AAPL” Processor
Segments created as needed Randomize on segments until available one found Fast, scalable, fair We call it “FIFO groups” or “Virtual Queues” Dynamic Segmentation
Memory state can get corrupt on errors It’s not always as simple as “pop off the queue” limits, priorities, circuit breakers, etc. Resiliency is always a pain It Can (and Does) Get Much More Complex
What you don’t want to do Implement data structures Handle concurrency Handle HA Handle transactions A Bit about Usability
What you want to control Event flow Grouping attribute (e.g symbol) Event handlers A Bit about Usability
Transactional Highly available Supports complex matching Data Grid as a Foundation