Basics
Start with the product and the ideas it depends on. If these are clear, the design that follows will feel obvious rather than memorised.
1.1Product in plain words
Who uses it, what a normal day looks like, and why it is harder than it seems.
Chat inside a services marketplace: customers and providers message each other one-to-one or in small groups, share photos, and see when messages are sent, delivered and read. Think of WhatsApp built into the booking app.
Who uses it
- Customers: chat about a booking, often while travelling with patchy data.
- Providers: busy, frequently on low-end Android phones, replying between jobs.
- Support agents: join a conversation when something goes wrong.
A day in the life
- A customer opens the chat from her booking and types ‘Running 10 minutes late’.
- The message appears instantly with a clock, then one tick when the server has it.
- The provider’s phone is in his pocket with the app closed; he gets a push notification.
- He opens it; messages load from his phone first, then any missing ones arrive.
- Her screen shows two blue ticks the moment he has read it.
Why it is hard
Two people typing at once must see the same order on every device.
Phones switch networks every few minutes; nothing may be lost or sent twice.
Real-time delivery means keeping sockets open for every online user.
Notifications can be delayed or dropped; the app must still end up complete.
Like a post office that numbers every letter in a conversation. Anyone holding letters 1 to 40 who receives letter 43 knows exactly which two to ask for.
1.2Key concepts
The building blocks this design uses. Each one appears again in the answer.
A WebSocket keeps one open channel between phone and server, so new messages arrive in milliseconds instead of waiting for the app to ask.
Each conversation gets increasing numbers assigned by the server. Clients sort by them and notice gaps, which tells them a message is missing and must be fetched.
Sent means the server stored it, delivered means the recipient's device received it, read means it was shown. Each is a separate acknowledgement.
The sender creates an id before sending. Retries reuse it, so a reconnect never produces a duplicate bubble.
Copying a message into every member's inbox when it is sent (fan-out on write), or looking it up when each member reads. Small groups favour fan-out on write.
When the app is closed, the server wakes the device through APNs or FCM. The payload should be a hint to sync, not the message text, so content never leaks.
Learn them properly in the deep dives:
Question
Now read the question closely. Agree on what you are building, ask what is unclear, and work out the numbers before you draw anything.
2.1Brief and scope
The question exactly as the interviewer asks it, and what they want to learn from your answer.
Design the messaging feature for a service marketplace: customers and providers chat one-to-one and in small groups of up to 50. Messages must arrive in order, show sent, delivered and read states, work offline, and support photos. Expect 5 million daily users.
What it tests: Real-time transport, ordering, idempotency, fan-out, presence, push and multi-device sync.
The answer in one sentence. Persistent sockets for speed, a per-conversation sequence number for correctness, and history sync by cursor as the guarantee.
How to spend the 45 minutes
- Clarify~5 minAsk questions, agree on scope and write down assumptions.
- Estimate~5 minRough numbers for users, data and traffic, and what they imply.
- High-level design~11 minComponents, data flow and the key API on the board.
- Deep dive~16 minThe two or three hardest parts, with real mechanisms.
- Trade-offs and wrap-up~9 minDecisions, failure modes, and what you would do next.
2.2Clarify the problem
The first five minutes. Each answer removes a guess from your design.
| You ask | Interviewer answers |
|---|---|
| Group size limits? | 1:1 and groups up to 50. No broadcast channels. |
| End-to-end encryption? | Not in v1; encrypted in transit and at rest. |
| Multiple devices per user? | Yes, phone and tablet; history must match on both. |
| How long is history kept? | Forever for 1:1, two years for groups. |
| Message edits and deletes? | Delete for everyone within an hour; no edits in v1. |
2.3Estimate the scale
Say the arithmetic out loud. What matters is the order of magnitude and what it forces you to do.
| Metric | Estimate | How |
|---|---|---|
| Daily users | 5M | |
| Messages per day | ≈ 100M | 20 each |
| Average write rate | ≈ 1,150 msg/s | 100M ÷ 86,400 |
| Peak write rate | ≈ 10k msg/s | ×8–10 at evening peaks |
| Concurrent sockets | ≈ 1M | 20% of daily users online at peak |
| Storage | ≈ 50 GB/day text | 100M × ~500 bytes with metadata |
Detailed answer
The design itself, from the big picture down to the code: components first, then one request end to end, then the parts interviewers push hardest on.
3.1Architecture
Clients hold a persistent socket to a gateway layer. A chat service orders and stores messages per conversation, and a fan-out step delivers to online devices or falls back to push.
- WebSocket gateways
- Thin, stateful only for connections. A registry in Redis maps user and device to the gateway holding its socket, so any service can route a delivery.
- Chat service
- Assigns each message a per-conversation sequence number, dedupes on the client message ID, and appends to the store. This step decides the order.
- Message store
- Wide-column store partitioned by conversation ID and clustered by sequence, so reading history is a single partition scan.
- Fan-out workers
- For each recipient device: deliver over its socket if online, otherwise send a push and bump the unread index. Groups of 50 make write fan-out cheap.
- Presence
- Heartbeats refresh a short TTL key. Presence is approximate by design; it never blocks delivery.
3.2Key flow, step by step
A message sent while the recipient is offline, then read later on their tablet.
- The sender shows the message immediately as pending. The client message ID makes the send idempotent across reconnects.
- Ordering comes from the server’s per-conversation sequence, never from device clocks. The sender replaces its pending row with the acknowledged sequence.
- Receipts are cumulative: ‘read up to 1042’ is one small write, not one per message.
- Every device syncs by conversation cursor, which is how phone and tablet end up with identical history.
3.3Front end
The chat client is a small real-time system of its own: a connection manager, a local message store, an outbox for sends and a UI that never jumps or flickers as messages change state.
Screen map
- Inbox
- Reads conversations from the local database ordered by last message, with unread counts from the server’s inbox index. Swipe actions for mute and archive.
- Thread
- A reversed list keyed by client message ID so a message keeps its place as it moves from pending to sent to read. Older history pages in from the local database, then the server.
- Composer
- Drafts persist per conversation. The transfer manager compresses and queues photos, and the message shows a local thumbnail immediately.
- Notification
- On Android an FCM data message triggers a quick sync before the notification shows; on iOS a notification service extension can enrich the preview.
Client layers
| Layer | Technology | Responsibility |
|---|---|---|
| UI | Jetpack Compose / SwiftUI | Lists, composer, receipts |
| ViewModels | StateFlow / @Observable | Map database queries to UI state |
| Connection manager | OkHttp WebSocket / URLSessionWebSocketTask | Connect, heartbeat, reconnect with jitter, route events |
| Message repository | Room / GRDB | Insert pending, apply acks, fill gaps by sequence |
| Outbox | Local table | Resend unacked messages after reconnect with the same ID |
| Notifications | FCM / APNs + service extension | Wake, sync, show |
Every state the user can see
| State | What the user sees | How it works |
|---|---|---|
| Sending | Clock icon | Inserted locally with status pending |
| Sent | One tick | Server ack with sequence number |
| Delivered | Two ticks | Recipient device acknowledged |
| Read | Two blue ticks | Cumulative ‘read up to’ receipt |
| Failed | Red ‘!’ with Tap to retry | Rejected or retries exhausted |
| Reconnecting | Small ‘Connecting…’ in the title bar | Socket closed, backoff running |
| Gap | Spinner row between messages | Sequence gap detected, fetching history |
Code
Connection manager with reconnect and resend (Kotlin)
1class ConnectionManager(private val client: OkHttpClient, private val repo: MessageRepo) {2 private var socket: WebSocket? = null3 private var attempt = 04 val state = MutableStateFlow(ConnState.CONNECTING)5 6 fun connect(token: String) {7 val req = Request.Builder().url(WS_URL).header("Authorization", "Bearer " + token).build()8 socket = client.newWebSocket(req, object : WebSocketListener() {9 override fun onOpen(ws: WebSocket, r: Response) {10 attempt = 0; state.value = ConnState.ONLINE11 scope.launch { repo.pending().forEach { ws.send(it.toSendFrame()) } } // same client_msg_id12 }13 override fun onMessage(ws: WebSocket, text: String) { scope.launch { repo.applyFrame(Frame.parse(text)) } }14 override fun onFailure(ws: WebSocket, t: Throwable, r: Response?) { retry(token) }15 override fun onClosed(ws: WebSocket, code: Int, reason: String) { retry(token) }16 })17 }18 19 private fun retry(token: String) {20 state.value = ConnState.RECONNECTING21 val delayMs = Random.nextLong(0, min(30_000L, 1_000L shl min(attempt++, 5))) // full jitter22 scope.launch { delay(delayMs); connect(token) }23 }24}Thread list that never flickers (Compose)
1@Composable2fun Thread(messages: List<MessageUi>, onLoadOlder: () -> Unit) {3 val listState = rememberLazyListState()4 LazyColumn(state = listState, reverseLayout = true) {5 items(messages, key = { it.clientMsgId }, contentType = { it.kind }) { m ->6 MessageBubble(m) // status icon changes, item identity does not7 }8 }9 LaunchedEffect(listState) {10 snapshotFlow { listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index }11 .filterNotNull().filter { it > messages.size - 10 }.collect { onLoadOlder() }12 }13}3.4Data model and APIs
The tables and contracts that make the design concrete.
Socket protocol
1// client → server2{ "t": "send", "conv": "c-91", "client_msg_id": "0192e…", "body": "On my way", "attach": null }3// server → client4{ "t": "ack", "client_msg_id": "0192e…", "seq": 1042, "ts": 1730650000123 }5{ "t": "msg", "conv": "c-91", "seq": 1043, "from": "u-7", "body": "Thanks!" }6{ "t": "receipt", "conv": "c-91", "user": "u-7", "read_upto": 1042 }Message table (CQL)
1CREATE TABLE messages (2 conv_id text, seq bigint, msg_id uuid, sender text,3 body text, attach_key text, deleted boolean, created_at timestamp,4 PRIMARY KEY ((conv_id), seq)5) WITH CLUSTERING ORDER BY (seq DESC);3.5Tested code, explained
The core of this design as working code, with what each part does and why.
One conversation's order
A resent message must keep its place. This is the server's op_id check, applied to chat.
1/** Owns one conversation's order. The client's ID makes a resend harmless. */2class ConversationLog {3 private val byClientId = HashMap<String, Long>()4 private var nextSeq = 0L5 6 @Synchronized7 fun receive(clientMsgId: String): Long =8 byClientId.getOrPut(clientMsgId) { nextSeq += 1; nextSeq }9}byClientId.getOrPut(...)- The first arrival takes the next number. A resend returns the same one.
@Synchronized- One owner per conversation. Consistent hashing routes each chat to one instance, so no global lock is needed.
Compiled and tested by npm run test:mobile.
The phone's copy of a chat
Socket, push and catch-up all feed one method, so ordering logic lives in one place.
1/** The phone's copy of one chat. Socket, push and catch-up all end up in [onServerMessage]. */2class ChatThread {3 private val bySeq = sortedMapOf<Long, Msg>()4 private val pending = LinkedHashMap<String, Msg>()5 var highestRead = 0L6 private set7 8 fun sendLocal(id: String, text: String) { pending[id] = Msg(id, text) }9 10 /** Our own message came back with its place in the order. */11 fun onServerMessage(m: Msg) {12 val seq = checkNotNull(m.seq)13 pending.remove(m.clientMsgId)14 bySeq[seq] = m.copy(state = MsgState.SENT)15 }16 17 /** Confirmed messages in order, then the ones still waiting. */18 fun visible(): List<Msg> = bySeq.values + pending.values19 20 /** Holes to ask the server for after a reconnect. */21 fun missingSeqs(): List<Long> {22 val last = bySeq.keys.lastOrNull() ?: return emptyList()23 return (1..last).filter { it !in bySeq }24 }25 26 /** One number stands for many messages, and two devices merge by taking the larger. */27 fun markRead(seq: Long) { highestRead = maxOf(highestRead, seq) }28}sendLocal- Shows the message straight away, with no signal needed.
sortedMapOf<Long, Msg>()- Keyed by sequence, so arrival order stops mattering.
missingSeqs()- A gap in the numbers means a message was missed. After a reconnect, the client asks for these.
maxOf(highestRead, seq)- One number covers many messages, and two devices merge by taking the larger.
Compiled and tested by npm run test:mobile.
Telling other devices to look
A push doesn't carry the change. It just says something changed, so Dev's tablet pulls by cursor like any other device.
1/** Push says "come and look". It carries no data, and one push covers many changes. */2class ChangeNotifier(private val sendPush: (device: String) -> Unit) {3 fun onCommitted(changes: List<Change>, devicesFor: (jobId: String) -> Set<String>, author: String) {4 changes.flatMap { devicesFor(it.jobId) }5 .toSet()6 .minus(author)7 .forEach(sendPush)8 }9}flatMap { devicesFor(it.jobId) }- Finds who cares about each change.
.toSet()- Five changes for one tablet still mean one push, which saves battery and provider quota.
.minus(author)- The phone that made the change doesn't need telling.
Compiled and tested by npm run test:mobile.
3.6Hard parts, in depth
Where interviewers spend the second half of the round.
Ordering and gaps
Sequence numbers per conversation come from a counter owned by the chat service for that conversation, routed by consistent hashing so one instance owns a conversation at a time. That avoids a global lock.
Clients detect gaps: if they hold 1040 and receive 1043, they fetch 1041 to 1042 before rendering. This makes delivery over the socket an optimisation and history sync the guarantee.
Reconnects and duplicates
Mobile sockets drop constantly. On reconnect the client resends any pending messages with the same client message ID; the server returns the original sequence instead of storing twice.
Clients reconnect with exponential backoff and jitter, and gateways drain gracefully during deploys so a million phones do not reconnect at the same second.
Push without leaking content
Push payloads carry the conversation ID and a short preview the user opted into. On iOS a notification service extension can fetch and decrypt content; on Android an FCM data message triggers a quick sync.
Collapse keys keep a busy group from producing fifty notifications.
Q&A
The questions that come after the design, with model answers. Answer each one out loud before opening it.
4.1Follow-up questions
What the interviewer asks once the main design is on the board.
How do you show ‘typing…’?
How would you add end-to-end encryption?
How do you delete for everyone?
What about a 10,000-member channel?
4.2Front-end follow-ups
Questions aimed at the client: rendering, state and devices.
Why key messages by client ID rather than server sequence?
How do you avoid draining battery?
4.3Incidents, probes and traps
Realistic production incidents and pointed probes, grouped by topic.
The product 1
ProbeWhy not order messages by server timestamp?
Requirements 1
TrapWhat clarifying question do candidates usually forget?
Scale 1
ProbeYou need a million concurrent sockets. How many gateway servers, and what limits them?
Architecture 2
IncidentAfter a gateway deploy, delivery latency spikes for ten minutes and the auth service falls over. Why?
ProbeHow does a fan-out worker find the gateway holding a user’s socket?
The key flow 1
IncidentUsers report messages out of order after reconnecting.
Front end 2
IncidentOpening a busy group freezes low-end Android phones for two seconds.
ProbeHow do you keep unread counts consistent across phone and tablet?
Data and APIs 1
TrapWhy send receipts as ‘read up to sequence N’ instead of per message?
The hard parts 2
IncidentA message appeared twice in a group after a network blip.
ProbeWhat happens to ordering if the instance owning a conversation crashes mid-write?
Trade-offs 1
ProbeWhy not deliver messages through push notifications alone?
Failures 2
IncidentAll iOS push notifications stopped after a credentials change.
IncidentOne member floods a group with 100 messages a second.
Extra depth
What turns a good answer into a strong one: the decisions you rejected, how the system fails, and the signals of seniority.
5.1Trade-offs and decisions
Every choice has a cost. Naming it is what interviewers listen for.
Show detailsHide details
| Decision | Chosen | Rejected | Why |
|---|---|---|---|
| Transport | WebSocket | Long polling or MQTT | Bidirectional, widely supported, and gateways scale horizontally with a connection registry. |
| Fan-out | On write | On read | Groups cap at 50, so writing to each recipient’s inbox is cheap and makes unread counts trivial. |
| Store | Wide-column partitioned by conversation | Postgres | Append-heavy, huge volume, simple access pattern by conversation and sequence. |
| Ordering | Server sequence per conversation | Timestamps | Clock skew and concurrent sends make timestamps unreliable. |
5.2Failure modes
What breaks in production, and how the design keeps working when it does.
Show detailsHide details
| What breaks | How the design copes |
|---|---|
| Gateway crashes | Clients reconnect to another gateway; pending messages resend with the same ID; history sync fills gaps. |
| Push never arrives | Unread index and sync on app open guarantee delivery; push is a hint. |
| Hot conversation | Rate-limit per sender and conversation; sequence owner can be moved. |
| Recipient blocked sender mid-flight | Fan-out checks block lists at delivery time, not just at send. |
5.3Performance and accessibility
The client-side details that separate a demo from a product.
Performance
- Keys are client message IDs, so pending → sent → read updates the same row instead of inserting a new one.
- The app batches receipts: it sends ‘read up to 1042’ once when the thread is visible, not per message.
- Images reserve their aspect ratio before loading so the list never jumps.
- The socket closes in the background; delivery then relies on push plus sync on open, which saves battery.
Accessibility and localisation
- Screen readers announce incoming messages politely without stealing focus.
- Ticks have text equivalents: sent, delivered, read.
- Dynamic type and right-to-left layouts are supported in bubbles.
5.4Senior versus principal
The same design, answered at two levels. Use the rubric to score yourself.
A senior answer uses WebSockets, stores messages and sends push when offline.
A principal answer sizes sockets and write rate, makes sends idempotent, orders by server sequence, treats socket delivery as an optimisation over cursor sync, and explains how the design changes for large channels and encryption.
Score yourself
5.5Whiteboard
Draw the design here, in this order. It stays in this browser.
- Phone and tablet connected to WebSocket gateways
- Chat service assigning sequence numbers
- Message store partitioned by conversation
- Fan-out branching to socket, push and unread index
- Receipt flow with ‘read up to’