System Design AI

Design a Stock Exchange: Junior, Senior, and Staff Answers

A stock exchange is a classic system-design interview because it forces you to think about extreme scale, strict latency, and strong consistency. The interviewer wants to see whether you can break down a complex workflow, choose the right building blocks, and reason about trade-offs at different seniority levels.

What the interviewer is testing

  • Scalability – can you handle a burst of orders during market open?
  • Latency – matching engines must react in sub-millisecond time.
  • Consistency – every participant must see the same order book state.
  • Reliability – the platform must survive node failures without losing orders.
  • Operational thinking – monitoring, capacity planning, and cost awareness.

Core requirements and driving numbers

RequirementTypical value (hedged)
Peak order rate~100,000 orders per second (≈8.6 billion per day)
Latency target<1 ms for order matching, <10 ms for client response
Data durabilityAll orders must be persisted before acknowledgement
Availability99.999% (five-nine) uptime for the matching engine
Read-heavy workloadsMarket data feeds to millions of subscribers

These numbers guide every architectural decision. If you size for 100 k ops/s, you automatically cover lower loads and give headroom for spikes.

Request path, step by step

  1. Client submits an order – HTTP/HTTPS request hits an API gateway (Amazon API Gateway). The gateway forwards the payload to a thin microservice (order-ingest) via Amazon SQS for decoupling.
  2. Order validation – The ingest service checks syntax, user limits, and risk rules. Valid orders are written to DynamoDB (primary key = order_id) with a conditional write to guarantee idempotency.
  3. Publish to matching engine – A record of the new order is placed on a Kafka topic (Amazon MSK). The matching engine consumes the stream.
  4. Matching engine – Runs in a fleet of EC2 instances behind an Auto Scaling group. It holds the order book in memory (Redis via Amazon ElastiCache) for ultra-low latency. When a match is found, a trade event is emitted back to another Kafka topic.
  5. Trade persistence – Trade events are stored in Aurora PostgreSQL for relational queries and audit.
  6. Client notification – A Lambda function reads the trade topic and pushes confirmations via WebSocket (API Gateway) to the originating client. Market data feeds are fan-out from the trade topic to a CloudFront distribution serving static JSON/ProtoBuf snapshots.
  7. Metrics & alerts – CloudWatch collects latency, queue depth, and EC2 health. Alarms trigger SNS notifications.

Data model snapshot

  • Orders (DynamoDB): order_id (PK), user_id, side, price, quantity, timestamp, status.
  • Order book (ElastiCache): two sorted sets per symbol – bids (price desc) and asks (price asc). Each entry stores order_id and remaining quantity.
  • Trades (Aurora): trade_id (PK), buy_order_id, sell_order_id, price, quantity, timestamp.

Key architectural choice

The core decision is where to keep the order book. We chose an in-memory sorted set in ElastiCache because:

  • Sub-millisecond reads/writes are required for matching.
  • The data is transient; durability is handled by persisting orders/trades separately.

Alternatives like DynamoDB alone would add several milliseconds per operation, too slow for a sub-millisecond matching budget. Real exchanges go one step further and keep the book in the matching engine's own memory, recovering it from a replicated log; a managed cache trades some latency for simpler operations.

What a complete junior answer covers

  • High-level flow (client → API gateway → ingest → matching → confirmation).
  • Simple persistence (store orders in DynamoDB, trades in Aurora).
  • Mention of scaling the ingest service with an Auto Scaling group.
  • Basic latency target and rough capacity estimate.
  • One diagram showing the components.

What a senior answer adds

  • Ordering guarantees – use Kafka partitions per symbol to preserve order of events.
  • Cold-start mitigation – warm the ElastiCache nodes with a snapshot of the order book from S3 on startup.
  • Back-pressure handling – SQS dead-letter queues for malformed orders, throttling at the API gateway.
  • Shard the matching engine – each EC2 instance handles a subset of symbols; use a consistent hash to route orders.
  • Metrics – detailed latency breakdown (ingest → match, match → trade, trade → client) and a dashboard in CloudWatch.
  • Security – IAM roles for each service, TLS for client connections.

What a staff answer adds

  • Failure modes – discuss loss of a cache node (automatic replication in ElastiCache), Kafka broker failure (MSK multi-AZ), and network partition. Explain how the system falls back to DynamoDB for order replay.
  • Operational concerns – capacity planning (e.g., 100 k ops/s → 8.6 billion/day → ~100 TB of order data per year, Aurora storage scaling), backup strategy (Aurora continuous backup, DynamoDB point-in-time recovery), and disaster recovery (cross-region replication of S3 snapshots and Kafka logs).
  • Cost trade-offs – compare on-demand EC2 vs. Spot fleet for matching engines, the price impact of provisioned vs. burstable throughput in DynamoDB, and the savings from using S3-based market data archives.
  • Defending a design – articulate why sub-millisecond latency outweighs the extra operational complexity of an in-memory cache, and why Kafka is preferred over SQS for ordered streams.
  • Compliance – mention audit logging to CloudTrail and data retention policies.

Common mistakes that sink the interview

MistakeWhy it hurts
Ignoring latency requirementsMatching engines need <1 ms; using only relational DBs adds unacceptable delay.
Over-engineering the UI layerInterview focus is on the core matching engine, not on front-end polish.
Forgetting durabilityOrders must be persisted before ACK; otherwise you lose trades on a crash.
No clear scaling planWithout sharding or auto-scaling, the design cannot handle market-open spikes.
Not addressing ordering guaranteesOut-of-order matching leads to inconsistent books and violates exchange rules.

Level comparison table

AspectJuniorSeniorStaff
Core flowBasic request-response diagramAdds Kafka ordering, sharded matching, detailed metricsFull fault-tolerance, ops, cost analysis
Data storeDynamoDB for orders, Aurora for tradesAdds ElastiCache for order book, S3 snapshotsMulti-AZ replication, point-in-time recovery, cross-region DR
ScalingAuto Scaling group for ingestSymbol sharding, partitioned Kafka, warm cacheSpot fleet vs. on-demand, capacity planning, cost optimization
ReliabilitySimple retries, dead-letter queueReplication in ElastiCache, Kafka durabilityFailure mode analysis, automated failover, SLA modeling
Operational depthBasic CloudWatch alarmsDetailed dashboards, latency breakdownsFull runbooks, backup/restore procedures, compliance

How to practice this

  1. Study a related worked design in the System Design AI library, the stock trading app, which approaches orders from the brokerage side. Click each component to read its notes.
  2. Draft your own answer on the canvas. Start with the junior flow, then layer senior and staff details.
  3. Use the rubric to self-evaluate: check that you covered latency, ordering, durability, scaling, and operational concerns for each level.

Questions people ask

What is the typical throughput of a modern stock exchange?

Roughly 100 k orders per second at peak, which translates to about 8.6 billion orders per day. Designs must handle this burst without queuing delays.

Why use an in-memory cache for the order book instead of a database?

Matching needs sub-millisecond latency. In-memory structures (Redis sorted sets) give O(log n) updates and reads, whereas a durable networked database typically adds a few milliseconds per operation.

Can I replace Kafka with SQS for the matching engine?

Standard SQS queues give at-least-once delivery with only best-effort ordering. FIFO queues do keep order within a message group, but a consumed message is gone: there is no log to replay, which a deterministic order book relies on for recovery. Kafka (MSK) preserves order per partition and supports replay, making it the safer choice.

How do you ensure durability if the cache crashes?

All incoming orders are written to DynamoDB before they reach the matching engine. If the cache is lost, the engine can reconstruct the book by replaying the persisted orders from DynamoDB or from a recent S3 snapshot.