Deliver to users who are offline
The recipient has no connection; the message must arrive later and in order.
Say it like this
Delivery has to survive the recipient being offline, so every message is written to a durable per-user inbox before any push, and on reconnect the client sends its last cursor and drains the inbox in order.
Basic solution
Write to a durable per-user inbox before pushing; on reconnect the client sends its last cursor and drains the inbox.
Inbox first, push second, drain by cursor
The message lands in the recipient’s inbox before any delivery attempt. Online users get a push over the socket; offline users get a silent notification and drain the inbox from their cursor on reconnect.
How it works
- The chat service receives Alice's message for Bob, assigns it the next sequence number within that conversation, and writes it to the message store under (conversation_id, message_seq).
- It then appends a row to Bob's inbox: (user_id, inbox_seq, conversation_id, message_seq). The inbox_seq is a counter that only ever increases for Bob, so his inbox is an ordered list of pointers to messages he has not yet acknowledged.
- Only after both writes does the service look up Bob's connection in the registry. If he is online, it pushes the message frame over his socket, and when his client acks it the inbox row is deleted. If he is offline, it sends a silent push notification containing no message content, just a signal that there is something to fetch.
- When Bob reconnects, his client sends the highest inbox_seq it has acked, its cursor. The server reads every inbox row with a higher inbox_seq, in order, and returns them in pages of a few hundred.
- For each conversation that appeared in the drained inbox, the client compares the last message_seq it holds with the server's latest. If there is a gap (it has 1041, the server says 1060), it fetches that range from the message store. This covers messages whose inbox rows were trimmed while Bob was away.
- The client acks each page it has stored. The server deletes the acked inbox rows, and Bob's cursor moves forward.
Why inbox first, push second
- A push is best effort: the socket can close between the write and the ack. The inbox row is the source of truth, so a lost push only delays delivery until the next reconnect.
Worked example
Sizing the inbox for the chat app
- 500K messages per second, 1.3 recipients on average, so 650K inbox rows per second written and later deleted.
- 25 percent of recipients are offline at any moment, so 160K rows per second stay in the inbox until the user returns; the median offline gap is 2 hours.
- Steady-state inbox size: 160K x 7,200 s = 1.15B rows, at about 60 bytes each with keys roughly 70 GB. Partitioned by user id across a wide-column cluster, where the row (user_id, inbox_seq) is a cheap append and the drain is a single range scan.
- Reconnect load: 10M reconnects per day, each draining 40 rows on average, so 400M rows read per day, under 5K per second averaged and about 50K per second at the morning peak.
- A user offline for a month with 50 group chats could accumulate 200K rows; cap the inbox at 10K rows per user and mark it truncated, so the client falls back to fetching each conversation's history by sequence range.
- Cost of the alternative, no inbox: the client would have to ask every conversation for new messages on reconnect, 50 conversations x 10M reconnects = 500M range queries per day instead of 10M.
Variants and when each wins
Per-user inbox with cursor
- One ordered list of undelivered message pointers per recipient, drained from the client's cursor on reconnect. It wins because writes are cheap appends and a reconnect is one range scan; it needs a cap and a history fallback for users who are away a long time.
Per-conversation sequence only
- No inbox; the client remembers its last sequence per conversation and asks each conversation for newer messages on reconnect. It wins for a simple server, but the reconnect cost grows with the number of conversations a user has.
Device-level inbox
- One inbox per device rather than per user, so each device drains and acks independently. It wins when users have several devices that must all end up with every message; the cost is one inbox write per device instead of per user.
Store-and-forward queue per user
- A real message broker queue per user, with the broker holding undelivered messages. It wins on clean semantics, but millions of queues strain most brokers.
Recommendation: a per-device inbox in a wide-column store keyed by (user_id, device_id, inbox_seq), capped, with per-conversation history as the fallback for gaps.
Failure modes
Messages delivered out of order on reconnect
- Replies appear above the question they answer. It happens because the inbox was drained by arrival time instead of by sequence, or because two pages overlapped and the client interleaved them. Drain strictly by inbox_seq, use the last sequence of each page as the cursor for the next, and let the client sort by message_seq within each conversation.
Push without a row
- The phone buzzes but the message is not there when the app opens. It happens because the push went out before the inbox write had committed, so the drain found nothing. Always write the inbox row first and send the push afterwards; the push should carry nothing but a wake-up.
Inbox never shrinks
- Storage grows forever even though users are reading everything. It happens because some clients never ack, or acks are lost, so rows are never deleted. Delete on ack in batches, cap the inbox per user and set a truncated flag when the cap is hit, and expire inbox rows after 30 days regardless.
Duplicate delivery after a crash mid-ack
- The same message shows up twice. It happens because the server deleted the rows after sending them but before the client had stored them, or the client re-requested a page it already had. Dedup on message id in the client, and delete rows only after the client's ack arrives rather than when the page is sent.
Multi-device divergence
- The laptop shows messages the phone never received. It happens because there is one inbox per user and whichever device connected first drained it. Keep one inbox per device, each with its own cursor.
Interviewer follow-ups
Answer out loud before you open each one.
What exactly is the cursor and who owns it?
The highest inbox_seq the client has stored and acked, kept by the client and sent on every reconnect. The server trusts it to resume from, and keeps rows above it until acked. Because inbox_seq is per user and monotonic, the cursor is a single integer regardless of how many conversations are involved.
Why do you also need per-conversation sequences if the inbox is ordered?
The inbox is capped and trimmed, so a long absence can lose inbox rows. Per-conversation sequences let the client detect a gap (it has seq 1041 and the server says 1060) and fetch the range from history, which is kept far longer than the inbox.
How does this interact with push notifications?
The push is a wake-up signal sent only after the inbox write, with little or no content. When the app opens it reconnects and drains the inbox normally. That means a lost or delayed push costs latency, never a lost message.
How would you handle a user who has been offline for a year?
The inbox was capped and marked truncated long ago. On reconnect the client sees the truncated flag, discards its inbox cursor, and instead syncs each conversation from its last known sequence, paging through history. Old conversations beyond the retention window simply start fresh.
Connections
- In study paths
- Real-time delivery
- Built on
- Vector clock · CRDT
- Pairs with
- Find which server holds a user · Reach devices through platform push · Order events within a stream · Append-heavy, time-ordered rows · Make retries safe
- Pulls against
- Simpler one-way push
- Used in
- Chat App (WhatsApp / Messenger) · Notification System
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon ElastiCache | Registry lookup | Memorystore | Azure Managed Redis | Redis, Valkey, Memcached |
| Amazon EC2 Auto Scaling | Chat service, Sync service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon Keyspaces | Message store, Inbox (per device) | Bigtable | Managed Instance for Apache Cassandra | Apache Cassandra, ScyllaDB |