Scrivio Interview library
ScenarioAdvanced6 min read · updated 13 November 2026

A notification service holds 400,000 WebSocket connections on 20 nodes. Before an event, 10 more nodes were added, but they stayed nearly empty while the original nodes ran short of memory, and rolling deploys take two hours because each node waits for its connections to end.

Why does scaling out not help, and how would you balance and deploy long-lived connections?

A load balancer only chooses a node when a connection opens, and WebSocket connections stay open for hours, so the 10 new nodes only receive the trickle of new connections while the old nodes keep their 400,000. We fix it by making connections move: give each one a maximum lifetime with jitter, actively shed a small share from overloaded nodes with a reconnect hint, use least-connections with a per-node cap for new connections, and drain nodes on a schedule during deploys instead of waiting for connections to end.

Why this question is asked

Scaling out is the reflex answer to load, and for short HTTP requests it works within seconds because every new request can go to a new node. Long-lived connections break that intuition, and this scenario shows the two symptoms that follow: new capacity that sits idle, and deploys that take hours. Interviewers ask it at senior level because the fix is not a load balancer setting but a design that treats connections as things the system moves on purpose. In this answer, we will see why the balancer cannot help, design active rebalancing with lifetimes and shedding, protect nodes with caps, and turn the two-hour deploy into a bounded, scheduled drain.

1A Quick Recap: Where a Balancer Makes Its Decision

For HTTP requests, a load balancer picks a backend for every request, or at least every short connection. For a WebSocket, the decision happens exactly once, when the client connects and the upgrade completes. After that, the connection is pinned to that node until it closes. A notification connection may stay open for many hours, because the app holds it while running.

Our fleet holds 400,000 connections on 20 nodes, 20,000 each. When we add 10 nodes, nothing happens to those 400,000. Only new connections, from users who just opened the app or whose connection happened to drop, are spread over 30 nodes. If 2 percent of connections turn over per hour, the new nodes gain a few thousand connections in that hour while the old nodes keep nearly all of theirs. Before an event, when traffic climbs, the old nodes run out of memory first.

Connections per node after adding 10 nodes
TimeOriginal 20 nodes (each)New 10 nodes (each)
Before scaling20,000-
1 hour later, natural churn onlyabout 19,700about 600
Event starts, 100,000 new usersabout 21,000 and risingabout 6,000
Goal: balancedabout 16,700about 16,700
Rough numbers: with natural churn alone, the fleet takes days to even out.

2Least-Connections and Per-Node Caps for New Connections

The first step is to place new connections well. Round robin sends the same number of new connections to every node, which keeps the old nodes as overloaded as they were. Least-connections sends each new connection to the node with the fewest open connections, so during the event all new users go to the new nodes until they catch up. Most layer 7 balancers support it, and so does NGINX with least_conn.

Second, each node needs a hard cap. A node that has reached, say, 25,000 connections or 80 percent of its memory should refuse new upgrades with a quick, retryable error (for example 503 with Retry-After, or failing its readiness check so the balancer stops sending), rather than accepting connections until it runs out of memory and crashes. A crash would drop 25,000 clients at once and send them all to the remaining nodes, which is how one overloaded node becomes a cascade.

3Making Connections Move: Lifetimes and Active Shedding

Better placement only affects new connections, so we also need existing connections to move. There are two complementary tools.

The first is a maximum lifetime with jitter. Every connection is closed by the server after a random duration between, for example, 30 and 60 minutes, with a close frame that tells the client to reconnect immediately. The jitter matters: without it, all the connections opened at the start of an event would expire at the same moment. With lifetimes, the fleet rebalances itself continuously. Within an hour every connection has been through the balancer again, and no node holds anything older than the limit.

The second is active shedding for faster results. When nodes are added, a rebalancing controller compares each node's count with the fleet average and tells overloaded nodes to close a small fraction of their excess at a time, for example 1 percent of connections every 10 seconds, with a reconnect hint. The clients reconnect, least-connections sends them to the new nodes, and the fleet converges in minutes. The rate limit protects the authentication service and the message store from a reconnect storm.

