Skip to content
Commitwise
Menu

Design a chat system like a messaging app with one-to-one and group chats.

System designSenior

In shortClients keep a persistent connection (WebSocket) to gateway servers; messages are written durably with a per-conversation sequence number, then fanned out to online recipients through a pub/sub layer and to offline ones through push notifications. Clients sync by asking for everything after their last sequence number.

Model answer

Connections: clients hold a WebSocket to a gateway tier (stateless apart from connections). A session registry maps user → gateway node(s), so messages can be routed to the right node.

Sending: the client creates a message with a client-generated id (for idempotent retries) and sends it. The message service assigns a per-conversation sequence number, stores it, and acknowledges. Ordering is by sequence number, never by device clocks.

Storage: a wide-column or key-value store partitioned by conversation id, clustered by sequence (Cassandra, ScyllaDB, DynamoDB): fast appends and range reads of recent history. Media goes to object storage via presigned uploads; messages only carry references.

Delivery: after storing, publish to a pub/sub layer; gateways holding recipients’ connections push the message. Offline recipients get a push notification. Receipts (delivered, read) are small messages flowing the other way, stored as the last-read sequence per user per conversation.

Sync: on reconnect, the client sends its last known sequence for each active conversation and receives everything newer, which also covers multiple devices.

Groups: small groups fan out on write; very large groups or channels fan out on read (members fetch from the conversation log) to avoid millions of writes per message.

Extras: presence with heartbeats and TTLs (eventually consistent), typing indicators as ephemeral, unstored events, rate limits and spam checks, and end-to-end encryption (Signal protocol), which moves search and moderation to the client.

What they're looking for

Connection management, message ordering and ids, durable storage partitioned by conversation, delivery and read receipts, offline sync, group fan-out trade-offs, push notifications, presence, and media handling. Bonus for end-to-end encryption awareness.

Common mistakes

  • Relying on timestamps for message order.
  • No idempotency for messages resent after reconnects.
  • Fanning out large group messages synchronously in the request path.

More system design questions

Esc

Type to search the whole site.