The whole course ▾
System Design · Lesson 18 of 39 · Week 4
Real-time updates: polling, SSE and WebSockets
QuickBite's customers want to watch their rider move across the map, live. Today: polling, server-sent events and WebSockets.
Got a doubt? Ask in the comments under the video.Subscribe
Live rider tracking on QuickBite's map.
QuickBite is the food delivery app we grow through the course, from one server to millions of orders.
Where's my rider?
Until now, every conversation started with the phone asking and the server answering. Live tracking breaks that pattern.
The rider app sends its GPS position to a location service every four seconds. The customer app wants to show the bike moving on the map, the moment it moves. But plain HTTP works one way: the phone asks, and the server answers. The server can't simply call the phone when the rider turns a corner. So how does the customer find out?
And the scale makes it harder. At dinner time, two hundred thousand customers are watching a rider at the same moment. There are four ways to get the news to them, from the simplest to the most capable, and each one has a cost. For each one, ask the same two questions: how many requests does it cost, and how fast does the news arrive?
Asking again and again
The first two ways keep the old pattern: the phone keeps asking.
Short polling: the customer app asks every two seconds, any news? It's easy to build and works everywhere. But most answers are: nothing new. Two hundred thousand customers asking every two seconds is a hundred thousand requests a second, mostly wasted. Ask more often, and the servers melt. Ask less often, and the bike jumps across the map.
Long polling is smarter. The app asks, and the server holds the request open instead of answering: until there's news, or about thirty seconds pass. It answers the moment the rider moves, and the app immediately asks again. Far fewer empty answers, and news arrives at once. But every single update still costs a brand-new request, with all its headers. And the server must hold tens of thousands of waiting requests at once, which servers built for one thread per request handle badly.
Keeping the line open
The other two ways stop asking. They open one connection, and keep it open.
Server-sent events: the phone opens one ordinary HTTP request, and the server keeps sending events down it as they happen. Simple, works through most proxies, and reconnects by itself. But it's one-way: to send anything back, the phone makes a separate request. WebSockets start as HTTP, then upgrade into a two-way connection that stays open, and either side can send a small message at any moment. QuickBite also wants customers to chat with their rider, so WebSockets it is.
So choose by direction and rate. Updates flowing one way, like a live score or a progress bar: server-sent events are enough. Messages both ways, like chat or a game: WebSockets. And updates every few minutes: plain polling is fine, and the simplest thing that works is a strong answer in an interview. Chat apps and ride-hailing apps keep long-lived connections exactly like this.
Two hundred thousand open lines
Open connections bring a new problem. Each phone is connected to one particular server, and the rider's update may arrive somewhere else entirely.
A WebSocket is a long-lived connection to one specific machine. So we run dedicated connection servers, whose only job is holding open connections, up to hundreds of thousands each. When the customer app connects for order four-two-one, its connection server subscribes to that order's channel on a pub/sub system, like Redis. Now it will hear everything said about that order.
Now the rider app sends a GPS point to the location service, which publishes it on channel four-two-one. Pub/sub delivers it to whichever connection servers subscribed, and they push it down the open sockets to the right phones. The location service never needs to know which server a customer is connected to. That's the whole trick: connections in one place, routing through pub/sub.
Running them takes care. Keep connection servers separate from the stateless API servers, because they hold state and deploy differently. Send small heartbeats, so dead connections are noticed and closed. And when a connection server restarts, its clients reconnect to others, each after a small random delay, so they don't all arrive in the same second. Load balancers need care too: they must allow connections that last for hours, and spread new ones evenly.
What it costs
Open connections are powerful, and they're not free.
Connection servers hold state: every connection lives on one machine, so scaling and deploying them is harder than with stateless servers. Mobile networks drop connections all the time, in lifts and tunnels, so the app must reconnect and catch up on what it missed. And keeping the phone's radio awake costs battery, so the app should close the connection when the order is delivered. None of these is a reason to avoid sockets; they're things to plan for.
And send less. Send only what changed: a new point, not the whole order. Throttle to what the eye can see: a bike icon updating every second or two looks smooth, especially if the app animates it between points. Fewer messages means cheaper servers, and longer battery.
In the interview
Real-time updates come up in chat apps, ride-hailing, delivery, live comments, and games.
Say it like this: clients hold WebSockets to dedicated connection servers, and pub/sub routes each update to the servers whose clients care, with polling as a fallback when a connection can't be held. Then add one line about reconnecting, because mobile connections always drop. And if the updates only flow one way, say server-sent events instead, and explain the choice by direction.
Then expect these. Why not just poll? At this scale, mostly empty requests, and slower news. A connection server dies: now what? Its clients reconnect to other servers, which subscribe to the same channels. And what about the updates a phone missed while it was offline? On reconnect, it asks for the latest state with one ordinary request, then continues live.
The common mistake is drawing WebSockets and stopping there. The interviewer will ask how an update reaches the one server holding the customer's socket. Pub/sub is the answer.
Quick check
A match score changes every few minutes, and only flows to viewers. Which approach?
Show the answer
Server-sent events, or simple polling: one-way, rare updates.
A rider's update reaches one server, but the customer is connected to another. How does it get there?
Show the answer
Pub/sub: the customer's server subscribed to the order's channel.
Remember: polling is simple, and wasteful at scale. Server-sent events go one way, and WebSockets both ways. And at scale, connection servers hold the sockets while pub/sub routes the updates. Tomorrow, protecting all of this from a flood.
Lessons on weekdays, the week's revision on Saturday, a mock interview on Sunday.