Exchange Engineering
· 2 min read
Notes on YouTube video How to Build an Exchange by Jane Street.
Difficulties
- High transaction rates (Scale)
- Low, deterministic latency
- Fairness
- Reliability
- Durability
- Robustness to bad clients
Speaker built a crossing engine to handle messages of 500k/sec with ~1ms latency.
Simple Design
flowchart TD
MatchEngine[Match Engine] --> Port1[Port 1]
MatchEngine[Match Engine] --> Port2[Port 2]
MatchEngine[Match Engine] --> Port3[Port 3]
Port1 --> Cloud((Somethings))
Port2 --> Cloud
Port3 --> Cloud
Cloud --> Client[Client 1]
Cloud --> Client2[Client 2]
Cloud --> Client3[Client 3]
Boardcast by udp to ensure low latency. However, udp is unreliable and can drop packets.
Hence, we add a lager of resenter (RT).
- Everyone gets every message
- One-in-flight, latency determines throughput
Matching algorithm
- FIFO
- TOP: people with best price and shares above threshold get priority.
- Pro-Rata
- Lead market maker
Low Latency Related
Real Contract Template
- Specific activity A
- When run as part of System S
- Throughtput X events / second
- Duration time T
- Hardware H, OS configuration O
- Latency L @ percentile P
- Coordinated ommission
- Timing policy
Microsecond Requiires
-
Shared memory
- Chronicle Queue: 50th %ile 0.5us
- Aeron: 50th %ile 0.25us
- Single trips in ~100ns
-
Single threaded
- No synchronization
- No complicated object pooling strategies
- Scale out by partitioning your data and adding more microservices
-
Hog the CPU and CPI pin
-
Record all inputs and output.
- Reproduce bugs
- Measure latency
- Replay is especially important
-
Map your microservices onto your hardware.
Hardest areas
-
Wait free (off heap) data structures, you know the cpu cycles required
-
Resilience / High Availability
-
Failover / hot-hot
