System Design AI

WebSockets: Follow-Up Questions Interviewers Ask and How to Answer Them

When you drop WebSockets in a design interview, the interviewer isn’t just checking if you know the API. They want to see whether you understand the operational realities of long-lived connections and how they fit into a larger distributed system. Probing questions reveal gaps in your mental model of scaling, fault tolerance, and observability.

A well-crafted answer shows you can move from the protocol level to the infrastructure level, and that you can anticipate the edge cases that appear in production. Below is a progression of the typical follow-up questions, from the most common to the hardest, each answered as you might in a real interview.

Connection Management and Scaling

1. How many concurrent WebSocket connections can a single server handle?

I would start by saying the limit is largely a function of the server’s file descriptor budget and memory per connection. On a typical Linux box with a 64-bit kernel, you can raise the open-file limit to a few hundred thousand. In practice, a single EC2 m5.large (2 vCPU, 8 GB RAM) can sustain roughly 30 k concurrent connections if each connection consumes about 250 KB of memory for buffers and state. Beyond that you’d need to shard across multiple instances behind a load balancer such as Amazon Elastic Load Balancing with WebSocket support.

2. How do you load-balance WebSocket traffic?

Because WebSockets are long-lived, you need a load balancer that passes the HTTP upgrade through and allows long idle timeouts. In AWS an Application Load Balancer supports WebSockets natively: it routes the upgrade request to a target and the connection stays on that target for its lifetime, so cookie-based stickiness only matters if clients fall back to multi-request transports such as long-polling. Alternatively, you can place a reverse proxy like NGINX or HAProxy in front, configured to pass the Upgrade and Connection headers and to use long read timeouts. The key is to ensure that once a connection is established, it stays on the same backend until it closes.

3. What happens if a backend instance crashes while a client is connected?

If the backend crashes, the TCP connection breaks without a close frame, so the client sees an abnormal closure or, if packets are silently dropped, notices only when a heartbeat times out. The load balancer stops sending new connections to the failed target, but it cannot move an existing connection; the client has to reconnect. To make this seamless, the client should implement exponential backoff and a reconnection strategy that re-establishes the WebSocket after a short pause. On the server side, you can use a shared state store (e.g., Redis) to persist any session data so that a new instance can pick up where the crashed one left off.

Message Delivery Guarantees

4. Do WebSockets guarantee ordered delivery?

WebSocket frames are delivered over TCP, so they inherit TCP’s in-order guarantee. That means a client will see messages in the same order they were sent, as long as the connection stays alive. However, if you implement application-level multiplexing (e.g., multiple logical channels over a single socket), you need to enforce ordering yourself for each channel, because interleaving can happen at the application layer.

5. How would you handle message loss or duplication?

Since TCP ensures no loss, the only scenario where you might see duplication is when a client reconnects after a network glitch and the server resends unacknowledged messages. To handle this, I would add a monotonically increasing sequence number to each message and have the client track the last sequence it processed. On reconnection, the server can resume from the next sequence, and the client can discard any duplicate numbers it has already seen.

Security and Authentication

6. How do you authenticate a WebSocket connection?

The usual pattern is to perform the authentication during the initial HTTP handshake that upgrades to WebSocket. You can include a JWT in the "Authorization" header or as a query parameter. The server validates the token before completing the upgrade. After the connection is established, you can also perform a secondary check by sending a short "auth" message over the socket, allowing you to rotate keys without re-handshaking.

7. What are the main security concerns with WebSockets?

Because the connection stays open, it can be a vector for denial-of-service attacks if a client sends a flood of frames. Rate limiting at the load balancer or using a Web Application Firewall (WAF) helps mitigate this. Additionally, browsers do not apply the same-origin policy or CORS to WebSocket connections, so the server must check the Origin header during the upgrade request to prevent cross-site WebSocket hijacking. Encrypting the traffic with TLS (wss://) is mandatory to protect data in transit.

Operational Concerns

8. How do you monitor the health of a WebSocket service?

I would instrument three key metrics: active connection count, message latency, and error rate. Active connections can be scraped from the server’s socket statistics or exported via a Prometheus exporter. Latency is measured by timestamping messages at send and receive points. Error rate includes failed upgrades, protocol violations, and abrupt disconnects. Alerting on sudden spikes in any of these metrics helps catch problems early.

9. What is the hardest scalability challenge with WebSockets, and how would you solve it?

The toughest part is maintaining state for millions of concurrent connections without overwhelming any single node. A common solution is to partition users by a hash of their user ID and route them to a specific shard. Each shard runs its own WebSocket server and shares a pub/sub backbone like Amazon SNS or a Redis cluster. When you need to broadcast a message to all users, you publish to the backbone, which fans out to each shard. This approach scales horizontally and keeps per-node memory usage predictable.

Senior vs. Staff Answers

A senior-level answer will correctly identify the core trade-offs—connection limits, load-balancing strategy, and basic failure handling. A staff-level answer goes further: it adds a concrete observability plan, discusses multi-region replication, and anticipates edge cases such as network partitions and graceful degradation. Staff candidates also talk about cost-aware scaling, e.g., using spot instances for non-critical shards, and they reference real-world patterns they have implemented.

How to practice this

  1. Study the worked design on WebSockets in the System Design AI library. It walks through a full-stack example with diagrams and notes.
  2. On the canvas, sketch your own architecture: choose a load balancer, decide on sharding, and add monitoring components.
  3. Use the AI coach to review your sketch and iterate on the answers you would give to each follow-up question.

Questions people ask

What is the difference between WebSocket and HTTP polling?

WebSocket opens a single persistent TCP connection that allows the server to push data instantly. HTTP polling repeatedly creates short-lived HTTP requests, causing higher latency and more overhead.

Can I use WebSockets with serverless functions?

Serverless runtimes like AWS Lambda don’t keep connections open, so you need a managed WebSocket service (e.g., API Gateway) that forwards events to Lambda only when needed.

How do I handle versioning of messages over WebSocket?

Add a version field to each message payload and design the client to ignore unknown fields. This lets you evolve the schema without breaking existing connections.

Is it safe to send large binary blobs over WebSocket?

It is safe, but you should split large files into smaller frames and implement back-pressure handling on both sides to avoid memory spikes.