Guide

A chat system delivers messages between people in near real time, stores their history, and works when the recipient is offline. WhatsApp, Slack, Discord and iMessage differ in product, but they share one skeleton. A large real-time connection layer pushes messages to devices. Careful ordering and delivery keep conversations correct. Storage holds trillions of small messages without melting on hot channels.

The common interview mistake is to name Kafka and Cassandra first. Clarify the product first. Encrypted one-to-one chat and searchable workplace rooms share a skeleton, but they diverge sharply on fan-out, search and encryption.

Concepts on this topic

StepConceptWhat it answers
1Requirements and scaleWhat must the system do, and how big is it?
2Client message IDsHow do retries avoid duplicate messages?
3Presence and typingHow do best-effort signals stay cheap?
4Connection layerHow do gateways and the registry deliver to devices?
5Message orderingHow does every device show messages in order?
6Chat storageHow is history partitioned and retained?
7Large channels and receiptsHow does fan-out work for huge rooms?

Ask these first

Is it end-to-end encrypted? If yes, the server is a relay of ciphertext it can't read. Signal-style apps use the Signal protocol, so the server can't search message bodies or make rich previews. Group encryption adds complexity, and large open communities often skip it. Notifications then say "New message" unless the device decrypts locally.

Is history searchable? Workplace chat needs search, retention policies and exports. Consumer messengers may limit search when encryption is on.

Is the room small or large? That decides between per-member fan-out and a topic. See the large channels page.

Failure modes

FailureEffectMitigation
Gateway crashDropped connectionsClients reconnect. Registry entries expire. Resume from history
Hot channelLatency spikeSplit buckets, cache recent messages, use the large-channel path
Fan-out bus lagLate deliveryMonitor lag. Degrade typing and presence first. Never lose persisted messages
Push provider outageOffline users stay quietRetry with backoff. Show a badge on next open
Clock skew on IDsOdd orderingUse per-channel sequencers, or hybrid logical clocks, not wall clocks alone

Under a festival spike, shed typing and presence first, then delay receipts. Keep accepting and saving the message bodies.

Other parts

  • Push notifications for offline users go through the notification system. Collapse several messages into one push.
  • Media uploads straight to object storage with a presigned URL. The message holds the key.
  • Link previews run in a separate, rate-limited worker. Never fetch attacker-controlled URLs on the send path.
  • Moderation and retention run as background jobs that mark rows, and they rely on the deleted flag from the storage page.

Key takeaways

  • Split the connection layer, gateways plus registry, from durable message storage.
  • Persist, then ACK. Deliver at least once, and deduplicate with clientMsgId and messageId.
  • Order messages per conversation, not globally.
  • Treat large channels as a different fan-out problem from small groups.
  • Presence and typing are best-effort. Push covers offline users. Encryption changes what the server can see.

Further learning

Discord's post on storing trillions of messages is the best public storage case study for chat.

How Discord Stores Trillions of MessagesEngineer Bo Ingram shares insight into how Discord shoulders its traffic and provides a platform for our users to communicate.discord.com

Slack's real-time messaging architecture explains gateway and channel fan-out in a workplace chat product.

Real-time Messaging (Slack Engineering)Did you know that ground stations transmit signals to satellites 22,236 miles above the equator in geostationary orbits, and that those signals are then beamed down to the entire North American subcontinent? Satellite…slack.engineering

Gaurav Sen's walkthrough of a messaging design covers gateways, sessions, group fan-out and retries clearly.

WHATSAPP System Design: Chat Messaging Systems for InterviewsGaurav Sen