Skip to main content

Chat Platform

1. Executive Summary​

A chat platform enables real-time and offline messaging between users in 1:1 and group conversations with delivery guarantees, ordering per conversation, presence, read receipts, and media attachments. Principal design centers on connection management at scale, message fan-out, per-conversation ordering, sync for offline clients, and storage tiering for message history.

This chapter designs a WhatsApp/Slack-class system for 500M MAU with billions of messages daily, emphasizing WebSocket gateways, partitioned message logs, and conflict-free offline sync. All ten system-design template phases are woven through the 30-section structure, with hybrid fan-out as the central scalability mechanism and durable-write-before-ACK as the central correctness invariant.

2. Why This Topic Matters​

Chat combines real-time systems, storage, and mobile constraints. Principal interviews probe:

  • WebSocket scaling — sticky sessions, connection registries.
  • Ordering — per-channel sequence vs. global clocks.
  • Group fan-out — write amplification in large channels.
  • Offline sync — catch-up APIs, gap repair.
  • End-to-end encryption — key management (optional scope).

Production failures include message loss perception, duplicate messages, presence storms, and hot partitions in celebrity group chats. Principal loops often spend 15+ minutes on fan-out and connection scaling alone—candidates who jump to database schema without clarifying group size distribution and online ratio typically underperform. E2E encryption and search are common scope extensions that should be acknowledged even when deferred. This chapter satisfies principal-level depth across all 30 standard sections.

3. Problems Being Solved​

ProblemCapability
Real-time deliveryWebSocket push to online users
Offline deliveryPersistent store + push notification
Group chatFan-out or fan-in storage models
OrderingMonotonic sequence per conversation
History syncPaginated fetch since sequence
PresenceOnline/typing/last seen
MediaObject storage + CDN URLs
Read receiptsPer-user cursor per conversation

4. Assumptions and System Model​

Phase 1: Clarify Requirements​

Functional:

  • Send/receive text, images, files in 1:1 and groups up to 500 members.
  • Message history query with cursor pagination.
  • Typing indicators and online presence (best-effort).
  • Delivery and read receipts.
  • Mobile and web clients; multi-device sync.

Non-functional:

  • Delivery latency p99 < 500 ms for online recipients.
  • Durability: messages never lost after server ACK.
  • Availability 99.95%.
  • 500M MAU; 50B messages/day.

Non-goals: E2E encryption (mention as extension); voice/video (separate SFU).

AssumptionImplication
Per-conversation ordering sufficientSequence number per channel
Most groups smallFan-out on write viable for <100
Large channels rareFan-out on read for megagroups
Clients reconnect oftenSync API critical

5. Essential Terminology​

TermDefinition
Conversation / channelThread of messages between participants
Sequence numberMonotonic ID per conversation
InboxPer-user view of conversations
Fan-out on writeCopy message to each member's inbox
Fan-out on readStore once; merge on read
Connection registryMaps user_id → WebSocket server
Sync cursorLast seen sequence per device
CRDTConflict-free replicated data type (optional edits)
APNs/FCMMobile push for offline

6. Core Mechanism​

6.1 Phase 5: High-Level Architecture​

Figure 1: Chat platform—WebSocket gateways for realtime; Kafka for async fan-out; Cassandra for message persistence.

6.2 Phase 3: Define APIs​

REST/gRPC:

POST /v1/conversations/{id}/messages { body, client_msg_id, attachments }
GET /v1/conversations/{id}/messages?after_seq=100&limit=50
GET /v1/sync?since=cursor
POST /v1/presence { status: online|away }

WebSocket frames:

{ "type": "message", "conversation_id", "seq", "body", "sender_id", "ts" }
{ "type": "ack", "client_msg_id", "server_seq" }
{ "type": "typing", "conversation_id" }

6.3 Phase 4: Model Data​

messages (Cassandra, partition by conversation_id):

conversation_id, seq (clustering), message_id, sender_id, body, created_at, client_msg_id.

