| Users | Connections | Message Rate | Latency | Infrastructure Changes |
|---|---|---|---|---|
| 100 | ~100 concurrent | Low (few msgs/sec) | <100ms | Single server with WebSocket support |
| 10,000 | ~10,000 concurrent | Moderate (hundreds msgs/sec) | <200ms | Load balancer + multiple app servers + Redis pub/sub |
| 1,000,000 | ~1M concurrent | High (thousands msgs/sec) | <300ms | Clustered message brokers (Kafka, Redis Cluster), sharded app servers, CDN for static content |
| 100,000,000 | ~100M concurrent | Very High (millions msgs/sec) | <500ms | Global distributed clusters, edge computing, advanced partitioning, multi-region data centers |
Real-time features in HLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the application server's ability to maintain concurrent connections. Real-time features rely on persistent connections like WebSockets, which consume server memory and CPU. Around 5,000 concurrent connections per server is typical. Beyond this, servers struggle to keep connections alive and process messages quickly.
- Horizontal scaling: Add more app servers behind a load balancer to distribute connections.
- Message brokers: Use systems like Redis Pub/Sub, Kafka, or MQTT brokers to handle message distribution efficiently.
- Caching: Cache frequent data to reduce backend load.
- Sharding: Partition users or channels across servers to limit connection and message load per server.
- CDN and edge computing: Offload static content and some processing closer to users to reduce latency and bandwidth.
- Connection multiplexing: Use protocols like HTTP/2 or WebTransport to optimize connection usage.
- At 10,000 users with 1 message per second: 10,000 messages/sec to handle.
- Each message ~1KB -> 10MB/s bandwidth needed.
- Storage depends on message retention; 1 day of messages at 10,000 msgs/sec = ~864GB.
- Network bandwidth per server limited to ~1Gbps (~125MB/s), so multiple servers needed.
- CPU and memory scale with connection count; 1 server ~5,000 connections.
Start by defining the real-time feature and expected load. Identify the main challenges: connection management, message throughput, and latency. Discuss bottlenecks in servers and network. Propose scaling steps: horizontal scaling, message brokers, caching, and sharding. Always mention trade-offs and monitoring needs.
Your database handles 1000 QPS. Traffic grows 10x. What do you do first?
Answer: Introduce read replicas and caching layers to reduce load on the primary database before scaling vertically or sharding.
Practice
Solution
Step 1: Understand real-time communication needs
Real-time apps require a protocol that supports two-way, instant data exchange.Step 2: Identify protocol features
WebSocket allows full-duplex communication over a single connection, unlike HTTP/1.1 which is request-response only.Final Answer:
WebSocket -> Option CQuick Check:
Real-time = WebSocket [OK]
- Confusing HTTP with WebSocket for real-time
- Choosing FTP or SMTP which are not real-time protocols
- Thinking HTTP/2 is the same as WebSocket
Solution
Step 1: Define roles in real-time messaging
Producers send data, consumers receive data, and brokers route messages between them.Step 2: Identify the distributor
The broker acts as the middleman ensuring messages reach the right consumers.Final Answer:
Broker -> Option AQuick Check:
Message routing = Broker [OK]
- Confusing producer as distributor
- Thinking consumer sends messages
- Assuming database handles message routing
Solution
Step 1: Analyze message flow in real-time chat
The server receives and then sends the message to all connected users, requiring CPU and network resources.Step 2: Identify bottleneck
Server CPU handles message processing; network bandwidth handles sending to many users simultaneously.Final Answer:
Server CPU and network bandwidth -> Option DQuick Check:
Scaling real-time = Server resources [OK]
- Blaming client device speed for server load
- Focusing on database latency which is less critical here
- Ignoring network bandwidth limits
Solution
Step 1: Understand MQTT broker role
The broker routes messages; if overloaded, it queues or drops messages causing delays.Step 2: Evaluate other options
Clients using WebSocket instead of MQTT would cause connection issues, not delays; small messages send faster; disabled notifications affect display, not delivery.Final Answer:
Broker is overloaded and dropping messages -> Option BQuick Check:
Delays usually mean broker overload [OK]
- Blaming client protocol mismatch for delays
- Assuming small messages cause delays
- Ignoring broker capacity limits
Solution
Step 1: Understand scalability needs
Millions of users require distributed systems to handle load and maintain low latency.Step 2: Evaluate options
Distributed brokers with topic partitions allow parallel processing; WebSocket supports instant push updates. Polling and email cause delays and high load.Final Answer:
Use a distributed message broker cluster with topic partitions and WebSocket connections -> Option AQuick Check:
Scalable real-time = Distributed broker + WebSocket [OK]
- Choosing polling which wastes resources and adds latency
- Using email which is not real-time
- Relying on a single database causing bottlenecks
