KatInterview

Build a Stock Trading Application Backend

System DesignModel answer
HardModel answerSystem Designtrading-systemsfinancial-servicesdistributed-systems

Last updated

Design the architecture for a consumer stock trading app handling order execution, account management, real-time market data, and settlement.

You need to architect a backend system for a consumer-facing stock trading application. Users should be able to fund their accounts, view live price quotes, place and cancel stock orders, and see their portfolio holdings and performance.

Your design should address:

  • Account management: User registration with identity verification, linking bank accounts, depositing and withdrawing funds, and tracking cash balances
  • Market data: Receiving price feeds from external sources and delivering current quotes to connected clients with low latency
  • Order handling: Accepting buy and sell orders (market orders, limit orders, and stop orders), validating that users have sufficient funds, preventing duplicate submissions, and managing order state through the lifecycle
  • Execution flow: Sending orders to external venues (exchanges or broker-dealers), receiving fill confirmations, and updating user portfolios
  • Portfolio tracking: Maintaining current holdings, calculating profit/loss, handling stock splits and dividends, and providing cost basis for tax reporting
  • Data storage: Choosing appropriate databases for orders, account balances, trade history, and price data
  • Service boundaries: Breaking the system into services with clear APIs, deciding between synchronous and asynchronous communication, and using message queues where appropriate
  • Correctness and consistency: Ensuring users cannot spend the same dollar twice, no trade confirmations are lost, and account balances always reconcile
  • Performance and scale: Handling peak load when markets open (30-50x normal traffic), managing millions of concurrent quote subscriptions, and dealing with backpressure
  • Reliability: Designing for high availability during trading hours, handling failure scenarios like downstream service outages, and implementing disaster recovery
  • Compliance and monitoring: Maintaining audit logs, detecting suspicious activity, tracking system health, and having fallback procedures

Estimate realistic traffic numbers and explain your architectural choices and tradeoffs. Consider how you would extend the design to support additional asset types later.

Hints

Hint 1

The order submission path and the market data streaming path have completely different performance characteristics—one is low-throughput and requires strong consistency, the other is high-throughput and can tolerate some data loss.

Hint 2

To prevent users from placing orders that exceed their buying power, you need atomicity between checking the balance and reserving funds. Think about transaction boundaries and database sharding strategies.

Hint 3

Orders go through many state transitions (submitted, routed, filled, cancelled). Event sourcing can help you maintain a complete audit trail while projecting the current state.