user_inbox (fan-out on write for small groups):

user_id, conversation_id, last_seq, last_preview, updated_at.

conversation_members: conversation_id, user_id, role, joined_at.

read_cursors: user_id, conversation_id, last_read_seq.

connection_registry: user_id → {gateway_id, device_id} TTL 60s heartbeat.

Sequence allocation: Per-conversation counter in Redis or embedded in partition leader.

6.4 Phase 6: Deep Dives​

Send message flow:

  1. Client POST or WS send with client_msg_id (idempotency).
  2. Message service allocates seq atomically per conversation.
  3. Persist to Cassandra; publish to Kafka topic conv.{hash}.
  4. Fan-out worker: for each member, update inbox; push via connection registry.
  5. Gateway delivers WS frame; ACK with server_seq.
  6. Offline users: enqueue push notification via notification platform.

Large group (>100 members): Fan-out on read—store single message row; recipients pull on sync. Typing indicators sampled/disabled.

Multi-device sync: Each device maintains cursor; sync API returns delta across all conversations.

Figure 2: Message send with durable write before ACK—fan-out async to other participants.

Ordering: Single writer per conversation partition guarantees monotonic seq. Cross-conversation order irrelevant.

Presence: Heartbeat every 30 s; Redis SET with TTL; gossip optional for gateway-local cache. Typing events rate-limited 1/2s per user.

6.5 Media messages​

Client uploads to presigned S3 URL; message body contains media_ref. Virus scan async; thumbnail generation pipeline.

7. Step-by-Step Walkthrough​

7.1 1:1 message delivery​

  1. Alice sends "hello" with client_msg_id=uuid-1.
  2. Server assigns seq=42, stores, ACKs Alice all devices.
  3. Bob online on gateway G2; push frame within 200 ms.
  4. Bob offline: message stored; FCM push "New message from Alice".

7.3 Large channel message (fan-out on read)​

  1. 50K-member community channel; admin posts announcement.
  2. System stores single message row; no inbox fan-out.
  3. Members on sync fetch conversation log; rank by seq.
  4. Push notification sent to "mentions" only—not all 50K.
  5. Latency: active readers see message within 1 s via WS to online subset.

7.4 Multi-device read receipt conflict​

  1. Alice reads on phone (cursor 100); tablet still at 90.
  2. Read receipt broadcasts max cursor per user across devices.
  3. Sender sees "read" when any device reaches seq—product policy documented.

7A. Design Phase Summary​

PhaseSectionKey decisions
Requirements§41:1 + group, receipts, multi-device
Scale§10115K msg/sec; hybrid fan-out
APIs§6.2WS + REST sync
Data model§6.3messages by conversation_id
Architecture§6.1Gateway → Message → Cassandra
Deep dives§6.4seq; durable ACK
Reliability§8–9quorum; idempotent send
Security§13membership ACL
Operations§12gateway drain
Tradeoffs§16fan-out write vs read

8. Invariants and Guarantees​

PropertyGuarantee
Durability after ACKMessage persisted before client ACK
Per-conversation total orderMonotonic seq
Idempotent sendSame client_msg_id → same server message
At-least-once deliveryWS may retry; client dedup by message_id
PresenceBest-effort; not contractual

9. Failure Scenarios​

ScenarioMitigation
Gateway crashClient reconnect; sync gap
Kafka lagScale consumers; backpressure
Hot conversationPartition; fan-out on read
Sequence collisionSingle sequencer per conv
Split brain gatewaySticky user routing; registry TTL
Cassandra node downQuorum read/write CL=LOCAL_QUORUM
Duplicate client retryIdempotent client_msg_id

10. Performance Characteristics​

Phase 2: Estimate Scale​

500M MAU, 20 msg/day average → 10B msg/day ≈ 115K msg/sec average
Peak 5× → 575K writes/sec
Storage: 10B × 500 bytes ≈ 5 TB/day raw (compression, TTL policies)
WebSocket connections: 50M concurrent peak (10% online)
Gateways: 50M / 50K per instance ≈ 1000 gateway instances
PathLatency target
WS delivery< 500 ms p99
History page< 200 ms
Media uploadSeconds (async)

