ealeng

Episode 34 of 55 · Real-time

Polling, SSE and WebSockets: asking once a second kept one core 91% busy, and being told kept it at 7%

Ten thousand users asking once a second, through one notification server process, kept its core 91% busy: 2,749 requests a second, most answered "nothing new", and a notification took 4.0 seconds at the median. The same users on WebSockets, a two-way connection the server writes into, took 1.3 ms at 7% of the core, and server-sent events took 1.4 ms at 8%. Long polling sat between them at 3.3 ms and 30%. Then one server process held 60,000 idle WebSockets in 1.3 GB, and the limits that decide how many one server holds.

Lab 15 · Real-Time Delivery and Fan-out9 min read
Video transcript8:28

0:00Cold open

0:01

I sent a thousand notifications a second to ten thousand users, through one server process.

0:08

With every user set to ask for news once a second, the server’s one processor core was ninety-one percent busy. And a notification typically took four seconds to arrive.

0:21

The same ten thousand users on WebSockets: one point three milliseconds, with that core seven percent busy.

0:30

And that one process held sixty thousand idle connections open, in one point three gigabytes.

0:38

So why does asking cost so much more than being told?

0:44Intro

0:45

Hands-on system design, part thirty-four.

0:49

Today: four ways to get news to users, what each one costs, and how many connections one server holds.

0:57Meet Lucía

0:58

I’m Lucía, a platform engineer, and I’m building notifications: your order shipped, someone replied to you.

1:05

The test: a thousand notifications a second, for thirty seconds, each one for one connected user.

1:13

Once with a thousand users, once with ten thousand. All through one server program, which can use only one processor core. The test clients run on the same laptop.

1:26

Think of a car trip. Polling is a kid asking “are we there yet?” every second. Long polling is “wake me when we’re there”. Server-sent events are the driver announcing over the speakers. And a WebSocket is a phone call: both sides can talk.

1:4301 · What you need

1:45

To run it yourself: Docker for the server, Redis and the test clients. The quick tests run in a couple of seconds, without Docker.

1:54

No cloud, and nothing to pay for.

1:5902 · Four ways

2:00

The same server offers all four. Each notification goes through Redis, a fast shared message board, to the server holding that user’s connection. Polling just reads the user’s inbox there.

2:14

Polling: the client asks “anything new?” every second. That’s one request per user per second, whether there’s news or not.

2:23

Long polling: the server holds the request open until there’s something, answers, and the client asks again. Roughly one request per message.

2:34

Server-sent events: one long HTTP response, and the server writes into it whenever there’s news. One open connection per user, one way.

2:46

WebSockets: one connection that both sides can talk on.

2:5203 · A thousand users

2:54

With a thousand users, all four delivered every notification.

2:59

WebSockets and server-sent events: one point three milliseconds, typically. The server used six to seven percent of its CPU.

3:10

Long polling: three milliseconds, but thirty-one percent of the core. Each message means a new request, and each request signs up for that user’s news in Redis, then signs off again.

3:23

Polling: half a second, typically. A notification waits for the next “anything new?”, on average half a second away. With a thousand users, asking was still cheap: fifteen percent of the core.

3:3804 · Ten times the users

3:39

Now ten times the users: ten thousand, with the same thousand notifications a second.

3:47

With WebSockets, how much more CPU do you think the server needed?

3:53

None. Seven percent, and one point three milliseconds, the same as with a thousand.

4:01

Each user got only about three notifications in the whole test, so their connections sat idle almost all the time. An idle connection costs memory, not CPU. The work is per message, and the messages didn’t change. Server-sent events: eight percent.

4:2005 · Polling

4:21

Polling is different. Ten thousand users would ask ten thousand times a second. But each one waits for its answer, then a second more, before asking again. And at least three answers in four were “nothing new”.

4:36

The server’s core was ninety-one percent busy, and it could answer only about two thousand seven hundred requests a second. So each user got a turn about every three point six seconds, not every second.

