Key takeaways
- ✓Polling costs per user, every interval: with 10,000 users each asking once a second, the server answered 2,749 requests a second on a core that was 91% busy, and a notification took 4,042 ms (4.0 s) at the median to arrive, with 1,459 of 29,998 still undelivered when the run ended.
- ✓A WebSocket is one two-way connection per user, and it cost the same at 10,000 users as at 1,000: 1.3 ms at the median and about 7% of the server's core, because the work is per message and the message rate did not change.
- ✓Server-sent events (SSE, one long HTTP response that the server keeps writing into) matched WebSockets for one-way news: 1.4 ms at the median and 8% of the core at 10,000 users. WebSockets earn their extra complexity when the client has to talk back, such as acknowledgements and heartbeats.
- ✓Long polling (a request the server holds open until there is news, then the client asks again) delivered at 3.3 ms but used 30% of the core, and made 44,074 requests for 29,993 messages at 10,000 users, about one per message plus about 14,000 extra when held requests timed out after 25 seconds with no news.
- ✓An idle WebSocket cost 21.3 KB of server memory and one open file descriptor, and one server process held 60,000 of them using 1.3 GB, with no limit reached at that count; the run stopped there.
- ✓What limits one server's connections is set by hand: the open-file limit (the default is often 1,024, and the lab raised it to 200,000), the source ports a client can use for each server address and port (tens of thousands), and memory for each connection's buffers and encryption, so every connection lives on one particular server.
A user's phone should buzz the moment an order ships. The question for this chapter is how the news gets there, and what each way costs the server. Lab 15 (Real-Time Delivery and Fan-out) of the system design labs ran four methods through one notification server process: polling, long polling, server-sent events and WebSockets. At 10,000 users the difference was large: polling kept the core 91% busy and took 4.0 seconds to deliver, while WebSockets delivered in 1.3 ms with the core at 7%.
The examples come from 01_push_methods.py (Exercise 1: 1,000 notifications a second for 30 seconds, sent to 1,000 and then 10,000 connected users) and 02_connections.py (Exercise 2: idle WebSocket connections opened on one server, in batches of 10,000, up to 60,000). The results files record one run of each script, on an Apple M4 Max with Docker Desktop (16 virtual CPUs, 7.7 GB of memory). The test clients ran on the same laptop, and the README says to read your own table. Treat the shapes as the lesson and the exact milliseconds as this machine's.
Polling: the cost is per question
In polling, the client asks "anything new?" on a timer. The lab's polling clients asked once a second, and each one waited for its answer and then waited a second more before asking again, so the rate was set by the server's speed, not just the timer.
Think of a car trip. Polling is a child asking "are we there yet?" every second. Most answers are "no," and each one still costs the driver a reply.
Each request reads the user's inbox from Redis (a fast shared message board) and answers, whether there is anything new or not. The cost follows the questions:
- At 1,000 users, polling was merely slow. The core was 15% busy, and a notification waited half the interval at the median: 507 ms.
- At 10,000 users, the same polling asked 2,749 times a second, the core was 91% busy, and each user got a turn about every 3.6 seconds. A notification took 4,042 ms at the median and 19,355 ms at the slowest one in a hundred. In all 1,459 of 29,998 notifications had not arrived when the run ended.
The test clients were straining too, at 2.7 cores, so part of that wait may come from the test itself. The lab's first observation still holds: most of those questions had nothing to say.
Long polling: wait on the server, then answer
Long polling keeps the request open. The server holds it until a message arrives, answers, and the client immediately asks again. There is a timeout as well: the lab's held requests gave up after 25 seconds with no news, so a quiet user still makes a request every 25 seconds.
Long polling is much cheaper than polling but not free. At 1,000 users it delivered at 3.0 ms with 31% of the core, because each message meant a new request, and each request subscribed to that user's news in Redis and unsubscribed again. At 10,000 users it delivered at 3.3 ms median, but 52.5 ms at the slowest one in a hundred, with 30% of the core and 44,074 requests for 29,993 messages.
Server-sent events: one stream, one way
Server-sent events (SSE) use one long HTTP response. The client opens it once, and the server writes an event into it whenever there is news. The connection waits on a queue; when a message arrives it writes it out, then waits again. There is no request per message and no polling.
In the lab, SSE and WebSockets were the same to within a tenth of a millisecond at both sizes. At 10,000 users SSE delivered at 1.4 ms with 8% of the core. It stays one way, server to client, which is all a notification needs. Its one hazard is the proxy in front of it: the lab's first SSE run through the HAProxy load balancer took 206 ms, because the proxy held back small writes. The fix was a proxy setting that sends each event at once.
WebSockets: one connection, both ways
A WebSocket is one connection that both sides can write to, the phone call in the car analogy. It is the choice when the client has things to say: acknowledgements that a message was shown, heartbeats that say the client is still there, a chat message. The lab's later exercises use acknowledgements and heartbeats on WebSockets; Exercise 1 measured only server to client.
The result at 10,000 users: 1.3 ms at the median, 3.5 ms at the 99th percentile, and 7% of the core. Going from 1,000 to 10,000 users changed nothing, because a user with a connection open costs memory, not CPU. The work is per message, and the messages did not change. The run's 30 seconds of traffic gave each user about three notifications, so each connection sat idle almost all the time.
Polling's requests per second were 2,749 at 10,000 users, and the server answered them on one core that was 91% busy at 10,000 users. The 1,459 undelivered messages at the end of the 10,000-user run are the queue that never drained. Long polling and polling are shown with their own request counts, not as push.
What one server holds
Every open connection is memory on the server, for as long as the user stays connected, plus one file descriptor, the operating system's handle for an open socket. Exercise 2 measured that cost directly, opening idle WebSockets in batches of 10,000 on one server process:
- The server's resident memory rose in a straight line: 51 MB with no connections, 259 MB at 10,000, and 1,300 MB at 60,000. That is 21.3 KB per idle connection on the server, and about 35 KB on the client that held them.
- The file descriptors rose one for one: 60,019 at 60,000 connections, one per connection plus a fixed 19 for the process.
- At 60,000 the server had not hit a limit, and the run stopped there. The machine's memory is the next limit: the server and clients shared 7.7 GB, which at 21 KB plus 35 KB per connection would fill somewhere past 100,000 connections, a calculation from those sizes rather than a run.
Memory per connection at this point: 21.3 KB, after subtracting the 51 MB the idle process used.Each connection also holds one file descriptor, the operating system's handle for an open socket. No limit was hit at 60,000; the run stopped there.
The limits that matter in production are mostly set by hand. The open-file limit is often 1,024 by default, so the lab raised it to 200,000. A client has only tens of thousands of source ports for each server address and port, so the test spread its connections over four server ports. Memory per connection also grows once the connections carry traffic, and with encryption, which the lab did not run. A connection lives on one particular server, so a deploy or a crash drops everyone on it at once. That is the subject of the next chapter.
The workflow
- Set up. Docker runs the notification server, Redis and the test clients.
01_push_methods.pytakes about seven minutes and02_connections.pyabout two. - Publish. The publisher sends 1,000 notifications a second for 30 seconds. Each one goes through Redis to the server holding that user's connection, or, for polling, is read from the user's inbox.
- Measure each method. Each run records how many notifications arrived, the delay at the median and the 99th percentile, the server's CPU share, and the requests per second.
- Scale the users. The same run is repeated with 10,000 users, which is where polling falls over.
- Hold connections.
02_connections.pyopens idle WebSockets in batches of 10,000 up to 60,000 and records memory and file descriptors after each batch.
Caveats
- One run per configuration. Each method and size was measured once on one laptop, so the millisecond values carry no error bars. The shapes (polling's queue, the flat WebSocket line) are the lesson.
- Clients on the same machine. The clients competed for the same 16 cores, and at 10,000 polling users they used 2.7 cores. Some of polling's 4-second delay may be the clients'.
- Synthetic load, one server. Exercise 1 used one server process for all four methods. A real deployment spreads users across several servers, and the lab's three-server setup was for Exercise 3 (
03_restart.py, a server dying mid-run). - Idle connections only in Exercise 2. No messages, heartbeats or encryption were on those connections. The lab's own note: with them, CPU and memory per connection go up.
- Polling at one interval. Only a one-second interval was measured. A 30-second interval would have made far fewer requests, and the lab did not test it.
- No test of reconnects. What happens to a user whose connection drops, and to messages sent while they were away, is the next chapter's question.
The checklist
- Measure the question rate, not only the message rate. Polling's cost is users times requests per second, whether or not there is news.
- For one-way news, SSE is enough. It matched WebSockets on delay and CPU here. Use WebSockets when the client needs to send on the same connection.
- Count connections the way you count memory. Budget about 21 KB per idle connection on the server, plus a file descriptor, then add what the traffic costs.
- Raise the limits on purpose. The open-file default, the client's source ports and the proxy's write buffering each stopped or slowed the lab until they were set.
Quiz
1.Ten thousand users each poll once a second, and each waits for its answer before asking again. Why did the server's core reach 91% while most answers were "nothing new"?
2.A WebSocket and a server-sent event stream both delivered at about 1.3 to 1.4 ms with 7 to 8% of a core, at 10,000 users. When would you still pick a WebSocket?
3.In the connection test one server process held 60,000 idle WebSockets using 1.3 GB, and no limit was hit. Which estimate of memory per connection is right?
Check your understanding
Try each question before opening its answer.
Polling and WebSockets both delivered to 10,000 users. Why did polling need 91% of the core while WebSockets needed 7%?
Polling does work per question: every request reads the user's inbox from Redis and answers, news or not, so 10,000 users asking once a second produced 2,749 answered requests a second. WebSockets do work per message. The message rate was the same 1,000 a second in both, so WebSockets did not get more expensive when the users went from 1,000 to 10,000.
Server-sent events matched WebSockets in the lab. What does a WebSocket give you that SSE does not?
A channel the client can write to on the same connection. SSE is one long HTTP response that only the server writes into. For news pushed to users, that is enough, and the lab measured the same speed and CPU. WebSockets pay off when the client sends acknowledgements, heartbeats or messages of its own.
Long polling delivered in 3.3 ms, far faster than polling's 4 seconds, but used 30% of the core. Where does its cost come from?
Each message ends a held request, and the client must make a new one, so long polling costs about one request per message plus the timeouts. At 10,000 users the lab counted 44,074 requests for 29,993 messages, about 14,000 extra from requests that timed out after 25 seconds with no news. Each request also subscribed to the user's news in Redis and unsubscribed again.
The connection test reached 60,000 idle WebSockets and stopped. Why was the server not the limit, and what would be?
At 60,000 connections the server had not hit a limit, and it used 1.3 GB, about 21 KB per connection. The limits that come first are set by hand or by the machine: the open-file limit (1,024 by default in many systems, raised to 200,000 in the lab), the client's source ports for each server address, and the total memory shared with the test clients. At 21 KB plus 35 KB per connection, those 7.7 GB would fill somewhere past 100,000 connections (a calculation, not a run).
Next up: Chapter 35, reconnects and missed notifications. This chapter's server kept running. Next, a server dies with a thousand users on it, and the chapter counts what they miss, and what replay and sequence numbers do about it.