11. Scalability Limits​

LimitMitigation
Celebrity group fan-outFan-out on read
Connection memoryHorizontal gateways
Inbox write ampHybrid model by group size
Global sequencePer-conversation only

Figure 3: Hybrid fan-out strategy by conversation size.

12. Operational Considerations​

Phase 9: Operations​

  • SLO: message delivery p99; sync API availability.
  • Metrics: WS connections, Kafka lag, send error rate, push latency.
  • Runbooks: drain gateway; hot conversation throttle; disable typing in incident.
  • Load test: synthetic WS connections; chaos gateway kill.

13. Security Considerations​

Phase 8: Security​

  • Auth: OAuth tokens; WS upgrade validates JWT.
  • Authorization: membership check before read/write.
  • Abuse: rate limit sends; report/block users.
  • Media: signed URLs; malware scan.
  • E2E extension: Signal protocol; server stores ciphertext only.

14. Cost Considerations​

WebSocket fleet is significant compute. Cassandra storage grows with retention policy (messages forever vs. 1 year). Push notification costs per offline user. CDN for media egress.

15. Production Implementations​

SystemPattern (from public sources)
WhatsAppErlang connection layer; custom protocol; emphasis on delivery and encryption
SlackChannel sharding; separate search and file services
DiscordLarge guild optimization; fan-out on read for megaservers
SignalE2E by default; minimal server metadata

Implementation choice: Self-host message store when data residency or cost at billions of messages/day; managed realtime (PubNub, Ably) for faster MVP with vendor lock-in tradeoff.

14A. Cost Considerations​

WebSocket gateway fleet: estimate 50K connections per instance (varies by heartbeat rate and memory). Message storage: Cassandra at 5 TB/day requires compaction and TTL strategy. Push notifications: FCM free; APNs infrastructure cost indirect. Principal review should separate connection COGS from storage COGS from push COGS.

21B. Extended Follow-Ups​

  1. Message search. — Async index to Elasticsearch; privacy filtering; not on hot path.
  2. Ephemeral messages. — TTL per message; compaction job; screenshot policy is legal/product not technical.

16. Alternatives and Tradeoffs​

Phase 10: Tradeoffs​

ModelProsCons
Fan-out on writeFast readWrite amp in large groups
Fan-out on readCheap writeSlow read for active users
MQTT vs WebSocketMobile efficiencyEcosystem
SQL vs CassandraACIDWrite scale

17. Common Misconceptions​

MisconceptionReality
"Global message order"Per-conversation suffices
"WebSocket alone delivers"Need persistence + push
"ACK after broadcast"ACK after durable write only
"One inbox table for all"Partition by user_id
"Delivery receipt means read"User may have notifications disabled
"CRDT required for all chat"Last-write-wins often sufficient for v1
"Push replaces persistence"Offline users need durable store
"Group voice is just more messages"SFU is separate media plane

17A. Failure scenario drill​

Gateway deploy kills 10% of connections—clients reconnect with exponential backoff. Without sync API, users perceive message loss. Strong answer: durable write before ACK + sync on reconnect + push for offline. Quantify reconnect storm: 5M users × simultaneous reconnect = thundering herd on auth—stagger JWT refresh.

17B. Additional misconceptions​

MisconceptionReality
"Sequence numbers globally ordered"Per-conversation only
"Kafka ordering across all chats"Partition per conversation required

18. Principal Architect Perspective​

  • Durable write before ACK—non-negotiable for trust.
  • Hybrid fan-out by group size is production norm.
  • Connection layer scales independently from message logic.
  • Sync API is as important as realtime path for mobile.
  • Idempotency on client_msg_id mandatory.
  • Regulatory retention policies (GDPR delete) conflict with message forever—negotiate TTL with legal early.
  • Gateway fleet is often largest COGS after storage—optimize connection memory before optimizing Cassandra.

