Quick revision
The short answer
Half a million long-lived connections are a capacity problem before they are a code problem: we plan file descriptors, memory per connection and load balancer connection limits, and spread connections across a fleet of gateways that do nothing but hold sockets. Gateways subscribe to a broker for fan-out, so a goal event reaches each gateway once and is copied locally to its subscribers; business services stay separate. A reactive server such as WebFlux on Netty keeps memory per connection low, jittered reconnects and connection draining prevent reconnect storms after deploys, and we authenticate at the handshake and re-check authorisation on every subscription.
What to remember
In this answer, we planned 500,000 WebSocket connections as a capacity problem: memory and file descriptors per connection, load balancer limits and source ports, and a fleet of about 30 gateways with headroom. Gateways hold connections only and subscribe to a broker per match, so a goal becomes one delivery per gateway and local fan-out. WebFlux on Netty keeps memory per connection low, jittered reconnects, draining and handshake rate limits prevent reconnect storms, and we authenticate at the handshake while re-checking authorisation on every subscription.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Half a million long-lived connections are a capacity problem before they are a code problem: we plan file descriptors, memory per connection and load balancer connection limits, and spread connections across a fleet of gateways that do nothing but hold sockets. Gateways subscribe to a broker for fan-out, so a goal event reaches each gateway once and is copied locally to its subscribers; business services stay separate. A reactive server such as WebFlux on Netty keeps memory per connection low, jittered reconnects and connection draining prevent reconnect storms after deploys, and we authenticate at the handshake and re-check authorisation on every subscription.
Why this question is asked
Live sports traffic has an unusual shape. Most of the time connections are quiet, and then a goal in a big match sends an update to 200,000 people within the same second, and a deploy can drop tens of thousands of connections that all try to come back at once. Interviewers ask this question at senior level to see whether we can turn a big number into concrete limits and an architecture that respects them. In this answer, we will size the problem, separate gateways from business services, plan the per-server limits, design fan-out, handle deploys and reconnect storms, and secure connections and subscriptions.
1Sizing 500,000 Connections
A WebSocket connection is a TCP socket that stays open for the whole time a user watches. On the server, each one costs a file descriptor (the operating system's handle for an open socket), some kernel memory for socket buffers, and application memory for the session object, buffers and subscriptions. On a reactive server, a reasonable planning figure is a few tens of kilobytes per mostly idle connection; we measure it in a load test rather than trusting a number.
Let us plan with 50 KB per connection and 25,000 connections per gateway instance. That gives 20 instances for 500,000 connections, about 1.25 GB of connection memory each, and we add headroom for peaks and for losing a zone, say 30 instances. Spreading across that many also limits how many users one instance failure disconnects: 1 in 30 instead of 1 in 5.
| Item | Per gateway | Fleet of 30 |
|---|---|---|
| Connections | about 17,000 normal, 25,000 max | 500,000 with headroom |
| Connection memory at 50 KB | about 1.25 GB at max | about 25 GB in total |
| File descriptors | ulimit raised to 100,000+ | - |
| Messages per goal in a big match | about 7,000 local sends | about 200,000 sends |
2Separating Connection Gateways From Business Services
The key architectural decision is to keep connection holding apart from business logic. Gateways accept WebSocket connections, authenticate them, track which matches each connection follows and push messages. They contain no scoring logic and change rarely. Score services compute updates and publish them to a broker; they never hold client connections.
The reason is that the two have opposite needs. Gateways want to run for days without restarting, because each restart disconnects users; business services want to deploy several times a day. Gateways scale with the number of connections; services scale with the number of events. Separating them lets each scale and deploy on its own terms.
- Clientsweb and mobile, one connection each
- Load balancer (L4 or L7)upgrade support, long idle timeout, connection limits
- Connection gatewaysWebFlux on Netty, 25k connections each
- Brokerone topic per match, gateways subscribe
- Score servicescompute updates, publish, no client connections
5 layers, from the top down. Step through them one at a time.
3Per-Server Limits in Spring and the Operating System
A gateway built with Spring WebFlux and a WebSocketHandler on Reactor Netty handles all connections with a small number of event-loop threads and allocates buffers only while data moves, which keeps memory per idle connection low. A servlet stack can also hold many connections, especially with virtual threads, but the reactive stack has a longer track record at this density; we choose by load test. Notice that Spring's STOMP broker support runs on the servlet-based messaging stack, not WebFlux, so a WebFlux gateway uses a small JSON protocol of its own: subscribe, unsubscribe and update messages.
Around the code, the operating system and network need tuning. The open file limit must exceed the connection target, for example 100,000 per process. Between the load balancer and a gateway, each connection uses a source port, and one source address to one destination address and port allows only about 64,000 of them, so either the load balancer uses several source addresses or gateways expose enough instances. The load balancer itself has connection limits and an idle timeout, so we pick one rated for this many concurrent connections and set the idle timeout above our heartbeat interval.
Notice the backpressure choice. For scores, only the newest value matters, so when a client on a slow mobile network cannot keep up, we drop older updates rather than buffer them. Buffering would make memory grow with every slow client.
4Fan-Out Through the Broker
When a goal is scored, the score service publishes one message to the match's topic. Each of the 30 gateways that has at least one subscriber for that match receives it once, and then sends it to its local connections. So a goal in a match followed by 200,000 users costs the broker 30 deliveries, and each gateway about 7,000 local sends, which takes milliseconds on an event loop.
This is why gateways subscribe per match, not per user. Per-user subscriptions in the broker would mean 200,000 broker deliveries for one goal. Redis pub/sub, Kafka or NATS all work; the choice depends on what the company runs. Where updates are very frequent, for example live odds, the gateway can also coalesce: send at most one update per match per second, always the latest.
5Deploys, Drains and Reconnect Storms
A reconnect storm happens when many clients lose their connection at the same moment and all reconnect immediately. If a gateway holding 25,000 connections dies, 25,000 handshakes, each with token validation and subscription setup, hit the remaining gateways within a second or two. Worse, if a bad deploy restarts all gateways, 500,000 clients reconnect at once and the authentication service may fall over.
We defend on both sides. Clients reconnect with exponential backoff and random jitter: the first retry after a random 0 to 5 seconds, then up to 30 seconds, so arrivals spread out. Servers drain instead of dropping: during a rolling deploy, a gateway stops accepting new connections, tells its clients to reconnect, and closes them gradually over a few minutes, so other gateways absorb them at a steady rate. Gateways also rate limit new handshakes, rejecting with a retry hint above, say, 2,000 per second per instance, which protects them and the services behind them.
6Authentication and Authorisation
We authenticate once, at the handshake, with a short-lived token: in a cookie for web clients, or in a header or the first message for mobile clients. Putting tokens in the URL query string is easy but they then appear in access logs, so we avoid it or use one-time tickets. The gateway validates the token locally, for example a signed JWT, so handshakes do not call the identity service 500,000 times.
Authentication is not authorisation. A connection may last for hours, and the user may subscribe to many matches, some of them premium content. So each subscribe message is checked against the user's entitlements, and when the token expires, the gateway requires a refresh message or closes the connection. Otherwise a cancelled subscription would keep receiving premium data until the user next reconnected.
7Follow-up questions
Would SSE be a simpler choice for live scores?
Very possibly. Live scores are almost entirely server-to-client, and subscriptions can be expressed in the request, so SSE over HTTP/2 would give the same fan-out with standard HTTP infrastructure and built-in reconnection. The capacity planning, gateways, broker and reconnect storms are the same either way. WebSocket makes sense if clients also send frequent messages, such as reactions or chat.
How do you load test 500,000 connections?
With a distributed load generator that holds real connections from many machines, because one client machine runs out of source ports and file descriptors too. We ramp to the target, keep the connections mostly idle with heartbeats, then simulate a goal burst and a gateway kill, and measure memory per connection, send latency and reconnect behaviour.
What happens to a client that misses updates during a reconnect?
When it subscribes again, the gateway sends the current state of each match first, not a history of events. For scores, the latest state is all the client needs, which keeps reconnects cheap.
8Conclusion
In this answer, we planned 500,000 WebSocket connections as a capacity problem: memory and file descriptors per connection, load balancer limits and source ports, and a fleet of about 30 gateways with headroom. Gateways hold connections only and subscribe to a broker per match, so a goal becomes one delivery per gateway and local fan-out. WebFlux on Netty keeps memory per connection low, jittered reconnects, draining and handshake rate limits prevent reconnect storms, and we authenticate at the handshake while re-checking authorisation on every subscription.
Open with
I would start with the limits, because 500,000 connections is mostly a capacity problem. Each connection costs a file descriptor, socket buffers and session memory; if a load test shows around 50 KB per idle connection and we target 25,000 per instance, that is 20 gateways, and I would run about 30 for headroom and zone failure.
Then, if they want more
I would raise file descriptor limits, make sure the load balancer is rated for that many concurrent connections, has a long idle timeout and enough source addresses so we do not run out of ports. Architecturally, I would separate connection gateways from business services.
Gateways only authenticate, track subscriptions and push; score services publish to a broker topic per match, and each gateway subscribes once per match, so a goal is one message per gateway and the gateway fans out locally.
For the gateway I would use WebFlux on Reactor Netty with a WebSocketHandler, because it keeps memory per connection low, and drop older updates for slow clients. The other big risk is reconnect storms: clients use exponential backoff with jitter, gateways drain gradually during deploys and rate limit handshakes.
For security, I authenticate at the handshake with a short-lived token validated locally, and check authorisation again on every subscription, closing the connection when the token expires.
About this answer
- Last updated
- 12 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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
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 "Design the architecture for 500,000 concurrent WebSocket connections for live sports...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/deb0b43ce5/