LESSON 05 · Conversation timeline

Order and sync the history

Give each conversation a stable sequence and each device a recovery cursor.

SYSTEM VIEW

The message path

Diagram view
Order by—
Next position—
Phone cursor—

Watch two clocks put an answer before its question, then let one position per conversation fix the order and set each device’s cursor.

  1. Ben’s laptop stamps “1pm?” 12:00:05. Ana answers one second later, but her phone’s clock runs 3 s slow, so her “yes” is stamped 12:00:03. Sorted by client time, Message history puts her answer before his question.

    Ana’s clock
    3 s slow
    Her answer
    1 s later

    What changed

    • “Ben: 1pm?” stamped 12:00:05
    • “Ana: yes” stamped 12:00:03
    • Answer sorted before question
  2. Now both sends pass a Sequencer, which gives each the conversation’s next position in arrival order: 42 for Ben’s question, 43 for Ana’s answer. Positions only go up; a global message ID gives identity. Kafka likewise orders within a partition, not across a topic.

    Position
    per conversation
    Identity
    global message ID

    What changed

    • Sequencer
    • 42: “Ben: 1pm?”
    • 43: “Ana: yes”
  3. Messages are stored by conversation and position, and read in pages from a cursor. Ben’s phone holds 40 and 41, then gets 43 by live delivery, but 42 is missing. Its cursor, the highest position with no gap below, stays at 41.

    Phone holds
    40, 41, 43
    Cursor
    41

    What changed

    • Ben’s phone
    • 43 by live delivery
    • Cursor 41: 42 missing
  4. On reconnect, Ben’s phone asks for positions after 41 and receives 42 and 43. It already has 43 from live delivery, so it matches the ID and drops the repeat. Its cursor moves to 43, with no gap below.

    Fetch
    after 41
    Receives
    42 and 43

    What changed

    • 42 and 43 fetched
    • Repeat of 43 dropped
    • Cursor 41 → 43
  5. A dense counter needs an atomic increment for every message. When one conversation gets exceptionally hot, every send takes its turn at the Sequencer, which can become the bottleneck. Adding shards doesn’t give strict order for free.

    Each message
    atomic +1
    More shards
    order not free

    What changed

    • One conversation gets hot
    • Sequencer is the bottleneck
  6. Discord gives messages time-sortable Snowflake IDs, so no per-channel counter is needed. The price is gaps: Ben’s phone can’t tell a skipped number from a missing message, so it fetches by range. Try Per-chat counter, where a gap means missing.

    Snowflake
    no counter, gaps
    Also define
    joins, leaves, deletes

    What changed

    • Snowflake IDs replace the counter
    • Ben’s phone fetches by range
    • Per-chat counter: a gap means missing
    IDs from

Space plays · ← → step · Home resets

THE IDEA

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.

DECISION POINT

A phone has seen messages through position 44; a laptop stopped at 39. Both reconnect. What should each request?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

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.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

Why are client timestamps inadequate as the only conversation order?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.