Skip to content

Lesson 4 of 9 · 24m

Scaling Connections: Sticky State Without Sticky Architecture

Connection state is local to a process; scaling means separating socket ownership from application state and designing for redistribution.

One Node process can hold tens of thousands of mostly idle WebSocket connections. The first scaling wall is rarely CPU; it is the moment you deploy a second instance and discover that sockets do not magically know each other. Connection ownership is local. Everything else must be designed around that fact.

The mental model: sockets are attached to one process

client A ---- ws ---- server 1 ----┐
                                    ├---- shared broker/state
client B ---- ws ---- server 2 ----┘

If A and B join the same room on different servers, server 1 cannot write directly to B's socket. It publishes to a shared broker; server 2 receives the event and writes locally. This is the canonical fan-out shape for horizontally scaled realtime.

Sticky sessions can keep reconnecting clients on the same server, but they do not solve cross-server delivery. They also make deployments and failover worse. Treat stickiness as a compatibility crutch, not architecture.

Separate connection state from durable state

Keep three kinds of state distinct:

  1. Ephemeral connection state — socket handle, negotiated protocol, last pong, local subscriptions.
  2. Shared realtime state — room membership, presence, rate-limit counters, delivery cursors.
  3. Durable application state — messages, documents, permissions, audit logs.

Only the first belongs in process memory. Shared state belongs in Redis, NATS, Postgres, or whatever your operational stack already runs well. Durable state belongs in your database of record.

Capacity is file descriptors, memory, and write shape

A mostly idle connection is cheap. A connection receiving large messages at high frequency is not. Capacity planning starts with writes per second and bytes per write, not raw socket count.

10,000 clients x 1 small event/sec x 500 bytes = 5 MB/sec egress
10,000 clients x 20 events/sec x 2 KB       = 400 MB/sec egress

Kernel limits matter too. File descriptors, ephemeral ports on proxies, TLS termination CPU, and conntrack tables all become ceilings before application logic does. On Linux, check ulimit -n, systemd LimitNOFILE, and your load balancer's maximum connection settings together.

Backpressure is not optional

If a client reads slowly, writes queue in memory. Ten thousand slow readers will eat the process. Every serious server needs a bounded outbound queue and a policy when it fills: drop low-priority events, coalesce them, or close the socket.

if (ws.bufferedAmount > 1_000_000) {
  metrics.increment("realtime.slow_consumer_close");
  ws.close(1013, "slow consumer");
}

Dropping a typing indicator is fine. Dropping a chat message is not unless the client can resynchronize. Priority and resumability determine what can be shed.

Rolling deploys are connection migrations

A deploy closes thousands of sockets at once unless you drain. Stop accepting new connections, send close with 1001 or application code server restarting, wait for clients to reconnect elsewhere, then exit. Clients must add jittered exponential backoff or the fleet experiences a reconnect thundering herd.

Field notes

  • Count messages written per second and bytes written per second per instance; socket count alone is a vanity metric.
  • Load balancers can become the largest connection pool in the system. Size them as realtime infrastructure, not HTTP plumbing.
  • TLS session reuse matters at reconnect scale. Handshake CPU spikes are real during mass reconnects.
  • Autoscaling on CPU often reacts too late. Use connection count, outbound queue depth, and write rate as signals.

Was this lesson helpful?

Sign in to rate