Loading...

A 20-minute mock system design interview. You drive the pace. Jump to the board when you are ready; skip capacity math if you want. The interviewer grades your thinking, not a template match.
Two shared surfaces (only one is on screen at a time; use the panel arrow to switch):
Build incrementally while you talk. Trace one message end to end through your diagram. Argue the tradeoff, not "industry standard."
Read this before you go in. These are the decisions, and why they matter.
Clients never talk peer to peer. Everything goes through a chat service that receives, routes, relays, and holds messages for offline users.
Polling. The client keeps asking "any new messages?" Mostly empty responses. Wasteful at scale.
Long polling. The server holds the request until a message arrives or it times out. Better, but sender and receiver can land on different servers, disconnects are hard to detect, and quiet users still cycle connections.
WebSocket. Client-initiated, persistent, bidirectional. Starts as HTTP, upgrades, works on ports 80/443. Best default for send and receive. Login, signup, and profile stay plain HTTP.
WebSocket is not a rival to TCP. It is a persistent TCP connection with a thin handshake and framing layer, which is why it keeps TCP's ordering and reliability. Facebook's real mobile client uses MQTT over TCP instead: smaller frames on lossy networks. That is equally defensible if you can say why.
Three kinds of pieces:
A and B are rarely on the same chat server. You need something that routes between servers (Kafka, a pub/sub topic, Redis pub/sub, RabbitMQ). Without that hop, "I sent hi" never reaches the other socket.
One server holding every connection is a starting sketch only. Call it out as a single point of failure, then go to a fleet.
Method: DAU × messages/day × bytes/message, then convert to per-second rates. Rough numbers are fine; the method is what gets graded.
For this interview, a sound landing is:
Chat is read/write ≈ 1:1 (not a read-heavy feed). Group fan-out is O(n) per member.
Two data types, two stores:
Partition so a user's (or a channel's) messages stay together: hash on user ID, or channel_id as the partition key for groups. Redis caches recent threads so opening a chat does not hit the database.
created_at is not enough. Two messages can share a timestamp. The ID must be unique and time-sortable.
Options:
1:1 table keyed by message_id. Group table keyed by (channel_id, message_id) with channel_id as the partition key.
User A → chat server → mint a message_id → persist to the KV store. If B is online, forward to B's chat server and push down B's socket. If B is offline, send a push notification; the message waits in the store until reconnect.
Usual contract: at-least-once delivery with per-conversation ordering. Persist before you ack.
Same account on a phone and a laptop. Each device stores cur_max_message_id. A message is new for that device if the recipient is the logged-in user and id > cursor. Each device pulls independently from the store, so both converge on the same history.
Cross-device history wants strong reads. Live delivery often prefers availability. Pick one, and say what you sacrificed.
For groups up to ~100, copy the message into each member's inbox (WeChat caps around 500 for this reason). Sync stays simple. It breaks at huge scale: a 100K-member group is a write fan-out explosion per message. If they ask about that, say you would stop copying per inbox and fan out on read, or cap group size.
Login writes online + last_active_at; logout writes offline. Naive online/offline on every TCP blip flaps the green dot (tunnels, flaky networks).
Heartbeat instead: client pings every ~5s; mark offline if none arrive for ~30s. Fan out via pub/sub, one channel per friend pair, for small graphs. For a huge group, do not publish 100K events per status change. Fetch lazily on group entry, list refresh, or viewport.
When a chat server holding tens of thousands of sockets dies, you do not fail over live TCP. Clients detect the drop and reconnect through discovery / the load balancer to a new server. Retry and queue so in-flight messages are not lost. Replicate stored history (copies, or erasure coding) so one node loss is not data loss.
Compression, object storage, thumbnails. The chat message stores a URL, not the file. Serve through a CDN. End-to-end encryption is a wrap-up topic, not required up front.
One complete design you can study, then rebuild in your own words. Do not recite it. In the interview, draw your graph and defend the choices.
How the graph works: clients never talk to each other. Each holds a sticky socket to one chat server. That server writes the message, then either pushes it to the recipient's server (if they are online on a different box) or hands it to push + the store (if they are not). Login and profile stay off this path.
Web Client, Mobile Client
→ Load Balancer
→ WebSocket Server fleet (mark distributed)
→ Chat Service
→ Cassandra (history)
→ Redis (recent threads)
→ Kafka / Pub-Sub Topic → peer WebSocket Servers → recipients
→ Blob Store + CDN (media URLs only)
→ Third-party API (APNs / FCM) for offline push
Components:
message_id, persist, publish for the other servers, ack the sender. Keep this as its own box (or name it on the chat server) so the walk is traceable.online, last_active). Opening a chat should hit cache, not the full history store. Heartbeats land here so the green dot does not flap.message_id (Snowflake-style, or a per-conversation counter).cur_max_message_id. A message is new on that device if id > cursor. Devices pull independently, so they converge.Group of ~100: copy the message into each member's inbox. Cheap here. A 100K-member group explodes write fan-out; cap size or switch to fan-out on read.
Presence: heartbeat every ~5s; offline after ~30s of silence. Do not flip the green dot on every TCP blip.
Server death: live TCP does not fail over. Clients reconnect via the load balancer / discovery to a new server. History is replicated so one node loss is not data loss.
Users(id, name, last_active)
Messages(id, sender_id, conversation_id, text, media_url, ts)
Conversations(id, name?)
ConversationUsers(conversation_id, user_id)
message_id(conversation_id, message_id) with conversation_id as the partition keyconversation_id or user ID; replicate 3×RabbitMQ or Redis pub/sub instead of Kafka. DynamoDB instead of Cassandra. Long-polling as a fallback on hostile networks. MQTT over TCP instead of raw WebSocket on mobile. What matters is that you can argue the tradeoff.