18.1 Multi-team boundaries​

Chat platforms split across connection team (gateways), messaging team (persist + fan-out), notifications team (offline push), and client SDK team. Principal architects define API contracts between teams—especially ACK semantics and sync cursor format—before parallel development. Conway's law: avoid merging gateway and message logic into one deployable if teams scale independently.

19. Architecture Review Exercise​

Scenario: Fan-out on write for all groups including 100K member channels.

Review: Write amp 100K per message; calculate Kafka and DB load; propose threshold switch to fan-out on read.

20. Whiteboard Explanation​

"Clients connect via WebSocket gateways registered in Redis. Sending a message hits the message service which assigns a per-conversation sequence, writes Cassandra, then ACKs. Kafka consumers fan out to recipient inboxes and push to their gateways. Offline users get mobile push. Small groups use fan-out on write; large channels store once and read on sync. Presence is heartbeat-based best-effort. Principal non-negotiable: no ACK before durable write—user trust depends on it."

21. Interview Questions​

  1. Design WhatsApp for 500M users. — Signals: WS scale, persistence, fan-out hybrid. Red flags: polling MySQL.
  2. WebSocket scaling approach? — Signals: gateway fleet, connection registry, sticky routing. Follow-up: cross-gateway push.
  3. Fan-out on write vs read? — Signals: group size threshold, write amp math. Red flags: one model for all.
  4. Message ordering guarantees? — Signals: per-conversation seq, single partition. Red flags: global timestamp order.
  5. Offline message sync? — Signals: cursor API, gap repair. Red flags: "client polls all."
  6. Group chat with 100K members? — Signals: fan-out on read, disable typing. Red flags: 100K inbox writes.
  7. Read receipts implementation? — Signals: per-user cursor, broadcast policy. Follow-up: multi-device.
  8. Idempotent message send? — Signals: client_msg_id unique constraint. Red flags: no dedup.
  9. Multi-device consistency? — Signals: sync all devices, max read cursor. Red flags: per-device silos only.
  10. Storage choice for messages? — Signals: Cassandra partition by conversation. Red flags: normalized SQL only.
  11. Typing indicator scale? — Signals: rate limit, best-effort drop. Red flags: reliable typing queue.
  12. Media attachment flow? — Signals: presigned upload, async scan. Follow-up: thumbnail pipeline.

22. Interview Follow-Ups​

  1. Edit/delete message. — Tombstone flag; propagate update event.
  2. E2E encryption. — Key exchange; server blind storage.
  3. Search across messages. — Separate Elasticsearch index async.

22A. Production Low-Level Design (Interview + Game Day)​

Per Production LLD and Game Day Standard.

C4 context​

Step-by-step flow:

StepInteractionExplanation
1–3IngestACK only after durable seq assigned
4–5Fan-outWrite path for small groups; read for large
6DeliverWS if online; push if offline

Production stack​

LayerAWS / genericPurpose
EdgeNLB + WS gatewaysSticky / connection registry
MessagesECSSeq allocation + ACL
StoreCassandra / DynamoPartition by conversation_id
Fan-outMSK + workersHybrid push/pull
PresenceRedisHeartbeats; ephemeral
MediaS3 + presignBytes off WS path

API contract (snippet)​

WS send: {"type":"msg","client_msg_id":"c1","conv_id":"cv","body":"hi"}
← {"type":"ack","client_msg_id":"c1","seq":1042,"message_id":"m…"}

GET /v1/conversations/{id}/sync?after_seq=1000
→ {"messages":[…],"read_cursors":[…]}

Data model​

EntityKeyNotes
messages(conv_id, seq)Ordered log
user_inbox(user_id, score)Fan-out on write
membership(conv_id, user_id)ACL
read_cursors(conv_id, user_id)Receipts

Happy-path sequence​

Failure / gateway kill mid-send​

SLOs (one-liner)​

