Design Online Stock Brokerage System
| Difficulty: Hard | Source: awesome-low-level-design |
Requirements
- Create/manage trading accounts
- Buy/sell stocks, view portfolio and history
- Real-time stock quotes
- Order placement, execution, settlement
- Business rules: check balances, stock availability
- Concurrent access
- Scalable
- Secure
Class Diagram

Classes, Interfaces and Enumerations
| Class/Interface | Description |
|---|---|
| User | User id, name, email |
| Account | Account id, user, balance; deposit/withdraw |
| Stock | Symbol, name, price; update price |
| Order | Abstract class; order id, account, stock, quantity, price, status; abstract execute() |
| BuyOrder | Concrete subclass of Order; executes buy logic |
| SellOrder | Concrete subclass of Order; executes sell logic |
| OrderStatus | Enum: PENDING, EXECUTED, REJECTED |
| Portfolio | Stocks owned; add/remove stocks |
| StockBroker | Singleton; manages accounts, stocks, order processing |
| InsufficientFundsException | Custom exception for insufficient account balance |
| InsufficientStockException | Custom exception for insufficient stock holdings |
Design Patterns Used
| Pattern | Application |
|---|---|
| Singleton | StockBroker provides centralized management of accounts, stocks, and order processing |
| Abstract Class/Polymorphism | Order is abstract with BuyOrder and SellOrder subclasses, each implementing their own execute() logic |
Code Implementations
| Language | Source Code |
|---|---|
| Java | View on GitHub |
| Python | View on GitHub |
| C++ | View on GitHub |
| C# | View on GitHub |
| Go | View on GitHub |