Design Stock Market Application (with concurrent orders, threads, heap, brokers, sync)

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-05-07

Problem Statement

Designing a stock market trading system that allows users to place buy/sell orders in real time, matches them efficiently, and executes trades with support for brokers, order books, and concurrency. The system must be scalable, highly available, and thread-safe, as multiple users and brokers interact simultaneously.


🔶 Functional Requirements


1.User & Broker Management
•Users register through brokers.
•Brokers manage portfolios, commissions, and transactions.
•Each user can hold multiple stocks in their portfolio.
2.Order Management
•Support Buy Orders and Sell Orders with price and quantity.
•Support Market Orders (execute immediately at best available price) and Limit Orders (execute only at given price or better).
•Orders can be placed, updated, or cancelled.
3.Order Matching Engine
•Maintain an Order Book per stock (Buy side, Sell side).
•Match orders based on price-time priority.
•Use heaps/priority queues for efficient min/max order retrieval.
4.Trade Execution
•When a match is found, execute the trade.
•Update both buyer and seller portfolios.
•Record transaction history.
5.Concurrency & Synchronization
•Multiple users/brokers can place orders concurrently.
•Ensure thread safety for shared order book updates.
•Support locking/synchronization to avoid race conditions.
6.Market Data & Notifications
•Provide real-time stock prices, order book depth, and executed trades.
•Notify users/brokers about order execution, partial fills, or cancellations.


🔶 Non-Functional Requirements


•Scalability: Must handle millions of orders per second.
•Low Latency: Order matching and execution < 10ms.
•Thread Safety: Concurrent order handling without race conditions.
•Fault Tolerance: Survive broker/server crashes without data loss.
•High Availability: 99.99% uptime.
•Durability: Completed trades must be persisted reliably.


🔶 Constraints


•Read-heavy + Write-heavy mix: Continuous real-time reads of prices + high-frequency order placement.
•Memory-bound Order Books: Must fit live order book in memory for speed.
•Concurrency Handling: Multiple threads must update the same stock’s order book safely.
•Fairness: Orders must follow strict price-time priority without bias.


🔶 Example Use Cases


1.User A places a buy order for 100 shares of TCS at ₹3500 → goes into Buy Order Book.
2.User B places a sell order for 100 shares of TCS at ₹3490 → immediately matched and executed (buyer pays ₹3490 per share).
3.Multiple users place concurrent orders for Reliance stock → order book updates correctly with synchronization.
4.User places a market order to sell → matched instantly against best available buy order.
5.Broker system crash → trades and order books recover from persisted logs.


👉 Summary:

"Design a Stock Market Trading Application that supports concurrent buy/sell orders, maintains synchronized order books using thread-safe data structures (heaps/priority queues), ensures low-latency trade execution, integrates with brokers, and guarantees durability, scalability, and fairness under heavy load."

Solution

Comments