System DesignSep 30, 2026⏱️ 3 min read
Design a Real-Time Chat System: WebSockets, Presence Heartbeats & Message Sync

Design a Real-Time Chat System: WebSockets, Presence Heartbeats & Message Sync

Why Chat Breaks Traditional Stateless HTTP

Chat applications (1-on-1 messaging, group chats, online indicators, multi-device sync) differ fundamentally from REST APIs: the server must push incoming messages to recipients in milliseconds over long-lived stateful connections.
Real-Time Chat System Architecture

Real-Time Chat System Architecture


1. Protocol Choice: HTTP Polling vs. SSE vs. WebSockets

  • Long Polling: Client opens HTTP request and waits; server holds connection until a message arrives, then closes and client immediately reconnects. Wastes HTTP overhead when idle.
  • Server-Sent Events (SSE): Great for server-to-client streams (like LLM token streaming), unidirectional only.
  • WebSockets (wss://): Full-duplex, bidirectional persistent TCP channel after an initial HTTP Upgrade handshake. A single tuned server (Go/Elixir/Rust/C++) easily holds 500K+ open WebSocket connections.
Note: Keep stateless HTTP APIs for login, profile updates, and media uploads; reserve stateful WebSocket Chat Servers strictly for message delivery and presence.

2. Routing Messages Across Stateful Chat Servers

When User A (connected to Chat Server 1) sends a message to User B (connected to Chat Server 3), how does Server 1 locate Server 3?
  1. Service Discovery (ZooKeeper / etcd): Assigns each client the best regional WebSocket server at login (Presence/Session map: user_id -> chat_server_ip).
  2. Message Sync & Pub/Sub Relay:
    • User A sends payload { msg_id (Snowflake), channel_id, text } to Chat Server 1.
    • Chat Server 1 persists the message to the Message KV Store and publishes it to a Message Sync Queue / Pub-Sub (Kafka or Redis Pub/Sub).
    • If User B is online (Server 3), Server 3 pushes the frame immediately over User B's open WebSocket.
    • If User B is offline, the notification pipeline routes to APNs / FCM Push.

3. Storing Billions of Messages: Why Wide-Column NoSQL Wins

Assuming 50M DAU × 40 messages/day $\approx 2\text{ billion messages/day}$ (~100 GB/day).
  • Access Pattern: Users almost exclusively read recent messages by channel_id or search exact messages, never cross-table SQL JOINs.
  • Database Choice: Apache Cassandra or HBase (like Discord/Slack), partitioned by channel_id and clustered by message_id (a Snowflake 64-bit ID!).
    • Partition key channel_id keeps an entire conversation localized; clustering key message_id enforces chronological ordering without locking across servers!

4. Multi-Device Sync & Online Presence Indicators

Multi-Device Sync (cur_max_message_id)

Each device (phone, laptop) tracks cur_max_message_id locally. On reconnect, it requests all messages in channel_id with message_id > cur_max_message_id from the KV store—guaranteeing zero missed messages.

Taming Presence Fanout Storms

Do NOT publish "User logged in" immediately on brief mobile network drops!
  • Clients send lightweight heartbeat pings every 5 seconds; if no heartbeat arrives within 30 seconds, mark offline.
  • Use Pub/Sub between a user's presence channel and online friends—for massive group servers (100K+ Discord members), fetch presence lazily on scroll instead of broadcasting every online/offline flip to 100K sockets.

Share this article

Comments