4:50

A notification typically took four seconds to arrive. One thousand four hundred fifty-nine hadn’t arrived when the test ended. The test clients, on the same laptop, were straining too, at almost three cores, so part of that wait may be theirs.

5:07

Long polling: forty-four thousand requests for thirty thousand messages. About one per message, plus about one more per user: a held request that gave up after twenty-five seconds without news. Thirty percent of the core, the same as with a thousand users.

5:2706 · In code

5:28

In code, polling is one function per request: every request reads that user’s inbox from Redis and answers, whether there’s anything new or not.

5:39

Server-sent events: the connection waits on a queue. When a message arrives, it writes it out. Then it waits again.

5:48

Waiting costs nothing. So push does work per message, and polling does work per question.

5:5607 · Connections

5:57

So a connection costs memory. How much? I opened idle WebSockets on one server process, ten thousand at a time, and held them open.

6:08

The server program’s memory rose in a straight line: about twenty-one kilobytes per idle connection, plus what the operating system keeps for each.

6:19

And one file descriptor each: the operating system’s handle for an open connection.

6:26

At sixty thousand connections, the server program used one point three gigabytes, and it hadn’t hit any limit. I stopped there.

6:3708 · What runs out

6:38

Nothing ran out at sixty thousand. But here’s what would, and most of it is limits you set by hand.

6:46

The open-file limit: one file descriptor per connection. I raised it to two hundred thousand. The default is often one thousand and twenty-four.

6:57

Ports: each connection from one client address to one server port needs its own port number on the client, and there are only tens of thousands. So the test spread its connections over four server ports.

7:12

And memory: Docker on a Mac runs everything in one virtual machine, here seven point seven gigabytes. The server and the test clients shared it, at about twenty-one plus thirty-five kilobytes per connection. Together they’d fill it somewhere past a hundred thousand.

7:30

Idle is the easy case. With messages, keep-alive pings and encryption, each connection costs more. Measure your own traffic.

7:40Back to Lucía

7:42

So, three lessons.

7:45

Polling costs per user, every second. Push costs per message.

7:50

For one-way news, server-sent events matched WebSockets on speed and CPU. WebSockets pay off when the client talks back.

8:00

And every open connection costs memory, and lives on one particular server.

8:07

Next time: I kill a server with a thousand users on it, and count what they miss.

8:14

For today: don’t ask. Get told.Lucía: Don’t ask. Get told.

8:17Summary

8:19

Polling costs per user, push costs per message, and every connection costs memory on one server.

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.

Try it · Delivery and server costLab 15 · 01_push_methods.py · recorded, 1,000 messages a second for 30 s
WebSockets
29,995 of 29,995 delivered
Delay, median
1.3 ms
Server CPU
7%
Server-sent events
29,994 of 29,994 delivered
Delay, median
1.4 ms
Server CPU
8%
Long polling
29,993 of 29,993 delivered
Delay, median
3.3 ms
Server CPU
30%
Polling every 1 s
28,539 of 29,998 delivered
Delay, median
4,042 ms
Server CPU
91%

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.
Try it · Memory per idle connectionLab 15 · 02_connections.py · recorded, one server process
04008001,200020,00040,00060,000MB
Open connections
60,000
Server memory
1,300 MB
Open file descriptors
60,019
Recorded slope
21.3 KB

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

  1. Set up. Docker runs the notification server, Redis and the test clients. 01_push_methods.py takes about seven minutes and 02_connections.py about two.
  2. 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.
  3. 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.
  4. Scale the users. The same run is repeated with 10,000 users, which is where polling falls over.
  5. Hold connections. 02_connections.py opens 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

  1. Measure the question rate, not only the message rate. Polling's cost is users times requests per second, whether or not there is news.
  2. 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.
  3. 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.
  4. 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

Try it · Check your understanding0 of 3 answered
  1. 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. 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. 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.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.