Girish Maity

Work / Case study

A WebSocket gateway that held 100,000 connections

Gateway processes behind one port fan messages out through Redis pub/sub, with token auth at the handshake, heartbeats, backpressure for slow clients and a graceful drain.

  • Node.js
  • TypeScript
  • WebSockets
  • Redis pub/sub
  • node:test

A chat room, a live dashboard or a multiplayer lobby needs one thing from its server: a message published by anyone reaches everyone in the room, now. With one server process that is a loop over sockets. With several processes, each holding a share of the connections, a message published on one must reach the members held by all the others.

Holding many connections also means planning for the ones that go wrong: clients that vanish without closing, clients too slow to read, and deploys that would otherwise drop everyone at once.

Schematic A client publishes through the gateway process that holds its socket. That process publishes to the room's Redis channel, and Redis sends the message to every process with members in the room, which deliver it to their own sockets.

Fan-out through Redis, per room

Each gateway process keeps an index of its own sockets by room. It subscribes to a room's Redis channel when its first local socket joins and unsubscribes when the last one leaves, so a process receives one Redis message per publish for each room it has members in, however many members it holds. The publishing process gets its own message back from Redis like every other process, which keeps one delivery path and one ordering.

Authenticate before the socket exists

An HMAC-signed, expiring token is checked in the HTTP upgrade handler, before the WebSocket handshake completes. A client without a valid token never gets a socket or any server state.

Heartbeats and slow clients

Every 30 seconds each socket is pinged, and one that hasn't answered since the last ping is terminated. That reaps half-open connections, such as a closed laptop lid, that would otherwise hold memory forever. Before each send the gateway checks how much is already queued for that socket. Above 1 MB the message is dropped for that socket only, and after 50 drops in a row the socket is cut. One slow phone costs at most about 1 MB and never delays anyone else.

Draining without a stampede

On shutdown a process stops accepting, removes itself from the cluster view, and closes its clients in 20 batches over a few seconds, so they reconnect to the other processes gradually instead of all at once.

Choices I'd revisit

  • Redis pub/sub is at-most-once: a process that is reconnecting to Redis misses messages. Redis Streams or a per-room sequence number would allow replay.
  • The ws library costs about 4 KB of heap per socket and about 26 µs per send in these runs. uWebSockets.js does the room loop in C++ and would be the next step if send cost mattered most.

The load test starts 4 gateway processes behind one port, opens connections from 8 client processes, puts every connection in one room, then publishes 20 messages one second apart. Each message fans out to every connection, and the test checks that every delivery arrived.

Everything ran on one Windows laptop (Ryzen 7 5800H, 16 threads, 15.3 GB RAM): the gateways, the 8 client processes and Redis in Docker.

100,000-connection runs, one laptop
RunHeldFailedGateway memoryDeliveriesFan-out p50 / p99
Run 3100,0000252 → 929 MB2,000,000 / 2,000,000314 / 684 ms
Run 4100,0000237 → 1,158 MB2,000,000 / 2,000,000331 / 1,218 ms

What limited these numbers

  • Memory was not the limit. The gateway used about 6–9 KB of memory per connection, so 100,000 connections need about 1 GB. The test clients used more than twice that.
  • The first wall was the client's operating system. An early run stopped at 15,709 connections because Windows hands out client ports from one range of 16,384. The test now binds explicit source addresses and ports. A server never hits this, since accepted connections share one port.
  • Fan-out time is the send loop, on a shared machine. Sending one message to about 25,000 sockets took one process about 650 ms while the client processes competed for the same CPU. One room with 100,000 members is the worst case; real traffic spreads across many smaller rooms.
  • No message was lost in any run: deliveries equalled connections held times messages published.

I haven't measured how much of the per-send cost is the gateway itself and how much is CPU contention with the clients. That needs the load generator on separate machines.