/Interview
Interview room · Mobile system design · 45 minutes

Real-time chat with offline delivery

PracticeSystem designTier 3 · Edge casesCreate45 minMobile systems

You will be able to: design real-time chat with offline delivery in 45 minutes, and defend each trade-off.

Mobile engineeringAll mobile pages

Priya's edit becomes a message. The outbox, client ID and cursor all stay. Sockets and ordering per conversation are new. Start from the basics in Day 10 →

Study with AI

Ready45:00
ClarifyEstimateHigh-level designDeep diveTrade-offs and wrap-up
Part 1 of 5

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

  1. A customer opens the chat from her booking and types ‘Running 10 minutes late’.
  2. The message appears instantly with a clock, then one tick when the server has it.
  3. The provider’s phone is in his pocket with the app closed; he gets a push notification.
  4. He opens it; messages load from his phone first, then any missing ones arrive.
  5. Her screen shows two blue ticks the moment he has read it.

Why it is hard

Order must be right

Two people typing at once must see the same order on every device.

Connections drop constantly

Phones switch networks every few minutes; nothing may be lost or sent twice.

A million open connections

Real-time delivery means keeping sockets open for every online user.

Push is unreliable

Notifications can be delayed or dropped; the app must still end up complete.

Think of it like this

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.

01Persistent connection

A WebSocket keeps one open channel between phone and server, so new messages arrive in milliseconds instead of waiting for the app to ask.

02Server sequence numbers

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.

03Delivery states

Sent means the server stored it, delivered means the recipient's device received it, read means it was shown. Each is a separate acknowledgement.

04Client message id

The sender creates an id before sending. Retries reuse it, so a reconnect never produces a duplicate bubble.

05Fan-out

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.

06Push notifications

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:

Part 2 of 5

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.

Interviewer

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

  1. Clarify~5 minAsk questions, agree on scope and write down assumptions.
  2. Estimate~5 minRough numbers for users, data and traffic, and what they imply.
  3. High-level design~11 minComponents, data flow and the key API on the board.
  4. Deep dive~16 minThe two or three hardest parts, with real mechanisms.
  5. 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 askInterviewer 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.

MetricEstimateHow
Daily users5M
Messages per day≈ 100M20 each
Average write rate≈ 1,150 msg/s100M ÷ 86,400
Peak write rate≈ 10k msg/s×8–10 at evening peaks
Concurrent sockets≈ 1M20% of daily users online at peak
Storage≈ 50 GB/day text100M × ~500 bytes with metadata
Part 3 of 5

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.

CLIENTREAL-TIME EDGECOREMobile appSQLite · outboxTabletsame accountPhoto uploadpresigned URLWS gateways1M socketsPresenceRedis TTL keysS3 + CDNphotosChat serviceorder · storeFan-out workersper recipientPush serviceFCM · APNsMessage storeCassandra / ScyllaKafkamsg eventsInbox indexper-user unreadWSPUTsendappendeventonline?deliverofflineunread
ClientServiceData storeQueue / stream
Reference design. Boxes show components; arrows show who calls whom.
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.

Sender appGatewayChat serviceFan-outRecipientinsert pending + client_msg_idsend {client_msg_id, text}forwarddedupe, seq = 1042ack seq 1042sent ✓eventpush (offline)Recipient opens app on tabletsync conversations since seqmessages 1040–1042delivered + read up to 1042read ✓✓
Solid arrows are requests; dashed arrows are responses.
  1. The sender shows the message immediately as pending. The client message ID makes the send idempotent across reconnects.
  2. Ordering comes from the server’s per-conversation sequence, never from device clocks. The sender replaces its pending row with the acknowledged sequence.
  3. Receipts are cumulative: ‘read up to 1042’ is one small write, not one per message.
  4. 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

Inboxconversations ·unreadThreadmessages · receiptsComposertext · photo ·draftPhoto viewerzoom · saveNotificationtap → threadSearchlocal full-textGroup infomembers · mutePhoto pickersystem pickerattachdeep linkjump to
Client
Screens and how users move between them.
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

