skip to content

Chat Delivery Semantics

A chat client sends with its own message id, echoes locally, shows sending, sent, delivered and read, and orders by server sequence. Interviewers probe duplicates, gaps and out-of-order arrival.

on this pageshow

explore

questions

6

In a dating app's mobile chat, why should the phone generate each message's id when the message is composed, before the first send attempt?

level: juniorimportance: must knowfreq 65%

answer

  1. the answer that never came back
  2. one id for echo, ack and retry
  3. minted once, not per attempt
  4. random UUID needs no server
  5. no temporary id to swap later

basics

~20 s

An id minted once at compose time lets the client show the message at once, lets the server recognise a retried send as the same message, and lets every acknowledgement or returning copy update one row instead of adding another.

solid answer

~50 s

When the user taps send, the client writes the message to its local database under an id it generates itself — a random UUID needs no coordination with the server — and renders it straight away as a *sending* bubble. Every send attempt carries that same id, so when a send times out after the server had in fact stored the message, the retry is recognised and answered with the original sequence number instead of becoming a second message. The acknowledgement, the copy the server echoes back over the socket and later receipts all carry the id, so the client updates one row rather than appending a duplicate. Because the id never changes, there is no temporary-to-server id swap to do. The id is minted once per message, never once per attempt, or the server cannot tell a retry from a new message.

code

json · 12 lines
json
{
  "send": {
    "clientId": "6f1c2e0a-8b4d-4c3e-9a71-2d5b0f9e4c13",
    "conversationId": "match-8812",
    "body": "Saturday works for me!"
  },
  "ack": {
    "clientId": "6f1c2e0a-8b4d-4c3e-9a71-2d5b0f9e4c13",
    "seq": 57,
    "duplicate": true
  }
}

go deeper

for a junior

Recall the timeline: the server stored the message, the answer was lost, the phone retries. Say that one id, created when the user taps send, is what lets the server recognise the retry.

for a middle

Walk through every place the id is used: local echo row, send request, server dedup, acknowledgement match, the echoed copy and receipts. Stress minting it once, not per attempt.

for a senior

Show you know the id only helps because the server honours it, and that the phone can never learn the outcome by asking first. Mention deleting a still-sending message without orphaning a delivered one.

for a principal

Frame it as a contract decision: client-minted ids remove the temporary-id swap from every write path, at the cost of the server keeping a deduplication record per message long enough to cover the slowest retry.

