design·lab

System design · application

Design: Realtime Chat

A chat room combines a persistent-connection gateway, cross-server fan-out, and durable storage into one system.

🧩 Requirements

What a realtime chat room demands

  • Realtime delivery — every message reaches everyone in the room instantly, not on the next poll.
  • Many concurrent connections — thousands of users sit idle in rooms, waiting.
  • Persistence + history — new joiners need to see what was already said.
  • Presence — who's online, who's typing, who just left.
  • Multiple server instances — one process can't hold every socket; two users in the same room may land on different servers.
Two hard problems hide here: the server must push, not wait to be polled — and a message from a user on Server A must still reach a user connected to Server B.
✓ Big picture

The 10,000-ft view — one message, fanned to everyone

Before the details: here's the whole system in one glance. Follow the numbers left → right.

🏗️ Architecture

Gateway + broker + queue

Clients hold a persistent WebSocket to a Gateway; the event loop lets one thread juggle thousands of idle sockets, non-blocking. A sent message publishes onto a Pub/Sub topic per room — every gateway instance subscribed to that room gets it and fans out to its own connected members. A queue durably stores the message and drives async notifications.

broker.publish(
  'room:42', msg);
// every gateway subscribed to
// room:42 gets it → fans out
// to its own connected sockets
Client A
WebSocket
Gateway A
event loop
↓ publish
Broker
topic: room:42
↓ fan-out
Gateway A, B, …
All room members
The broker also feeds a queue for persistence + notifications — watch it in step 3.
▶️ See it live

One message, delivered everywhere

User A and User B share room 42 but are connected to different gateway instances. Play through the sequence and watch the message fan out through the broker to both.

Follow the numbered messages top-to-bottom — the broker delivers to both gateways (steps 3–4) before either renders the message to its user.

✓ How it combines

Three primitives, one chat room

  • WebSocket gateway + event loop — thousands of idle sockets parked on one non-blocking thread per gateway instance.
  • Pub/Sub for cross-instance fan-outObserver at scale: every gateway subscribes to the rooms it has members in.
  • Queue for durable history + async notifications — chat history survives a gateway restart; push notifications fire without blocking the send path.
  • Horizontal scaling — add gateway instances freely; the broker, not direct socket references, keeps everyone in sync.
🎯 Combines: event loop (connection concurrency) + pub/sub fan-out (Observer at scale) + message queue (persistence & notifications).