Step throughActive shedding after scale-out
  1. Controller reads connections per nodeaverage 13,300 after adding 10 nodes
  2. Node above average by more than 10 percent?
    YesPick 1 percent of its connectionsNoLeave it alone
  3. Send a close frame with a reconnect hintclient waits a random 0 to 5 s
  4. Client reconnects through the balancerleast-connections picks a new node
  5. Client resumes from its last message idnothing is lost
  6. Repeat every 10 s until balancedstop if errors or latency rise

6 steps in order. Play them, or step through them one at a time.

4Deploying Without Waiting Two Hours

The current deploy waits for each node's connections to end naturally, and with long-lived connections that is the slowest possible strategy. Instead we drain on a schedule. When a node is told to stop, it first fails its readiness check so the balancer stops sending new connections. Then it closes its existing connections in batches, for example 5 percent every few seconds, each with a reconnect hint, while clients reconnect with jittered backoff to other nodes. After a bounded time, say 2 minutes, any remaining connections are closed and the node stops. Kubernetes' terminationGracePeriodSeconds must be longer than this drain time.

With 20,000 connections per node and a drain of 2 minutes, we can replace a few nodes at a time, and a 30-node fleet deploys in well under an hour. Notice that the deploy and the rebalancer use the same building block: closing connections deliberately and letting clients come back elsewhere.

5What the Client and Server Must Promise

All of this depends on clients that treat a server-initiated close as routine. The client reconnects with backoff and jitter, honours a reconnect hint, and resumes from the last message it processed. On the server side, notifications must not live only in the node's memory. Each notification is written to a durable store or log, such as a Redis stream or a Kafka topic per user group, with an increasing id, and the client sends its last id when it reconnects so the new node can replay what was missed. Without that, every rebalance and every deploy would lose messages, and we could not move connections freely.

6Follow-up questions

How do you make sure messages sent during a reconnect are not lost?

Every notification is stored durably with an increasing id before it is pushed, for example in a Redis stream or a Kafka topic keyed by user, with retention of a few hours. The client acknowledges or at least remembers the last id it processed and sends it on reconnect. The new node replays everything after that id and then switches to live delivery. Messages may then arrive twice around the switch, so the client ignores ids it has already seen.

Why not let the load balancer rebalance existing connections?

It cannot move an established TCP connection with its TLS and WebSocket state to another server. Only the endpoints can end a connection and start a new one, so rebalancing has to be driven by the server closing connections and the client reconnecting.

How do you size the cap per node?

From a load test that measures memory and CPU per connection, including the buffers for slow clients and the peak message rate. If each connection costs about 50 KB and a node has 2 GB available for them, 40,000 is the hard limit, and we set the cap well below it, for example 25,000, to leave headroom for bursts.

7Conclusion

In this answer, we saw that balancers only place WebSocket connections when they open, so new nodes stay empty while old nodes keep their connections. We fixed placement with least-connections and per-node caps, and made existing connections move with jittered maximum lifetimes and rate-limited active shedding. Deploys became a scheduled drain: stop accepting, close in batches, stop after a bounded time. All of it relies on clients that reconnect with backoff and resume from a durable message log.

What to say in the interviewAbout 80 seconds aloud

Open with

The balancer only makes a decision when a connection opens. Our 400,000 WebSocket connections stay open for hours, so when we added 10 nodes, nothing moved; the new nodes only got the small stream of new connections while the old ones kept theirs and ran out of memory.

Then, if they want more

So I would make connections move on purpose. For new connections, use least-connections rather than round robin, and give every node a hard cap so it refuses upgrades instead of overloading and crashing.

For existing ones, give every connection a maximum lifetime with jitter, say 30 to 60 minutes, so the fleet rebalances continuously, and after a scale-out have a controller tell overloaded nodes to close a small percentage of connections at a time with a reconnect hint, so clients come back through the balancer and land on the new nodes.

Deploys use the same mechanism: the node fails readiness, closes its connections in batches, and stops after a bounded time like two minutes rather than waiting for connections to end naturally.

All of this requires clients that reconnect with jittered backoff and resume from the last message id, and a durable store for notifications so nothing is lost during the move.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why does scaling out not help, and how would you balance and deploy long-lived...": https://scrivio-interview-library.netlify.app/architecture/load-balancing-networking/6a7bcbf48b/