Module 04 · System Design · The parts, properly · about 17 min to read
Continue where you left off ×
The customer who won’t hang up (polling, SSE and WebSockets)
Four Systems in One Night · Open board
Four famous designs worked in one sitting. This lesson opens up the first of them, chat, whose hardest part is not storing messages but getting each one to a person the instant it exists.
Before you start. From The Contract at the Counter (API design) : two programs meet at a counter, and every exchange so far was one question, one answer. From TCP vs UDP, and the 3-way handshake and HTTP Keep-Alive : opening a connection (the open line between two computers) costs a round of “hello” messages, and keeping one open saves paying it again. From HTTP 07: Chunked Encoding : a server can send its answer in pieces as they happen. From The order slip rail (queues & the utilization cliff) : work that arrives in a bunch hurts far more than the same work spread out.
The idea in 30 seconds
Polling means asking on a timer. It wastes most requests and still delivers news late, so no polling speed is right.
Long polling holds each request open until there is news, which delivers instantly but makes the server hold many waiting requests.
A WebSocket is one connection kept open that carries messages both ways, so the connection cost is paid once.
Chat systems hold open connections on dedicated gateway machines and keep a list of which gateway holds each user.
A heartbeat is a small regular message that proves a connection is still alive, and presence (who is online) is often costlier than the messages because every change must reach many people.
When many clients lose their connection at once, they must wait random times before reconnecting, or they return in synchronised waves that knock the servers over again.
Read the story
Lesson 24 of 115 in System Design
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 13 May 2008: Facebook Chat, and the cost nobody expected The stall’s phone line: polling, asking again and again Hold the line: long polling A line both sides can talk on: the WebSocket A room of people holding phones: the gateway Is anyone still there? The heartbeat Who is on the line right now: presence The power trips: ten thousand callers redial at once Terms it explains: polling, long polling, WebSocket, gateway, heartbeat, presence, reconnect storm, jitter.
Review System Design
Mark as read Saved in this browser, and to your account when you sign in.