How Zerodha Streams Live Market Prices to Millions of Traders Without Crashing

Imagine 9:15 AM on a Monday morning. Millions of traders open the Zerodha app at the exact same second to track stock prices, check their open orders, or view market charts.

If Zerodha’s app repeatedly asked the server "Has the price changed yet?" every second (a technique called Polling), the servers would instantly collapse under billions of incoming requests. It would be like millions of people calling a bank every single second just to ask for their current account balance. Instead, Zerodha uses a Push Model. They keep a single, open communication channel to every user and push price changes the millisecond they happen. Let’s deep dive how it streams tens of thousands of price updates per second with zero lag.

Architecture

Leased TCP Lines (Direct Exchange Connection)

  • Instead of using standard internet connections, dedicated, high-speed fiber-optic cables are used to connect straight from stock exchanges like the National Stock Exchange (NSE) and Bombay Stock Exchange (BSE) directly into Zerodha's backend data centers.

  • Standard internet connections are unpredictable and introduce delays since data needs to go through multiple ISP routers. Leased TCP lines bypass the public internet entirely, ensuring market tick data reaches Zerodha in milliseconds.
    So stock brokers like - zerodha, groww host their backend service that is responsible to capture live data from exchange at the same data center where NSE/BSE backend is hosted (in Mumbai).

  • These dedicated cables are extremely expensive to maintain and NSE/BSE rented out to stock brokers, so not entire backend app is hosted in same data center. But only the service responsible to capture live data

Exchange Feeder Nodes (Go Ingestion Service)

  • These are ultra-fast programs written in the Go programming language that receive raw data streams directly from the exchange lines.

  • Stock exchanges broadcast market updates as raw, scrambled byte packages. Feeder nodes ingest these raw bytes, clean them up, format them into structured data, and send them downstream instantly.

  • If an exchange changes its data format unexpectedly, feeder nodes can crash or drop updates unless defensive parsing is built in. So exchange and brokers need to be in sync

Publishing live stock price to millions

Assume if millions of customer logged in to client app and making REST request to zerodha backend to get live stock data, entire system would crash within seconds. So this is how zerodha achieved this -

  • As soon as user opens client app (mobile or web app), web socket connection is established that would remain active for a session

  • Now whenever a user goes to a page that needs live data, ex - watchlist, order etc. It sends subscriber request to kafka topic to get live stock price for stocks added in watchlist or stocks in order list or dashboard

  • Zerodha don’t maintain the price anywhere in DB but it receives price from stock exchange and it publishes same to clients

  • Now to handle so many web socket connections and publish kafka messages, zerodha uses go routines rather than traditional threads. It is faster and light weight and takes very less memory

  • Also as soon user navigates to different pages it send unsubscribe request as well so that unnecessary subscribers are not there

Go WebSocket Edge Gateways & Goroutines

  • Imagine when 1 million users open the application at the same time, 1 million connections need to be maintained at the same time. Standard server threads consume roughly 1 Megabyte (MB) of memory each. Maintaining 1 million connections would require 1,000 Gigabytes (GB) of RAM just to keep the connections open

  • So it uses high-concurrency servers that hold open connections to user devices using Go's lightweight internal workers called Goroutines (instead of standard operating system threads).

  • A Go Goroutine uses only about 2 Kilobytes (KB) of initial stack memory, allowing a single server to handle tens of thousands of active socket connections effortlessly.

Forming live charts

  • You might have noticed that these stock broker sites also provide option to build charts for stock prices over different periods. These charts can be live charts during trading sessions or can be historical data charts like over a period of 5-days or one month etc

  • Generating complete chart images on the server for millions of users would require huge server clusters, instead servers only supply data and charts are formed at client side or via a separate middleware responsible to handle only chart building.

  • When client opens a chart, the app makes one standard web request (REST call) to download historical price and form candles. To also build live chart during trading session - it uses the ongoing WebSocket feed to draw new incoming tick points live on your screen.

What do you think that can be improved to handle this real world problem or what should be our next architecture to deep dive? Feel free to drop a note via email.

Cheers,

Tech Builder