SLOTargetHow achieved
Send ACK p99< 200 msLocal quorum write
Online deliver p99< 500 msKafka + GW push
DurabilityNo loss after ACKPersist before ACK
Fan-out lag large groupsN/A pushFan-out on read

22B. Game Day Playbooks​

Catalog​

#DrillFrequencySLO
GD-1Kill gateway mid-sendMonthlyIdempotent retry
GD-2Cassandra quorum lossQuarterlyNo false ACK
GD-3Kafka fan-out lagQuarterlyOnline deliver SLO
GD-4Megagroup spamQuarterlyWrite amp bounded
GD-5Presence Redis stormSemi-annualHeartbeat shed
GD-6Region drainAnnualSticky migrate

GD-1 — Idempotent send​

GD-4 — Megagroup write amp​

Pass criteria: O(1) writes per message; typing disabled; no Redis melt.

23. Strong Answer Example​

Question: "Fan-out on write vs read for groups?"

Answer this as a 45-minute design discussion using the PRACTICE framework. Strong students show hybrid fan-out by group size, durable ACK before client ack, and per-conversation seq—not “poll MySQL every second.”

Figure A: PRACTICE map — walk the board in this order.


Student interview script (PRACTICE)​

P — Problem framing​

"Fan-out on write copies each message into every member inbox—fast reads, O(members) writes. Fine for DMs/small groups. At 100K members, write amp explodes.

Fan-out on read stores one conversation log; members pull on open—cheap writes, heavier reads.

Short answer: Hybrid—push fan-out below ~100 members; pull above; megagroups disable typing and broadcast push."

R — Requirements​

Realtime + offline sync; receipts; media; ordering per conversation.

A — Assumptions​

Per-conv seq is enough (not global order); presence best-effort; E2E optional extension.

C — Capacity​

500M × 20 msg/day ≈ 115K writes/sec avg
WS: 10% online → 50M conns → ~1000 GW pods @ 50K each

T — Topology​

GW → Message Svc → store + Kafka fan-out; registry for online push; sync cursor API.

I — Invariants​

ACK only after durable write; client_msg_id idempotent; membership ACL on send/sync.

C — Constraints​

Multi-device max read cursor; media via presign; rate-limit typing.

E — Evolution​

1:1 → small groups push → threshold hybrid → push offline → E2E.

Closing line:

"Hybrid fan-out by size, durable ACK, per-conversation seq, sync for gaps—never poll MySQL for chat."

Scoring signals​

StrongWeak
Hybrid thresholdOne model for all
Durable before ACKMemory-only GW
Seq + idempotencyGlobal timestamp order

Compact outline​

"Push <100; pull for large; persist then ACK; client_msg_id; cursor sync."

24. Weak Answer Example​

Weak: "Store messages in MySQL and poll every second."

Red flags: No realtime, no ordering, no scale, polling waste.

25. Hands-On Exercise​

  1. Build WS server + REST send API.
  2. Implement per-conversation seq in SQLite.
  3. Simulate fan-out on write to inboxes.
  4. Add sync endpoint with cursor.
  5. Extension: Threshold-based fan-out (switch at N members).
  6. Extension: Chaos test—kill gateway mid-send; verify client retry idempotency.

23A. Additional Strong Answer​

Q: When does client receive ACK?

Outline: Only after durable write to message store with assigned seq. Never ACK on WS receive before persistence. If Kafka fan-out fails after persist, message is safe; async repair delivers. ACK includes server_seq and message_id for client dedup across devices.

19A. Extended Review Scenario​

Scenario B: Messages stored only in WebSocket gateway memory.

Review: Gateway restart causes data loss; propose durable store before ACK. Offline users cannot recover messages.

28B. Extended BOE Walkthrough (Interview Script)​

Interviewer: "500M MAU chat, 20 messages per day."

Strong candidate:

"500M × 20 = 10B messages/day ≈ 115K writes/sec average, 5× peak ≈ 575K writes/sec. Cassandra partition by conversation_id keeps writes localized. Storage: 10B × 500 bytes ≈ 5 TB/day—need TTL or tiering for old messages unless 'forever' is explicit requirement.

