KatInterview

Cross-Venue Arbitrage Detector for a Million Quotes a Second

System DesignModel answer
MediumModel answerSystem Designlow-latencylock-freespsc-ring-buffer

Last updated

Design an in-process, lock-free engine that tracks the best bid and ask across up to 16 venues for 10,000 instruments and flags crossed markets within microseconds.

Problem

A proprietary trading desk consumes top-of-book quotes from several stock exchanges. Each venue publishes, per instrument, its current best bid and best ask. If one venue's bid is higher than a different venue's ask for the same instrument, the market is crossed. The desk can buy on the cheap venue, sell on the expensive one, and lock in the difference. Your task is to design the in-process component that spots this condition and signals the execution layer.

Each incoming message looks like:

QuoteUpdate(instrument_id, venue_id, bid_price, bid_size, ask_price, ask_size, timestamp_ns)

For every instrument, the component must:

  1. Keep the latest quote from each venue.
  2. Continuously know the highest bid and lowest ask across venues.
  3. When the highest bid exceeds the lowest ask and they come from different venues, emit an ArbitrageOpportunity containing the buy venue, sell venue, spread, and the maximum quantity that can be executed.

Constraints

  • Sustained load of 1,000,000 updates/s, with bursts up to 3,000,000 updates/s.
  • P99 latency below 5 microseconds from message arrival to signal emission.
  • No dynamic heap allocation on the critical path.
  • Up to 10,000 instruments and 16 venues.
  • Multiple venue network threads deliver updates concurrently, so the design must be correct under concurrency.

What to cover in your answer

  1. Per-instrument and per-venue data layout: preallocation, alignment, and how you avoid cache-line contention.
  2. The threading model. Compare shared state with locks, lock-free shared state, and pinned single-writer shards fed by ring buffers.
  3. The algorithm for updating the extrema when a quote improves the top or worsens the current leader.
  4. Failure handling: stale venues, sequence gaps or reordering, full queues, and the trade-offs between your chosen concurrency approach and the alternatives.

Hints

Hint 1

Do the arithmetic first: 1M/s is about 1 µs per update on one core, and 10,000 x 16 quote slots is only a few MB. The hard part is contention and cache misses, not algorithmic complexity.

Hint 2

If every instrument has exactly one thread that writes to it, you need no locks on the book at all. Think about how updates from many venue threads reach that owner thread without sharing mutable state.

Hint 3

With at most 16 venues, a full rescan of one cache line may be cheaper than any clever structure. Still, describe the cheap incremental rule: a new quote that beats the leader replaces it, and only a worsening of the current leader forces a rescan.

Hint 4

A crossed market where the best bid and best ask come from the same venue is bad data, not arbitrage. Also decide how you handle a venue that goes silent or skips sequence numbers.