LayerTechnologyResponsibility
UIJetpack Compose / SwiftUILists, composer, receipts
ViewModelsStateFlow / @ObservableMap database queries to UI state
Connection managerOkHttp WebSocket / URLSessionWebSocketTaskConnect, heartbeat, reconnect with jitter, route events
Message repositoryRoom / GRDBInsert pending, apply acks, fill gaps by sequence
OutboxLocal tableResend unacked messages after reconnect with the same ID
NotificationsFCM / APNs + service extensionWake, sync, show

Every state the user can see

StateWhat the user seesHow it works
SendingClock iconInserted locally with status pending
SentOne tickServer ack with sequence number
DeliveredTwo ticksRecipient device acknowledged
ReadTwo blue ticksCumulative ‘read up to’ receipt
FailedRed ‘!’ with Tap to retryRejected or retries exhausted
ReconnectingSmall ‘Connecting…’ in the title barSocket closed, backoff running
GapSpinner row between messagesSequence gap detected, fetching history

Code

Connection manager with reconnect and resend (Kotlin)

connection-manager-reconnect.ktKotlin
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}
24 linesUTF-8Kotlin

Thread list that never flickers (Compose)

thread-list-that.ktKotlin
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}
13 linesUTF-8Kotlin

3.4Data model and APIs

The tables and contracts that make the design concrete.

Socket protocol

socket-protocol.jsonJSON
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 }
6 linesUTF-8JSON

Message table (CQL)

message-table-cql.sqlSQL
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);
5 linesUTF-8SQL

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.

ChatThread.ktKotlin
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}
9 linesUTF-8Kotlin
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.

ChatThread.ktKotlin
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}
28 linesUTF-8Kotlin
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.

SyncServer.ktKotlin
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}
9 linesUTF-8Kotlin
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.

Part 4 of 5

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…’?
Ephemeral events over the socket only, never stored, throttled to one every few seconds.
How would you add end-to-end encryption?
Adopt the Signal protocol: per-device keys, server stores ciphertext only, and multi-device means encrypting for each device. Search and moderation move to the client, which is the real cost.
How do you delete for everyone?
Write a tombstone event with its own sequence; clients replace the message with ‘deleted’. A worker removes media objects.
What about a 10,000-member channel?
Switch that conversation type to fan-out on read: one copy, members pull by cursor, and only mentions trigger push.

4.2Front-end follow-ups

Questions aimed at the client: rendering, state and devices.

Why key messages by client ID rather than server sequence?
The client ID exists from the moment the user taps send, so the row keeps its identity and animation state when the server sequence arrives.
How do you avoid draining battery?
No socket in the background; push wakes the app, a short sync runs, and the socket reconnects only when the app is in the foreground.

4.3Incidents, probes and traps

Realistic production incidents and pointed probes, grouped by topic.

The product 1

ProbeWhy not order messages by server timestamp?
Several servers can stamp the same millisecond and their clocks drift, so order becomes ambiguous and gaps are undetectable. A per-conversation sequence gives one total order and lets clients notice a missing message instantly.

Requirements 1

TrapWhat clarifying question do candidates usually forget?
Multi-device support and whether end-to-end encryption is required. Multi-device changes sync to per-device cursors; end-to-end encryption removes the server’s ability to search, moderate or render previews.

Scale 1

ProbeYou need a million concurrent sockets. How many gateway servers, and what limits them?
Memory per connection (TLS buffers plus state, tens of kilobytes), file descriptors and CPU for TLS handshakes. A tuned node holds tens of thousands to around a hundred thousand connections, so roughly 10 to 20 nodes plus headroom. The real sizing driver is the reconnect storm after a deploy, not steady state.

Architecture 2

