📈Stock Broker System Design - Groww
Last updated
Last updated
lets first try to understand what stock broker is ?
Stock brokers are authorized members of the stock exchanges (i.e. BSE, NSE).
They execute trades on behalf of the customers at the exchange. There are 100's of registered stock brokers in India offering stock trading to retail customers. At high-level, the most famous stock brokers in India are categories in 2 types:
Full-services brokers
Discount Stock Brokers
Full-service brokers offer a wide range of services including stock trading, research and advisory and local support. Discount stock brokers are online brokers specialized only in stock and commodity trading services. They do not offer add-ons like research and advisory, PMS, wealth management and branch support.
The most popular stock brokers in the full-service category include ICICI Direct, Sharekhan, HDFC Securities, Sharekhan, Angel One and Axis Bank. The most famous stock brokers in discount broker category include Zerodha, 5paisa, Groww and upstox.
We can't read system design for all the brokers but lets consider one by more number of clients .
Groww
9.19
Zerodha
7.22
Angel One
5.98
Upstox
2.4
Price Changes are frequent in fractions of seconds.
Buy / Sell Order placement
Portfolio Management
Trade history & Reporting
Notification & alerts
Order Matching Engine
High availability and reliability.
Low latency for real-time transactions.
Scalability to handle high volumes of trade requests.
Security to protect user data and transactions.
Compliance with financial regulations.
Considering the system like groww, it is good to have each microservices for single major functionality. There will be huge data flow in and out. Failures in one service do not necessarily impact others, improving system reliability. Having decoupling between the service dependencies, allows more flexibility and resilient architecture.
Below are the list of services required considering the above functionality. I am dividing the functionality into three categories. ( Core, Infra, Optional )
Core: These are the microservices specific to the grow as a company or product.
Market Service
Provides real-time market data to users
Order Service
Handles the creation, modification, and cancellation of buy/sell orders
Portfolio Service
Manages users’ portfolios and investments
Trade History Service
Stores and retrieves historical trade data
Trade Execution Service
Executes buy/sell orders by communicating with stock exchanges like NSE / BSE
Order Matching Engine Service
Matches buy and sell orders within the system. This is required for internal order processing and execution efficiency.
Price Update Service
Handles frequent price updates and broadcasts them to users
Real-time Data Processing Service
Processes real-time data streams for market data and order execution
Infra: These are the services required to run any kind of product. These services will be helpful when company / startup need for diversion from the existing path.
Identity Service
Manages user profiles, authentication, and authorization
Logging and Monitoring Service
Logs system activities and monitors system health
Payment Service
Manages financial transactions and settlements
API Gateway Service
Provides a unified entry point for client applications
Optional: These services are good to have for a system. Cover these services only if time permits in the interview.
Audit, Compliance Service
Ensures the system complies with regulatory requirements
Customer Support Service
Manages customer support requests and issues
User Preferences Service
Stores and manages user preferences and settings
Fraud Detection Service
Detects and prevents fraudulent activities
Let's assume different percentages of active users for each microservice, considering typical usage patterns.
Order Service: 30% of active clients.
Portfolio Service: 20% of active clients.
Trade History Service: 10% of active clients.
Trade Execution Service: 10% of active clients.
Order Matching Engine: Tied to order service, hence 30% of active clients.
Price Update Service: 10% of active clients, with updates for 5 companies each.
Market Data Service: 100% of companies (7,800).
As of March 2024, Companies registered are as follows
BSE: 5421
NSE: 2379
Total companies are around: 7800
Note: There can be companies listed in both. We are not considering this corner case.
Total Users ( Groww )
9.19 million
Registered Companies ( NSE and BSE )
7800
Trading hours in a day
9:15 AM to 3:30 PM (22,500 seconds)
Trading days in the month
22
API Call Average Request/Response Size
2 KB including headers
Average Request Processing Time
50 ms
Max Requests per Server
1000
Max Websockets per Server
20,000
Assumptions
Company Updates Per second
5 updates per company per second
Data per Update
0.1 KB (100 bytes)
Total Companies
7,800
Websocket Update Required ?
No
Storage Calculation
Total Updates per Trading Day: 7800 * 22500 = 175,500,000 Updates / day
Data per Trading Day: 175,500,000 * 0.5 KB = 87.75 GB / day
Total data per month (Assuming 22 days of trading): 16.73 GB * 22 = 1930.5 GB / Month
Servers Calculation
Total Updates per Second: 7,800 companies * 5 updates/sec = 39,000 updates/sec
Number of API Servers: 39,000 / 1000 = 39 servers
Assumptions
Orders per User per Day
2 orders
Data per Order
2 KB
Active Users
30% of 919,000 = 275,700
Storage Calculation
Total Orders per Trading Day: 9.19 m * 2 = 18.38 million orders per day
Data per Trading Day: 18.38 * 2 = 36.76 GB / day
Total Data per month: 18.38 * 22 = 808.72 GB / month
Server Calculation
API Requests per second = 275,700 / 0.05 = 5,514,000 requests/sec
Number of API Servers = 5,514,000 / 1000 = 5514 servers
Number of Websocket Servers = 275,700 / 20,000 = 13.785 = 14 ( Approx)
Assumptions
Updates per User per Day
5
Data per update
1 KB
Active Users
20% of 919,000 = 183,800
Storage Calculation
Total portfolio updates per Trading Day: 9.19 m * 5= 45.94 million updates / day
Data per Trading Day: 45.95 m * 1 KB = 45.95 GB / day
Total data per month (Assuming 22 days of trading): 45.95 GB * 22 = 1,010.9 GB / Month
Server Calculation
API Requests per second = 183,800 / 0.05 = 3,676,000 requests/sec
Number of API Servers = 3,676,000 / 1000 = 3676 servers
Assumptions
Trades per User per Day
2
Data per Trade
2 KB
Active Users
10% of 919,000 = 91,900
Storage Calculations:
Total trades per Trading Day: 9.19 m * 2 = 18.38 million updates / day
Data per Trading Day: 18.38m * 2 KB = 36.76 GB / day
Total data per month (Assuming 22 days of trading): 36.76 GB * 22 = 808.72 GB / Month
Server Calculations:
API Requests per second = 91,900 / 0.05 = 1,838,000 requests/sec
Number of API Servers = 1,838,000 / 1000 = 1838 servers
Number Of Websocket Servers = 1,838,000 / 20,000 = 91.9 = 92 Servers
Assumptions
Price Updates per second
1000
Data per Update
0.1 KB
Number of price updates per user
5
Number of active Users
10% of 919,000 = 91,900
Storage Calculations
Total updates per Trading Day: 1000 * 22,500 = 22,500,000 updates / day
Data per Trading Day: 22,500,000 * 0.1 KB = 22,500 KB / day = 2.25 GB / day
Total data per month (Assuming 22 days of trading): 2.25 GB * 22 = 49.5 GB / Month
Server Calculations
Total Price Updates: 91,900 * 5 = 459,500 updates/sec
API Requests per second = 459,500 / 0.05 = 9,190,000 requests/sec
Number of API Servers = 9,190,000 / 1000 = 9190 servers
Number of Websocker servers = 9,190,000 / 20,000 = 459.5 = 460 ( Approx)
Storage Calculations
Market Data Updates: 39,000 updates/sec
Updates Stored per Second: 1% of 39,000 updates/sec = 390 updates/sec
Total Data Size per Second: 390 * 0.01 = 3.9 KB/sec
Total Data Size per Day: 3.90 KB/sec * 22,500 = 87.75 MB /day
Total Data Size per Month: 87.75 * 22 = 1.93 GB /month
Server Calculations
Market Data Ingestion:
Active Companies: 7,800
Market Data Updates: Assume 5 updates per second per company
Total Updates per Second: 7800 * 5 = 39,000 updates/sec
Data Aggregation and Transformation:
Assume each update requires 1 additional processing step
Total Processing Steps per Second: 39,000
Event Detection:
Assume 10% of updates trigger events
Event Detection Steps per Second: 0.1 * 39,000 = 3,900
Streaming Updates:
Assume each update is sent to 30% of active users (30% of 919,000)
Active Users Receiving Updates: 0.3 * 919,000 = 275,700
Updates per User per Second: 2 (assume each user receives updates for 2 companies)
Total Streaming Updates per Second: 275,700 * 2 = 551,400 updates/sec
Data Storage:
Assume 1% of updates are stored for historical analysis
Data Storage Steps per Second: 0.01 * 39,000 = 390
Total Updates:
39,000 + 39,000 + 3,900 + 390 = 82,290 updates/sec
Number of API servers: 82,290 / 1000 = 82.2 = 83 ( Approx )
Number of Websocket Servers: 551,400 / 20, 000 = 28 Servers
Total Storage Required = 6227.71 GB / Month
Total API servers: 27, 692 per second
Total Websocket servers: 608 per second
https://www.chittorgarh.com/report/top_20_share_brokers_in_india_by_clients_at_nse/1/ https://www.statista.com/statistics/1056499/india-leading-equity-brokerage-firms-by-active-customers/ https://www.nseindia.com/regulations/listing-compliance/nse-market-capitalisation-all-companies https://blog.shoonya.com/how-many-companies-are-listed-in-nse-and-bse/#:~:text=There%20are%20a%20total%20of%205%2C309%20listed%20companies,DVRs%2C%20REITs%2C%20InVITs%2C%20ETFs%2C%20and%20Partly%20Paid%20Shares.https://cloud.google.com/customers/groww#:~:text=Groww%3A%20Empowering%20investors%20with%20a,platform%20powered%20by%20Google%20Clouhttps://stackshare.io/groww/groww https://tech.groww.in/