| Users | Connections | Server Load | Network Traffic | Data Storage |
|---|---|---|---|---|
| 100 users | ~100 connections | Single server handles all | Low, few messages/sec | Minimal, mostly ephemeral |
| 10,000 users | ~10,000 connections | Multiple servers with load balancer | Moderate, hundreds messages/sec | Some persistent logs |
| 1,000,000 users | ~1,000,000 connections | Many servers, horizontal scaling | High, thousands messages/sec | Large logs, analytics data |
| 100,000,000 users | ~100,000,000 connections | Massive cluster, sharding | Very high, millions messages/sec | Distributed storage, archiving |
WebSocket for real-time communication in HLD - Scalability & System Analysis
The first bottleneck is the WebSocket server's ability to maintain concurrent connections. Each server can handle roughly 1,000 to 5,000 concurrent WebSocket connections depending on hardware and software optimizations. Beyond this, CPU and memory usage spike, causing latency and dropped connections.
- Horizontal scaling: Add more WebSocket servers behind a load balancer to distribute connections.
- Connection sharding: Partition users by region or user ID to route connections to specific servers.
- Use message brokers: Employ systems like Redis Pub/Sub or Kafka to distribute messages between servers.
- Caching: Cache frequent messages or state to reduce backend load.
- CDN for static content: Offload static assets to CDN to reduce server bandwidth.
- Optimize protocols: Use binary frames and compression to reduce bandwidth.
- At 10,000 users sending 1 message/sec: 10,000 messages/sec to handle.
- Each message ~1 KB -> 10 MB/s bandwidth needed.
- Storage for logs: 10,000 messages/sec x 1 KB x 3600 sec = ~36 GB/hour.
- Network: 1 Gbps link can handle ~125 MB/s, so one server can handle this bandwidth.
- CPU: Each server can handle ~3,000-5,000 connections comfortably.
Start by explaining how WebSocket maintains persistent connections and why that limits concurrency per server. Then discuss how to horizontally scale servers and distribute connections. Mention message brokers for cross-server communication. Finally, talk about bandwidth and storage considerations. Keep answers structured: bottleneck -> solution -> trade-offs.
Your WebSocket server handles 1,000 QPS (messages per second). Traffic grows 10x to 10,000 QPS. What do you do first and why?
Answer: Add more WebSocket servers and use a load balancer to distribute connections and message load. This horizontally scales capacity to handle increased concurrent connections and message throughput.
Practice
WebSocket over traditional HTTP for real-time communication?Solution
Step 1: Understand WebSocket connection behavior
WebSocket establishes a persistent connection between client and server, unlike HTTP which opens and closes connections per request.Step 2: Identify the key benefit for real-time apps
This persistent connection allows instant two-way communication, ideal for real-time updates.Final Answer:
It keeps a persistent connection open for instant two-way data exchange. -> Option CQuick Check:
Persistent connection = Instant two-way communication [OK]
- Confusing WebSocket with HTTP polling
- Assuming WebSocket compresses all messages automatically
- Thinking WebSocket works without server support
Solution
Step 1: Recall WebSocket URL scheme
WebSocket URLs start withws://orwss://for secure connections, nothttp://.Step 2: Identify correct constructor usage
The WebSocket object is created withnew WebSocket(url), no.connect()or.open()methods exist.Final Answer:
const ws = new WebSocket('ws://example.com/socket'); -> Option BQuick Check:
WebSocket uses ws:// + new WebSocket() [OK]
- Using http:// instead of ws://
- Calling non-existent connect() or open() methods
- Omitting the new keyword
onMessage(client, message) {
broadcastToAllClients(message);
}What will happen if 1000 clients send messages simultaneously?
Solution
Step 1: Analyze server load with simultaneous messages
When many clients send messages at once, the server must process and broadcast each, increasing CPU and network load.Step 2: Understand server resource limits
Without scaling or message queuing, the server can get overwhelmed, causing delays or dropped connections.Final Answer:
Server may become overwhelmed, causing message delays or dropped connections. -> Option AQuick Check:
High load without scaling = delays/drops [OK]
- Assuming instant broadcast without delay
- Thinking clients queue messages automatically
- Believing only first message is processed
ws.on('message', (msg) => {
ws.send(msg);
});Why might this cause problems in a multi-client chat application?
Solution
Step 1: Understand the code behavior
The code listens for a message from a client and sends the same message back only to that client (ws.send(msg)).Step 2: Identify issue in multi-client context
Other connected clients do not receive the message, so chat messages are not broadcasted properly.Final Answer:
It only sends the message back to the sender, not to other clients. -> Option DQuick Check:
ws.send(msg) sends only to sender [OK]
- Assuming ws.send broadcasts to all clients
- Thinking event name 'message' is incorrect
- Believing connection closes after send
Solution
Step 1: Understand scaling challenges with many clients
A single server can become a bottleneck when handling thousands of WebSocket connections and broadcasting messages.Step 2: Identify role of message broker
A message broker like Redis Pub/Sub can distribute messages efficiently across multiple WebSocket servers, balancing load and improving scalability.Final Answer:
A message broker (e.g., Redis Pub/Sub) between WebSocket servers and clients. -> Option AQuick Check:
Message broker enables scalable message distribution [OK]
- Relying on a single server for all connections
- Switching to HTTP long polling loses real-time benefits
- Storing messages in DB adds latency, not scaling
Ready to prove it?
Pass the test and this pattern moves unknown