IncidentAfter a gateway deploy, delivery latency spikes for ten minutes and the auth service falls over. Why?
Every client reconnected at once and each reconnect called the auth service. Drain gateways gradually, make clients reconnect with jitter, validate tokens locally (signed JWTs) instead of calling auth per connection, and rate-limit reconnects per user.
ProbeHow does a fan-out worker find the gateway holding a user’s socket?
A registry (often Redis) maps user and device to gateway ID with a heartbeat TTL. Each gateway listens on its own channel. If no live entry exists, the worker sends a push and bumps the unread index instead.

The key flow 1

IncidentUsers report messages out of order after reconnecting.
The client inserted socket messages by arrival time and did not fill gaps. Sort by server sequence, keep unacknowledged sends at the bottom by local time, detect gaps and fetch missing ranges before rendering.

Front end 2

IncidentOpening a busy group freezes low-end Android phones for two seconds.
The app loads the whole history, parses large JSON on the main thread and decodes full images. Page from the local database, parse off the main thread, reserve image sizes with placeholders, and load older pages on scroll.
ProbeHow do you keep unread counts consistent across phone and tablet?
The server keeps a read cursor per user per conversation. Reading on any device sends ‘read up to N’; the server updates the inbox index and notifies other devices, which update their badge. Clients adjust optimistically and reconcile with the server count.

Data and APIs 1

TrapWhy send receipts as ‘read up to sequence N’ instead of per message?
It is one small, idempotent, monotonic write instead of one per message per member, which would explode in groups. Replays are harmless because a lower number never moves the cursor backwards.

The hard parts 2

IncidentA message appeared twice in a group after a network blip.
The client retried with a new ID, or the server did not enforce uniqueness. Keep the client message ID stable across retries and add a unique constraint on conversation plus client message ID so the server returns the original sequence.
ProbeWhat happens to ordering if the instance owning a conversation crashes mid-write?
Ownership moves through consistent hashing with a lease. The new owner reads the highest stored sequence before allocating, or uses an atomic counter in durable storage. Some numbers may be skipped; clients treat a gap that the server confirms as empty as filled.

Trade-offs 1

ProbeWhy not deliver messages through push notifications alone?
Push is throttled, best-effort, size-limited and unordered. It is a good wake-up signal, but delivery must be guaranteed by sockets in the foreground and cursor sync on open.

Failures 2

IncidentAll iOS push notifications stopped after a credentials change.
An expired APNs certificate, a sandbox versus production mismatch, or a wrong topic. Use token-based APNs auth (a .p8 key that doesn’t expire yearly), monitor push success rates per platform, and alert on drops.
IncidentOne member floods a group with 100 messages a second.
Rate-limit per sender and per conversation at the chat service, return 429 with a clear client message, offer a ‘slow mode’ for groups, and keep abuse reports flowing to moderation.
Part 5 of 5

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
DecisionChosenRejectedWhy
TransportWebSocketLong polling or MQTTBidirectional, widely supported, and gateways scale horizontally with a connection registry.
Fan-outOn writeOn readGroups cap at 50, so writing to each recipient’s inbox is cheap and makes unread counts trivial.
StoreWide-column partitioned by conversationPostgresAppend-heavy, huge volume, simple access pattern by conversation and sequence.
OrderingServer sequence per conversationTimestampsClock 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 breaksHow the design copes
Gateway crashesClients reconnect to another gateway; pending messages resend with the same ID; history sync fills gaps.
Push never arrivesUnread index and sync on app open guarantee delivery; push is a hint.
Hot conversationRate-limit per sender and conversation; sequence owner can be moved.
Recipient blocked sender mid-flightFan-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.

Senior answer

A senior answer uses WebSockets, stores messages and sends push when offline.

Principal answer

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.

  1. Phone and tablet connected to WebSocket gateways
  2. Chat service assigning sequence numbers
  3. Message store partitioned by conversation
  4. Fan-out branching to socket, push and unread index
  5. Receipt flow with ‘read up to’
Tip: draw boxes first, then arrows, then label them with Text.
AI Engineer · interview room