Real-Time Communication
Deliver sub-second chat, live scores, notifications, and video calls with the right push transport.
In plain words
Real-time communication lets the server push updates to you the instant they happen, instead of your app asking "anything new?" over and over.
Think of it like…
Polling is checking your mailbox every 10 seconds; SSE/WebSockets is a live phone line; push fan-out vs. pull fan-out is mailing flyers to 10M people vs. putting a poster on a billboard.
Where you’ve seen it
In WhatsApp, a WebSocket connection stays open, so a friend’s message appears on your screen immediately.
Words you’ll hear
- Polling
- The client repeatedly asks the server for updates.
- Long polling
- The server holds the request open until there is something new.
- WebSocket
- A two-way connection that stays open.
- SSE
- Server-sent events: a one-way stream from server to browser.
Visual diagram
Theory & deep dive
Scaling stateful WebSockets across multiple servers
Unlike stateless REST servers, a WebSocket holds an open TCP connection in memory! If Alice is connected to WS-Gateway-1 and Bob is connected to WS-Gateway-2, WS-Gateway-1 cannot push a message straight to Bob’s phone.
The standard pattern is a Connection Registry + Pub/Sub (e.g., Redis Pub/Sub or Kafka): WS-Gateway-1 publishes Bob’s message to channel user:bob, and WS-Gateway-2 (subscribed to Bob) pushes it down Bob’s live socket.
How it works, step by step
Step 1 of 41. Pick the client-server transport
Use SSE for one-way server-to-client feeds (LLM tokens, live scores); WebSockets for bidirectional low-latency chat/collab; WebRTC for P2P/SFU audio/video.
- 1-Way Stream
- SSE
- Bi-Directional
- WebSockets
When to use it & trade-offs
What it is
Real-time communication replaces wasteful polling with persistent or push channels - SSE, WebSockets, WebRTC, and mobile push (APNs/FCM) - paired with scalable fan-out routing.
Why it matters: Naive HTTP polling every second wastes battery and bandwidth, while naive WebSocket fan-out explodes when 1M viewers watch one live stream.
When to reach for it
- Chat apps (WhatsApp/Slack): WebSockets + Redis presence + APNs/FCM offline push
- Live news feeds (Twitter/Instagram): Hybrid push-for-normals / pull-for-celebrities fan-out
- Group video conferencing: WebRTC with an SFU (Selective Forwarding Unit)
When to avoid or be careful
- Short-polling every 500 ms across millions of clients (kills servers and batteries)
- Fan-out-on-write for a user with 50M followers (triggers massive write amplification)
Real-Time Transport Options
Strengths: Standard HTTP, automatic reconnect, firewall-friendly, simpler than WebSockets.
Watch out: Unidirectional (server → client only); binary payloads need Base64.
Strengths: Full-duplex, tiny 2-byte frame overhead, instant bidirectional messaging.
Watch out: Stateful TCP connections complicate load balancing, draining, and horizontal scale.
Strengths: UDP media transport with NAT traversal (ICE/STUN/TURN) and SFU selective forwarding.
Watch out: Complex signaling and media server bandwidth costs for large rooms.
Interview signals & numbers to know
When this topic comes up
- real-time chat
- presence / typing indicator
- live scores
- celebrity fan-out
- video call
Benchmarks & rules of thumb
- WebSockets per tuned server
- 50k–250k concurrent
- Presence heartbeat TTL
- 30–60 seconds
- Celebrity fan-out threshold
- ~10k–100k followers
- SSE reconnect
- Built-in `Last-Event-ID`
Common mistakes & follow-ups
Common interview mistakes
- Storing WebSocket connections without an external
user → podmapping, making cross-user delivery impossible - Updating presence ("last seen") straight to SQL every 5 seconds instead of Redis with TTLs
- Using fan-out-on-write for celebrities, causing 30-second feed delivery lag
Likely interviewer follow-ups
- How do you gracefully deploy a WebSocket gateway holding 100,000 active connections without a thundering-herd reconnect?
- How does WhatsApp ensure message ordering and offline delivery across multiple devices?
- Explain why Twitter/Instagram uses a hybrid push+pull fan-out model.
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Real-Time Communication inside real High-Level Design prompts or review Q&A flashcards.