System Design ( Day - 27 )

Manoj KumarManoj Kumar
4 min read

System Design of Stock Broker (Zerodha)

Requirements
1. WatchList at home
2. Stock → Hourly, daily, monthly, yearly etc… (Performance)
3. Market Depth (Buy or Sell) Stocks
4. Portfolio
5. Order book, Trade book

Deisgn
If you have used any of the stock broker applications like Zerodha, UpStocks, Grow etc.. you can see that numbers are updating every seconds, how’s that happening here they are using the Websocket connection which is a persistent connection between the stock brokers and the Stock Exchange, like NSE(National Stock Exchange) or BSE (Bombay Stock Exchange) these stock brokers are updating the data which is sent by these exchanges every seconds through webSocket connection, wait there are 5000 stocks are present in the Indian stock market are they going to update everything in the Stock broker user app, the answer is NO, The Stock Exchange will send the updated data to the Stock broker the data will be updating every second in the stock broker server through the WebSocket, the users are connected to the Stock brokers server for the data, the users are not directly connecting to the Stock Exchange, they are connected to the broker’s, This is a bit tricky let me explain it

The above image explains clearly about the transactions, like the stock brokers are connected to the Stock Exchange like BSE, NSE these Exchange will send the data to the stock brokers every seconds, every second the data is updating in the stock brokers end.
From the user’s end the users are requesting only the required stocks not all the stocks at once, just imagine users can see maximum 10 - 20 stocks at a time, if they are at the chart then we have to request the visible stocks to the server or else whenever user is requesting then the stock broker can reply with the response again and again if user is requesting.

Capacity estimation
We have 5000 Stocks in the Indian stock market, may be 1k - 2k Stock brokers would be there and for that stock brokers the users are connected to the stock brokers not directly to the stock exchange.

Order Management
There would be a order management service which is handling the Buy and Sell order of the particular stocks at the Stock Broker’s end, this would contains the stock details and the stock price and the buy order or the sell order details would be there and this is connected to the order validator service to validate the stock by the stock broker, in other end the order management is connected to the Order execution service which is directly connected to the Stock Exchange, to place the orders.

This flow is like whenever any of the user places an order it would be a buy order or a sell order it could be anything first it validated the order through the order validator, and then it goes to the order executor service, in order executor service, this is talking to the Stock exchange, to place the order which is placed by the user, these order executor and stock exchange are connected through the websocket, so that if the order is placed then the stock exchange has to give some acknowledgement about that order to the order Executor, after getting response from the stock exchange the order executor update the DB about the order details like OrderId, userId, orderType(buy or sell), price, stockId, etc.., and its going to store this data inside the Stock brokers DB, and notifies the user about the order status.
For Easy access we can use caching for the watchList, like home page, for showing the response quickly for the user, already zerodha is using Redis cluster for caching.

Order Tracker will get the status of the limit order which is placed by the user, through the Message queue price is updated in the watch list and order executor at the time at placing the order.

So this is the High level Design of the Stock Broker applications like Zerodha, Grow etc…

Thank You

0
Subscribe to my newsletter

Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manoj Kumar
Manoj Kumar