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?
answer
- highest contiguous, not highest seen
- store it, but do not advance
- ask for everything after the watermark
- page until the server says complete
- watermark and messages in one write
basics
~20 sStore 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.
solid answer
~50 sThe client keeps a per-conversation **watermark**: the highest sequence number it holds with nothing missing below it. Message 12 is stored, keyed by its id, but the watermark stays at 10 because 11 is missing. The client waits briefly in case 11 is merely late on another path, then asks the server for messages after 10, paging until the server says the range is complete. Everything returned is upserted by id, so 12 is not duplicated, and the watermark advances to the highest contiguous number — or to the end of the range the server answered for, if 11 no longer exists. The watermark is saved in the same local transaction as the messages. After a reconnect the client asks for the same thing: everything after its watermark. Using the highest number seen as the cursor would skip 11 for good.
code
pseudocode · 20 lineson_message(conv, msg):
in one local transaction:
store.upsert_by_id(msg) # a second copy merges, never appends
conv.watermark = highest_contiguous(conv) # stops below the first missing seq
store.save(conv)
if store.has_seq_above(conv, conv.watermark):
schedule_fill(conv, hold = GAP_HOLD) # assumption: about one second
fill(conv):
if not store.has_seq_above(conv, conv.watermark):
return # the gap closed during the hold
after = conv.watermark
loop:
page = server.get_messages(conv.id, after_seq = after, limit = 50)
in one local transaction:
for m in page.items: store.upsert_by_id(m)
conv.watermark = max(highest_contiguous(conv), page.covered_up_to)
store.save(conv)
if page.complete: return
after = page.covered_up_togo deeper
Know that messages can arrive out of order and that the client tracks the last number it holds without gaps, then asks the server for everything after it.
Explain highest contiguous versus highest seen, the brief hold, paged fetches until complete, and upserting by id so overlapping fetches never duplicate.
Cover the hole that never fills, persisting the watermark with the messages in one transaction, and using the same request after a reconnect for every conversation.
Judge the cost of gap fills at scale: many conversations after a long absence argue for one account-wide catch-up request instead of one per conversation, which changes the server contract.
## Why gaps happen In a dating app's one-to-one chat, messages reach the phone by more than one route: the live socket while the app is open, a data-only push when it is not, and history fetches when a conversation is opened. Routes are not ordered with respect to each other, messages the server wrote to a socket that had silently died never arrive, and a push can be delayed, reordered or discarded. So the client must expect to receive **sequence 12 while holding only up to 10**. The server numbers each conversation's messages with an increasing **sequence number**, assigned without skipping; the client's job is to notice a hole and fill it. ## The watermark The client keeps one value per conversation, persisted with the messages: - **Watermark** — the highest sequence number such that every message up to it is stored locally. It is deliberately not "the highest number I have seen". If the client used 12 as its cursor, it would later ask for "everything after 12" and message 11 would never be fetched. ## Handling message 12 1. **Store it** under its message id. If it is already present, the write merges instead of adding a row. 2. **Do not advance the watermark** past the first missing number; it stays at 10. 3. **Hold briefly.** Message 11 may be a moment behind on another path. This scenario assumes a hold of about a second; any figure is a product choice, not a standard. 4. **Fetch after the watermark.** Ask the server for messages after 10, a page at a time, until the response says the range is complete. 5. **Merge and advance.** Upsert every returned message by id, then move the watermark to the highest contiguous number. 6. **Recheck.** If 11 arrived during the hold, the scheduled fetch finds no gap and does nothing. Two display choices are defensible while the fetch runs: keep 12 out of view until 11 is in place, or show 12 in its sequence position with a small loading row above it. What the client must not do is show 12 directly after 10 as if nothing were missing. ## When 11 no longer exists The server's answer, not the numbering, closes the gap. A message may have been removed — by moderation, say — and never be delivered. If the client waits for 11 forever, the conversation stalls. So the history response should state the range it covers ("complete up to 12"), and the client advances its watermark to that point even if 11 is absent. ## The same rule after a reconnect When the app returns to the foreground or the socket reconnects, the client does not refetch the conversation. It asks, for each conversation it cares about, for everything after that conversation's watermark — the same request as the gap fill. How the socket is re-established, and how the server stores and replays history efficiently across many conversations, belong to the connection and to the server; the client's part is holding an honest watermark and asking from it. | Cursor the client keeps | After 12 arrives early | Outcome | |---|---|---| | Highest sequence seen (12) | Next fetch asks after 12 | Message 11 is lost forever | | Highest contiguous (10) | Next fetch asks after 10 | 11 and 12 arrive; 12 merges by id | | No cursor, refetch on gaps | Downloads the whole conversation | Correct but slow and costly on mobile data | ## Persist the watermark with the messages The watermark must be written in the **same local transaction** as the messages it covers. If the app saved a watermark of 12 and was killed before saving the messages, it would believe it holds 11 and 12 and never ask for them again. Written together, a crash leaves both old or both new. ## What interviewers listen for - **Highest contiguous, not highest seen** — the cursor never jumps a hole. - **Fetch after the watermark until complete** — paging stops only when the server says so. - **Upsert by id** — overlapping fetches, pushes and socket copies are harmless. - **The server's range answer closes a gap that will never fill** — no waiting forever on a removed message. - **One transaction** — the watermark never claims messages the database does not hold. Candidates who only say "sort by sequence" have described the display, not the gap.
- Should message 12 be shown before 11 arrives?Either hide it until the gap is filled or show it in its sequence position with a loading row above; both are honest. Showing it directly under 10 is not, because a reply can appear before the question it answers. The brief hold usually makes the choice invisible.
- Why does the history response need a 'complete up to N' marker?Because a missing number may never arrive: a removed message leaves a hole the client cannot fill. The marker lets the server say 'nothing else exists up to N', so the client advances its watermark instead of retrying forever; it also tells a paging client when to stop.
- Can a push-delivered message advance the watermark?Only by the same contiguity rule as any other path. A push may arrive late, out of order, or not at all, so a push carrying 15 when the client holds 12 is stored by id while the watermark stays at 12 until 13 and 14 are fetched.
It is like a numbered fax: if page 12 comes through while you hold pages 1 to 10, you do not count yourself up to 12 — you ask the sender to resend everything after page 10 and file 12 once its slot is reached.
- Advance the cursor to the highest sequence number received so far.
- Show message 12 directly under 10 and fix it later.
- Wait for message 11 to arrive on its own, however long it takes.
- Append every message a history fetch returns, since fetches never overlap.
- Save the watermark first and the messages afterwards.