Skip to main content

Exchange Engineering

· 2 min read
Hinny Tsang
Data Scientist @ Pollock Asset Management

Notes on YouTube video How to Build an Exchange by Jane Street.

Difficulties

  1. High transaction rates (Scale)
  2. Low, deterministic latency
  3. Fairness
  4. Reliability
  5. Durability
  6. 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

  1. FIFO
  2. TOP: people with best price and shares above threshold get priority.
  3. Pro-Rata
  4. Lead market maker

Low Latency Related

Real Contract Template

  1. Specific activity A
  2. When run as part of System S
  3. Throughtput X events / second
  4. Duration time T
  5. Hardware H, OS configuration O
  6. Latency L @ percentile P
  7. Coordinated ommission
  8. Timing policy

Microsecond Requiires

  1. Shared memory

    • Chronicle Queue: 50th %ile 0.5us
    • Aeron: 50th %ile 0.25us
    • Single trips in ~100ns
  2. Single threaded

    • No synchronization
    • No complicated object pooling strategies
    • Scale out by partitioning your data and adding more microservices
  3. Hog the CPU and CPI pin

  4. Record all inputs and output.

    • Reproduce bugs
    • Measure latency
    • Replay is especially important
  5. 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