In simple terms: When you place a ‘buy AAPL at $150’ order, the system needs to find a matching ‘sell AAPL at $150’ order. This must happen in microseconds, correctly, and never match the same order twice.
Designing a Stock Broker Platform (Robinhood / Zerodha)
Difficulty: Advanced Topics: Order Matching, Event Sourcing, CQRS, Kafka, Delivery Semantics Asked at: Robinhood, Zerodha, Groww, Upstox, Goldman Sachs · Prerequisites: Event Sourcing, Message Queues, and Idempotency
1. Understanding the Problem
A stock broker platform lets retail users buy and sell financial instruments (stocks, ETFs, options). The broker receives user orders, validates them against account balances and risk rules, routes them to an exchange (or internal matching engine), and shows real-time status updates. The system must handle thousands of concurrent orders during market hours with strong consistency guarantees - a lost or duplicate trade is a regulatory violation.
2. Naive First Cut
flowchart LR
Client["Mobile App"]
API["API Server"]
DB["Postgres DB"]
Exchange["Exchange NSE or BSE"]
Client --> API
API --> DB
API --> Exchange
New components:
- API Server: One process taking the order, checking the balance, and calling the exchange, all inside the user’s request.
- Postgres DB: Orders, balances and positions in one database, holding only each order’s current state.
- Exchange (NSE or BSE): The National and Bombay Stock Exchanges - the outside venues where an order actually meets a counterparty. Calling them synchronously is what makes this sketch fragile, because their latency becomes our latency.
How this breaks:
- Single API server can’t handle 50K orders/sec during market open
- Synchronous exchange call blocks the API - timeouts pile up
- No order queue - if exchange is slow, user retries create duplicates
- No way to notify users of fills without polling
- Single DB becomes write bottleneck during peak hours
- No audit trail - regulators require full event history of every order state change
The rest of the doc evolves this into a production-grade event-driven architecture.
3. Prior Art We’re Drawing From
- Zerodha Kite OMS - Silo-based user partitioning, pre-trade validation at the gateway, async order routing to exchange. Handles millions of orders daily on commodity hardware. (zerodha.tech blog)
💡 A silo is an independent group of users served by its own full copy of the stack, so a fault or a surge in one group cannot reach the others. OMS stands for order management system, the service that owns an order’s lifecycle. - LMAX Exchange Disruptor - Single-threaded matching engine processing 6M orders/sec using a lock-free ring buffer. Demonstrates that matching is CPU-bound, not I/O-bound.
💡 LMAX’s finding is counter-intuitive and this design leans on it throughout: one thread handling a symbol’s orders in order beats many threads coordinating, because the coordination costs more than the matching. A ring buffer is the fixed-size circular array it hands work through without taking locks. - Coinbase Matching Engine - Continuous first-come-first-serve order book with price-time priority. Publishes trade events via WebSocket feed. (Coinbase docs)
- Kafka + Flink for Exchange - Event-driven architecture where every state change is a Kafka event; Flink processes matching and settlement in real-time streams. (Medium article)
💡 Flink is a stream-processing engine: it reads an endless feed of events and keeps running state in memory, checkpointing it so a crash resumes rather than restarts. - Robinhood - Routes orders to market makers (payment-for-order-flow, or PFOF, model) rather than running own matching engine.
💡 Under payment for order flow the broker hands customer orders to a trading firm that pays for them, instead of matching the orders itself. It is worth knowing because it changes which half of this design you actually need to build. Emphasizes idempotent order submission and real-time push notifications.
4. Functional Requirements
Core (Top 3)
- Place and match buy/sell orders - submit limit/market orders, match based on price-time priority
💡 A market order says “buy now at whatever the going price is”; a limit order says “buy only at this price or better, and wait otherwise”. Price-time priority means the best price is filled first, and among orders at the same price the one that arrived first is filled first. These two rules are the whole of Deep Dive 1. - Show users their transactions - real-time order status, trade history, portfolio positions
- Send notifications - push notifications on order fills, partial fills, rejections, price alerts
Below the Line
- Watchlists and price charts (read-heavy, CDN-cacheable)
- Margin trading and short selling
- Options and derivatives
- Admin dashboard and compliance reporting
- Referral and rewards system
5. Non-Functional Requirements
Core
| Non-functional requirement (NFR) | Target |
|---|---|
| Latency | Order placement < 50ms P99 - the latency 99% of requests beat (broker side); matching < 5ms |
| Throughput | 50K orders/sec at market open |
| Consistency | Exactly-once order execution - no duplicates, no lost fills |
| Availability | 99.99% during market hours (09:15 - 15:30 IST) |
Discussion
Newest first