Design a Stock Exchange Matching Engine

System Design
Hard
Stripe
85.9K views

Design a system that matches buy and sell orders in a financial market. Focus on latency, concurrency, and maintaining an order book (priority queue/heap).

Why Interviewers Ask This

Interviewers at Stripe ask this to evaluate your ability to design low-latency, high-concurrency systems under strict consistency constraints. They specifically test your grasp of data structures like heaps for order matching, your understanding of race conditions in distributed ledgers, and your capacity to prioritize performance metrics critical to financial infrastructure.

How to Answer This Question

1. Clarify Requirements: Define the scope (e.g., limit orders vs. market orders), latency targets (microseconds), and consistency needs (strong vs. eventual). 2. High-Level Architecture: Propose a single-threaded or lock-free core engine to minimize context switching, surrounded by API gateways for scaling read/write traffic. 3. Data Structure Design: Detail the Order Book implementation using a priority queue (max-heap for asks, min-heap for bids) sorted by price and time. 4. Matching Logic: Explain the algorithm for crossing orders, handling partial fills, and updating the book atomically. 5. Concurrency & Scalability: Discuss strategies like partitioning by asset class or using sharding with consistent hashing to handle massive throughput without sacrificing speed.

Key Points to Cover

  • Prioritizing single-threaded or lock-free designs to eliminate race conditions and reduce latency
  • Correctly implementing Price-Time priority using dual heaps for the order book
  • Defining clear sharding strategies based on asset symbols for horizontal scalability
  • Addressing the atomic nature of trade execution and partial fill scenarios
  • Emphasizing audit trails and data consistency suitable for financial applications

Sample Answer

To design a stock exchange matching engine, I first clarify that we need sub-millisecond latency and strong consistency for trade execution. For the core architecture, I would avoid multi-threaded locks on the order book…

Common Mistakes to Avoid

  • Suggesting heavy database locking for the active order book, which creates unacceptable bottlenecks
  • Ignoring the Price-Time priority rule, which is the fundamental standard for fair trading
  • Over-engineering with complex distributed consensus algorithms before solving the core matching logic
  • Failing to address how to handle edge cases like market crashes or extreme volume spikes

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 190 System Design questionsBrowse all 57 Stripe questions