EN 中文

System Design Lab

A chat backend is a connection problem first: millions of live sockets must be tracked, then a message fanned out to everyone who should receive it.

Change concurrent connections, message rate, group size, the online ratio, history retention, devices per user, and regions. The design moves from a single socket server to a gateway fleet with a session registry, a routing and fan-out tier with a presence service, durable per-user inboxes, and finally cross-region routing with offline push.

Guided walkthrough

Reason about it one step at a time

Normal evolution scenarios

Click left to right for the intended demo path. Each card changes the workload inputs.

Workload

These are inputs, not preset architecture stages.

Recommended shape

Current architecture path
Real-time chat / messaging architecture diagram Whiteboard-style architecture diagram for a chat backend: clients on persistent sockets, a connection gateway fleet with a session registry, a routing and fan-out tier with a presence service, durable message and per-user inbox stores, and an async push tier for offline devices. Clients Connection gateway Routing + presence Message store Offline push Client persistent socket WS gateway holds sockets Session registry user to server Router / fan-out message to members Presence online + receipts Message store durable log Per-user inbox offline queue Push service wake devices
Clients
Client holds a long-lived WebSocket to send and receive messages in real time
Connection gateway
WS gateway terminates persistent connections and keeps each live socket in memory
Session registry maps each online user/device to the gateway node that holds its socket
Routing + presence
Router / fan-out expands a send to every group member and routes each to the right gateway
Presence tracks who is online and propagates delivered/read receipts
Message store
Message store persists every message so history survives restarts and late syncs
Per-user inbox queues messages per recipient device until it reconnects and pulls them
Offline push
Push service sends APNs/FCM notifications to wake offline devices so they sync

Bottlenecks

Connection memory

Message fan-out rate

Inbox + history storage

Presence + receipt cost

Cross-region routing

Why this changes

    Decision tradeoffs

    Connection / session layer

    Message routing

    Group fan-out

    Offline inbox

    Presence service

    End-to-end encryption

    Source-backed rules

    These are the durable system-design claims behind the model. The exact slider thresholds are deliberately labeled as teaching assumptions.

    Verified rule

    A WebSocket is a persistent, full-duplex connection the server must keep open

    Each chat client holds one long-lived connection, so the gateway tier is sized by how many open sockets it can keep in memory, not by request rate.

    MDN Web Docs
    Verified rule

    Publish/subscribe decouples senders from the many recipients of a message

    Fan-out to a group is naturally a pub/sub problem: one publish is delivered to every subscriber, which is how a router expands a send to many members.

    Google Cloud Pub/Sub
    Verified rule

    A durable message queue lets offline recipients receive messages later

    A per-user inbox queue holds messages for disconnected or multi-device users until each device reconnects and drains its queue.

    AWS SQS
    Verified rule

    End-to-end encryption means the server routes ciphertext it cannot read

    With E2EE the server can fan out and store messages but cannot inspect content, so server-side features must work on ciphertext and metadata only.

    Signal Protocol

    Teaching assumptions

    • Each live socket is charged a fixed memory budget (kernel plus app buffers plus session state); real costs vary by stack.
    • Fan-out rate is messages per second times average group size times devices per user; presence and receipts add return events on top.
    • Single-node gateway, routing, inbox-write, and presence budgets are conservative teaching numbers, not vendor limits.