A chat timeline that survives retries

System design scenarios Senior Swift 5.9+ iOS 17+ Reviewed By Alok Singh

Pick the fix

After a reconnect, some messages appear twice and others out of order. Best fix?

Options
Hint 1 of 1

What identifies a message, and what orders it?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Use client-generated ids for dedupe, server sequence numbers for order, and fetch after the last sequence on reconnect

Correct. Identity and order both come from stable sources.

Why not the others?
  • B Order by device timestamp. Clocks drift between devices.
  • C Clear and reload the whole conversation on reconnect. It's slow and loses pending messages.
  • D Dedupe by message text. People send the same text twice.

30-second answer

Give each outgoing message a client-generated id and show it immediately as pending. The server stores it idempotently by that id and assigns a sequence number per conversation. Order the timeline by server sequence, place pending messages at the end, and on reconnect fetch everything after the last sequence you have. Deduplicate by id, so retries and echoes never create duplicates.

Practise saying it · 60 s. No recording, just a timer

Design the chat timeline.

Say your answer out loud, then check which of these points you covered:

  • Client ids
  • Server sequence
  • Pending state
  • Gap detection

Deep dive

Show failed messages with a retry action.

Keep the local database as the timeline's source.

Gaps in sequence numbers trigger a fetch for the missing range.

Typing indicators and read receipts are ephemeral, not part of the timeline.

Common mistakes

  • Ordering by device clock.
  • Creating duplicates when a retry succeeds after a lost response.

Senior take & follow-ups

Seniors separate identity (client id) from order (server sequence).

Follow-up questions

Think of your answer first, then open the hint.

  1. Why not order by timestamp?

    Show hint

    Device clocks differ and drift.

  2. How do you detect missed messages?

    Show hint

    Gaps in the sequence numbers.

Suggest a fix if something here is wrong or out of date.