© Software Engineering Musings - https://bhuwanupadhyay.dev/ - subscribe us on https://youtube.com/@semusings

System Design : Stock Exchange

© Software Engineering Musings - https://bhuwanupadhyay.dev/ - subscribe us on https://youtube.com/@semusings

Step 1—Design Scope

  • Q: supported securities: stocks
  • Q: business functions:
    • placing a new order
    • canceling an order
  • Q: supports trading after normal hours: no
  • Q: business scale requirements:
    • how many users: at least ten of thousands of users trading concurrently
    • how many symbols: at least 100 symbols
    • how many orders: billions of orders per day
  • Q: risk checks or constraints:
    • user can only trade a maximum of 1 million shares per day for a single symbol
  • non-functional requirements:
    • availability: 99.99%
    • fault tolerance:
      • no data loss and fast recovery mechanisms are needed to limit the impact of a production incident
    • latency:
      • round-trip latency should be at the millisecond level; focus on low 99th percentile latency
      • round-trip latency measured from the moment a market order enters the exchange to the point where the market order
        returns as filled execution
    • security:
      • should have an account management system and do KYC check to verify a user's before a new account is opened
      • prevent distributed denial of service (DDoS) attacks on public page containing market data
  • back-of-the-envelope calculations:
    • 100 symbols
    • 1 billion orders per day
    • the stock exchange operates for 6.5 hours per day (9:30 am to 4:00 pm) Monday to Friday
    • QPS = 1 billion orders / (6.5 hours * 3600 seconds) =~ 50,000 orders per second
    • peak QPS = 5 * QPS = 250,000 orders per second; trading volume is usually 5 times higher at the opening and
      closing hours
© Software Engineering Musings - https://bhuwanupadhyay.dev/ - subscribe us on https://youtube.com/@semusings

Step 2—High-Level Design