WebSocket: assume 10% concurrent online → 50M connections. At 50K per gateway pod → 1000 pods globally—connection layer is major COGS line item.

I'll propose hybrid fan-out: write for <100 members, read for larger. ACK only after durable write. Offline sync via cursor API—mobile reliability depends on this as much as WS.

For principal scope I'll mention E2E as extension and multi-region: users expect regional latency <500ms; replicate conversation metadata; messages follow user residency policy if compliance requires."

26. Knowledge Check (extended)​

  1. What triggers fan-out on read vs write?
  2. Why heartbeat for presence?
  3. How many Redis ops/sec for 50M presence heartbeats at 30s interval?
  4. What is LOCAL_QUORUM protecting in Cassandra?

27. Flashcards​

FrontBack
Fan-out on writePre-distribute to member inboxes
client_msg_idClient idempotency key
Sync cursorLast seen seq for catch-up
LOCAL_QUORUMCassandra consistency for replica set
Fan-out thresholdMember count switching write vs read
MegagroupChannel too large for inbox fan-out
Durable ACKPersist before client acknowledgment
Gap repairSync API fills missing seq range
Media presignDirect upload to object storage
Tombstone messageDeleted message marker for sync
WS sticky routingSame user to same gateway when possible
Push payloadMinimal data when app backgrounded
Conversation partitionCassandra shard key for message locality
Sync gapMissing seq range fetched on reconnect

28. Cheat Sheet​

REQUIREMENTS: 1:1 + group, realtime + offline, receipts, media
SCALE: 115K+ msg/sec; 50M WS connections
APIs: WS frames + REST sync/history
DATA: messages by conversation_id; user_inbox; read_cursors
ARCH: Gateway → Message Svc → Cassandra + Kafka fanout
DEEP: seq per conv; hybrid fan-out; durable write before ACK
RELIABILITY: quorum writes; idempotent send
SECURITY: membership ACL; rate limits
OPS: gateway drain; Kafka lag alerts
TRADEOFFS: fan-out write vs read; WS vs push-only

28A. Principal Interview Deep Dive​

Connection layer sizing​

Target: 50M concurrent WebSocket connections
Per gateway pod: 50K connections (2GB RAM rule of thumb)
Pods: 50M / 50K = 1000 gateway pods
Cross-AZ: distribute 3 AZs → ~334 pods per AZ
Heartbeat: 50M × 1/30s ≈ 1.6M heartbeat writes/sec to registry → shard Redis heavily

Consistency model summary​

DataConsistencyUser-visible effect
Message after ACKStrong per conversationNo lost sent messages
Read receiptEventualSlight delay OK
PresenceBest-effort"Online" may lag
TypingEphemeral dropAcceptable loss

E2E encryption extension (interview bonus)​

Signal Protocol: X3DH key agreement, Double Ratchet for forward secrecy. Server stores ciphertext + minimal metadata (sender, recipient, timestamp). Push notifications contain no message body. Tradeoff: search, moderation, and compliance harder—legal must sign off.

Group size policy table​

MembersFan-outTypingPush to all
2–100WriteYesYes
101–10KReadSampledMentions only
10K+ReadOffAdmin pin only

Document thresholds in platform SLO doc; tune from metrics.

30. References​

  • Kleppmann, DDIA — ordering, partitioning.
  • WhatsApp engineering blog posts — Erlang gateway scaling (implementation anecdotes).
  • RFC 6455 — WebSocket protocol.

Distinction: Per-conversation ordering is design choice; WebSocket protocol is standardized.

30A. Further reading paths​

Study Message Delivery Semantics alongside this chapter. Compare fan-out models with News Feed—same write amplification problem, different product surface. For offline push integration see Notification Platform. Hands-on milestone: WS server with durable SQLite backing and measured reconnect sync latency under 500 ms p99.