## The problem: a send whose answer is lost Picture a dating app's one-to-one chat. A user writes "Saturday works for me!" on a train and taps send. The request reaches the server, the server stores the message and gives it the next **sequence number** in the conversation, and then the train enters a tunnel: the response never reaches the phone. From the phone's side the send simply timed out. It cannot know whether the message was stored. If it resends, the match may see the line twice; if it gives up, a message the server already delivered looks failed on the sender's screen. The way out is to give the message an identity **before** it leaves the phone, so that "send it again" and "this is a new message" are different things on the wire. ## What one client-generated id does A **client-generated message id** (often called a client id) is a value the app creates when the message is composed — typically a random UUID, which needs no central registration or coordination, so the phone can mint it offline. That one value then serves several jobs: - **Local echo key** — the message is written to the local database and shown as a *sending* bubble under this id, before any network call. - **Server deduplication key** — every send attempt carries it; the server records it with the stored message, and a repeat with the same id gets the original result back (the same sequence number) instead of a second row. - **Acknowledgement matching** — the server's acknowledgement names the client id, so the client knows exactly which pending bubble to mark *sent* and place by sequence. - **Merging returning copies** — when the server echoes the message back over the socket, or a later history fetch returns it, the client upserts by id instead of appending. - **Receipt target** — delivered and read receipts can name the message unambiguously, even one that has no sequence number yet on some screen. ## When and how to mint it 1. When the user taps send, generate the id, then write the message with state `sending` and the id to the local database in one write. 2. Render from the database, so the bubble appears instantly whatever the network does. 3. Send the message with its id; if the attempt times out or the connection drops, retry with the **same** id — never mint a fresh one per attempt. 4. On acknowledgement, update the row found by that id: store the server's sequence number and move it to *sent*. 5. If the user later deletes or retries a failed bubble, the same id travels with that action too. If the app may be closed right after the tap, the message is already on disk; a pending send can ride the app's durable queue of offline writes, which has its own rules for transactions and key lifetime. ## Alternatives and what they cost | Design | What goes wrong | |---|---| | No id until the server answers | Nothing to show the bubble under; a retry after a lost answer creates a duplicate | | A new id for every attempt | The server sees two different messages and stores both | | Deduplicating by message text | A user who sends "haha" twice has one message silently swallowed | | The phone's timestamp as the id | Two quick messages can share a timestamp, and the phone's clock can be wrong or move backwards | | A temporary id swapped for a server id | Works, but every reference to the temporary id must be rewritten; a client id makes that swap unnecessary | ## Where the edges are The id makes a resend safe only because the **server** honours it: it must look the id up before storing, and remember it at least as long as a client might still retry. How that store is built and how long a key is kept belong to the server and to the general theory of idempotency, not to the chat client. What the client owns is minting the id once, persisting it before the first attempt, and using it everywhere the message comes back. Interviewers usually probe this with "the send timed out — now what?": the strong answer is "retry with the same id, and let the acknowledgement or the echoed copy settle it", not "ask whether it arrived first", because that question can be lost exactly like the send, and a 'not found' answer can race a send still in flight.

  • Should the client id be random or time-ordered?
    For deduplication it only has to be unique, and a random UUID is unique without any coordination. A time-ordered UUID can keep a local index tidy, but its time part comes from the phone's clock, which can be wrong or move backwards, so it must never be used to order the conversation — the server's sequence number does that.
  • The user deletes a bubble that is still sending; can the client just remove it locally?
    Only if no attempt ever left the phone. If one might have reached the server, the message may already be delivered, so the client keeps the id and sends a delete for that id once the network allows; otherwise the match could read a message the sender believes was cancelled.

It is like phoning in a takeaway order with an order number you chose yourself: if the line drops and you call again with the same number, the kitchen sees it already has that order and cooks it once.

  • If the send timed out, the server never stored it, so resend as new.
  • A fresh id on every attempt makes retries easier to trace.
  • The server can spot a duplicate by comparing the message text.
  • The client needs the server's id before it can show or retry a message.
  • The phone's timestamp is unique enough to identify a message.
open as a page

In a dating app's mobile chat, what moves a message through sending, sent, delivered and read, and what does the sender see while offline?

level: middleimportance: must knowfreq 68%

basics

~20 s

Sending means only the phone holds it; sent, the server acknowledged it with a sequence number; delivered, the match's device stored it; read, they viewed it. Offline the bubble stays visibly pending, and failure shows with a retry, never a silent drop.

open as a page

In a dating app's mobile chat, the client holds messages up to sequence 10 when sequence 12 arrives over the socket; what should the client do?

level: seniorimportance: must knowfreq 60%

basics

~20 s

Store message 12 by id but keep the conversation's watermark at 10, the highest sequence held without a gap. After a brief hold, ask the server for everything after 10, merge by id, then advance the watermark.

open as a page

In a dating app's mobile chat, where should a just-sent message sit in the conversation before and after the server acknowledges it?

level: middleimportance: should knowfreq 58%

basics

~20 s

Until acknowledged, the local echo sits below every acknowledged message, in typed order. The acknowledgement gives it the server's sequence number, and from then on it sorts by that number like any other message, never by the phone's clock.

open as a page

In a dating app's mobile chat, how should the client send typing indicators and read receipts without flooding the connection or misleading the match?

level: middleimportance: should knowfreq 52%

basics

~20 s

Typing is ephemeral: send it at most every few seconds over the live socket only, never queued, and let the receiver expire it. Read receipts are durable but cumulative: one 'read up to sequence N', sent when messages are actually seen.

open as a page

In a dating app's mobile chat, one message reaches the phone by push, by socket and by a history fetch; how does the client show it once?

level: seniorimportance: should knowfreq 57%

basics

~20 s

Every path writes into the local database through one upsert keyed by message id, and the screen renders from that database. Copies merge into one row, counts are computed from stored rows, and a push copy never skips the gap check.

open as a page