Knowledge Guide
HomeSystem DesignScalable Systems (Advanced Topics)

hard Message Ordering: In-Order Retries & Reconstructing Global Order

The order you think you get is not the order you're guaranteed

A distributed queue or stream (Kafka, SQS FIFO, Kinesis) only guarantees order within a single partition — never globally across the whole topic. The moment a producer retries a send, or related events land on different partitions, the order a consumer observes can differ from the order events actually happened in, even though each partition, taken alone, still looks perfectly ordered by offset.

Producer sends event e1 (CREATED) which times out and is retried, while event e2 (SHIPPED) for the same key is sent right after and succeeds first; without an idempotent producer the partition ends up SHIPPED then CREATED (wrong), with per-partition sequence numbers it ends up CREATED then SHIPPED (correct)
Producer sends event e1 (CREATED) which times out and is retried, while event e2 (SHIPPED) for the same key is sent right after and succeeds first; without an idempotent producer the partition ends up SHIPPED then CREATED (wrong), with per-partition sequence numbers it ends up CREATED then SHIPPED (correct)

Traced: how the reorder happens

Order 42 emits two events for the same key: e1 = CREATED (sequence 0), then shortly after, e2 = SHIPPED (sequence 1). The producer has more than one request in flight at once (a common default for throughput):

  1. e1 is sent first, but its acknowledgment is lost to a transient network blip; the producer's client library queues it for retry.
  2. e2 is sent right after, on a separate in-flight request, and succeeds immediately — it lands at offset 5 of the partition.
  3. The producer retries e1; it now lands at offset 6after e2.
  4. The consumer reads offset 5 then offset 6, i.e. SHIPPED, then CREATED — the reverse of what actually happened. A consumer that blindly overwrites a status field ends up with the order stuck at "CREATED" when it was really shipped.

Note this happens within a single partition — partitioning by key alone (see Kafka Internals) is not enough to prevent it, because the retry itself is what reorders the log.

Fix (a): partition by key + a per-partition sequence number

Two things have to be true together. First, route by key (e.g. orderId) so every event for one entity lands on one partition — this is the baseline that gives you any ordering guarantee at all. Second, attach a monotonically increasing per-key/per-partition sequence number to every send, and have the broker (or a de-dup layer) reject or re-slot an out-of-order sequence instead of just appending whatever arrives. Kafka's idempotent producer (enable.idempotence=true) does exactly this: it assigns each producer a producer ID and a per-partition sequence number, and the broker refuses to accept sequence N+1 until it has seen sequence N — so a delayed retry can no longer jump ahead of a message sent after it. This is the fix traced in the diagram above.

Fix (b): reconstructing a trustworthy global order

Partitioning solves ordering within a key, but many systems also need to know the order of events across keys/partitions — e.g. merging per-user event streams into one global audit log. There is no free global order in a partitioned system; you have to build one, and there are three honest ways to do it:

Three partitions with logically-timestamped events feeding a consumer; option A funnels writes through one global sequencer for a true total order, option B buffers and merges by logical timestamp for an approximate order, and option C applies updates directly by comparing version numbers so no ordering is needed
Three partitions with logically-timestamped events feeding a consumer; option A funnels writes through one global sequencer for a true total order, option B buffers and merges by logical timestamp for an approximate order, and option C applies updates directly by comparing version numbers so no ordering is needed

Pitfalls

Judgment layer: which fix for which problem

Takeaways


Re-authored for this guide; diagrams hand-authored as SVG. Follows Kafka's idempotent-producer design (KIP-98), Lamport (1978) logical clocks, and standard last-writer-wins/CRDT practice. See also: Kafka Internals — Partitions, Offsets & Consumer Groups; Time, Clocks & Ordering; Idempotency & "Exactly-Once Is a Myth"; Event Delivery Semantics & Dead Letter Queues.

🤖 Don't fully get this? Learn it with Claude

Stuck on Message Ordering: In-Order Retries & Reconstructing Global Order? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Message Ordering: In-Order Retries & Reconstructing Global Order** (System Design) and want to truly understand it. Explain Message Ordering: In-Order Retries & Reconstructing Global Order from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Message Ordering: In-Order Retries & Reconstructing Global Order** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Message Ordering: In-Order Retries & Reconstructing Global Order** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Message Ordering: In-Order Retries & Reconstructing Global Order** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes