How it works
Client clocks and wall-clock timestamps cannot reliably order concurrent messages. If Ana’s phone runs three seconds slow and she answers Ben one second after his question, sorting by client time puts her answer above it. Instead, assign a monotonically increasing position, a number that only goes up, within each conversation, and use a globally unique message ID for identity. A per-conversation sequence is often enough; the whole product need not share one total order. Kafka makes the same choice, ordering within a partition but not across a topic.
Store messages keyed for the read pattern, such as conversation ID plus sequence, and page forward or backward from a cursor, a marker for a place in the sequence. Each device tracks the highest contiguous position it has applied per conversation, meaning the highest with no gap below it. A device holding 40, 41, and 43 has a cursor of 41. On reconnect it fetches positions after 41, receives 42 and 43, and deduplicates 43, which it already saw through live delivery.
Ordering comes with a coordination cost. A dense counter needs an atomic increment per message, so if one conversation gets exceptionally hot, the sequencer assigning its positions may become a bottleneck; adding shards does not give strict order for free. Discord instead gives messages time-sortable Snowflake IDs, which need no per-channel counter. The price is gaps, so a client cannot tell a skipped number from a missing message and must fetch by range. Membership changes and deletions also need explicit timeline semantics.
A phone has seen messages through position 44; a laptop stopped at 39. Both reconnect. What should each request?
The phone requests positions after 44 and the laptop after 39 for that conversation. Both merge replay with any live arrivals by message ID or sequence, avoiding duplicates and preserving the server-defined order.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Timestamps alone do not define a dependable conversation order.
- Cursors are per device and conversation; replay plus deduplication closes live-delivery gaps.