.

55
Virtual Queues as a Trade Processing Pattern Uri Cohen @uri1803 | github.com/uric Head of Product @ GigaSpaces

Transcript of .

Page 1: .

Virtual Queues as a Trade Processing Pattern

Uri Cohen @uri1803 | github.com/uric

Head of Product @ GigaSpaces

Page 2: .

Event Processing at Massive Scale Approaches to Concurrency

Uri Cohen @uri1803 | github.com/uric

Head of Product @ GigaSpaces

Page 3: .

This is What It Used to Be Like

Page 4: .

That’s What

It’s Like Now

Page 5: .

Some Numbers

15 Billion Trades / Day on

NYSE alone

http://www.nytimes.com/2011/08/27/business/as-trade-volumes-soar-exchanges-cash-in.html

Page 6: .

Some Numbers

That’s

641K Trades / Second

http://www.nytimes.com/2011/08/27/business/as-trade-volumes-soar-exchanges-cash-in.html

Page 7: .

Some Numbers

12 Billion Shares change

hands every day

http://www.bloomberg.com/news/2012-01-23/stock-trading-is-lowest-in-u-s-since-2008.html

Page 8: .

Some Numbers

$4 Million The cost of 1

millisecond of latency to a broker

http://www.tabbgroup.com/PublicationDetail.aspx?PublicationID=346

Page 9: .

The Problem

Massive stream of events

Time is money, literally

Can’t lose a single message

Fairness is a must

Page 10: .

Order Book -

Simplistic Example

Buy Sell 50, $12 60, $10

60, $11 100, $11

30, $10 30, $12

Page 11: .

Buy Sell 50, $12 60, $10

60, $11 100, $11

30, $10 30, $12

Price: $10

Order Book -

Simplistic Example

Page 12: .

Buy Sell 60, $11 10, $10

30, $10 100, $11

30, $12

Price: $10

Order Book -

Simplistic Example

Page 13: .

Buy Sell 60, $11 10, $10

30, $10 100, $11

30, $12

Price: $10

Order Book -

Simplistic Example

Page 14: .

Buy Sell 50, $11 100, $11

30, $10 30, $12

Price: $10

Order Book -

Simplistic Example

Page 15: .

Buy Sell 50, $11 100, $11

30, $10 30, $12

Price: $11

Order Book -

Simplistic Example

Page 16: .

Buy Sell 30, $10 50, $11

30, $12

Price: $11

Order Book -

Simplistic Example

Page 17: .

What it Really Means

Low latencyIn memory, GC tuning

Scalability Multi-coreMulti-node

OrderingBy price, order time

Exclusivity

Resiliency

Page 18: .

Trading is Just One Use Case

All things FCFS, with a limited stock

Flight booking

Betting

Online Auctions

Cloud Spot Instances

eCommerce

Page 19: .

Let’s Talk Solutions

Page 20: .

Queue (SEDA/Actor Style)

Not Validated Validated Processed

Validator Processor

Page 21: .

Queue (SEDA /Actor Style)

The Good: Ordered (Is it fair?)Multi-threaded

The Bad: Not very scalable

LockingContext

switchingTransient

Page 22: .

The Cost of Locking

Method Time in msec

Single Thread 300

Single Thread w/ Lock 10.000

2 Threads w/ Lock 224.000

Single Thread w/ CAS 5.700

2 Threads w/ CAS 30.000

Single Thread w/ Volatile Write 4.700

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

Page 23: .

Queue (Lack of) Fairness

Consumer Thread 1

Consumer Thread 2

60

50 Buy Sell 100

Page 24: .

Queue (Lack of) Fairness

Consumer Thread 1

Consumer Thread 2

50

60

Buy Sell 100

Page 25: .

Queue (Lack of) Fairness

Consumer Thread 1

Consumer Thread 2

Buy Sell 100

60 50

Can you tell which order will be executed 1st?

Page 26: .

Single-Threaded Queue

Validator Processor

Page 27: .

Single- Threaded

Queue

The Good: Fast, very fast

No contentionNo context

switchesAlways fair

The Bad: Multi-core?

Not fit for Intense compute & I/O

Need to be async.Transient

Page 28: .

Single- Threaded

Queue

They do it…

Page 29: .

Disruptor (LMAX)

Page 30: .

Segmented Queue

Symbol=A-H Symbol=I-S Symbol=T-Z

Validator Processor Processor

Processor thread pool per segment

Page 31: .

Segmented Queue - Optimization Single Processor thread pool, pick random segment

Symbol=A-H Symbol=I-S Symbol=T-Z

Processor

Page 32: .

Segmented Queue

The Good: Scalable

But segments can get hot

Minimizes contention

The Bad: Not trivial to

implementStill unfair

Is total ordering needed?

Transient

Page 33: .

What about

Fairness?

Exclusivity is Key Process one message for each segment at the same time

No exclusivity across segments

Page 34: .

Implicit ExclusivitySingle processor thread per segment

Symbol=A-H Symbol=I-S Symbol=T-Z

Processor Processor Processor

Page 35: .

Explicit ExclusivityShared thread pool, mark segments under processing (CAS)

Segment 1 Segment 2 Segment 3

Processor

Segment 1

Segment 2

Segment 3

Page 36: .

Explicit ExclusivityShared thread pool, mark segments under processing (CAS)

Segment 1 Segment 2 Segment 3

Processor

Segment 1 X

Segment 2

Segment 3

Page 37: .

Explicit ExclusivityShared thread pool, mark segments under processing (CAS)

Segment 1 Segment 2 Segment 3

Processor

Segment 1 X

Segment 2

Segment 3 X

Page 38: .

Explicit ExclusivityShared thread pool, mark segments under processing (CAS)

Segment 1 Segment 2 Segment 3

Processor

Segment 1

Segment 2

Segment 3 X

Page 39: .

Explicit Exclusivity

Num. of segments is keyToo few: little concurrencyToo many: wasting memory

Page 40: .

Dynamic Segmentation Segments are created and removed as needed

Processor

Page 41: .

Dynamic Segmentation Segments are created and removed as needed

“GOOG”

Processor

“GOOG”

Page 42: .

Dynamic Segmentation Segments are created and removed as needed

“GOOG”

Processor

“GOOG”

Page 43: .

Dynamic Segmentation Segments are created and removed as needed

“GOOG”

Processor

GOOG

AAPL

“AAPL”

Page 44: .

Dynamic Segmentation Segments are created and removed as needed

“GOOG”

Processor

GOOG X

AAPL

AMZN

“AAPL” “AMZN”

Page 45: .

Dynamic Segmentation

Segments created as needed

Randomize on segments until available one found

Fast, scalable, fair

We call it “FIFO groups” or “Virtual Queues”

Page 46: .

It Can (and Does) Get Much

More Complex

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

Page 47: .

A Bit about Usability

What you don’t want to do

Implement data structuresHandle concurrencyHandle HA Handle transactions

Page 48: .

A Bit about Usability

What you want to control

Event flow Grouping attribute (e.g

symbol)Event handlers

Page 49: .

Data Grid as a

Foundation

Transactional

Highly available

Supports complex matching

Page 50: .

How We Thought

of It

Page 51: .

How We Thought

of It

Page 52: .

How We Thought

of It

Page 53: .

How We Thought

of It

Page 54: .

How We Thought

of It