Course map

Topic 114 / Advanced

Message ordering and out-of-order events

Chapter 7.3 · Event-Driven Architecture and Delivery Guarantees

What and why

Once many producers and partitions are in play (→ 108), "the order things happened" is no longer automatically "the order things arrive." Ordering guarantees describe what sequence a consumer can rely on — typically per-partition, never global across a whole topic without giving up throughput. Out-of-order events are messages that arrive, or are processed, in a different sequence than they were produced, which consumer logic must handle explicitly. Builds on: → 108.

Diagram

partition (key = order-1):  E1 → E2 → E3     (strict order within this key)
partition (key = order-2):  F1 → F2          (independent — no relationship to order-1)
retry of E1 lands after E3 delivered:  E1 → E3 → E1(retry)   ← must be detected, not applied

How it works

  1. Pick a partition key matching the entity whose events must stay ordered relative to each other (e.g. order ID); the same key always routes to the same partition (→ 108), guaranteeing per-key order.
  2. Accept that order across different keys is not guaranteed, and usually doesn't need to be.
  3. Even within a key, a retried send can land after a later message if retries aren't serialized carefully.
  4. Consumers defend against this by carrying a sequence number or timestamp in the event and checking it before applying a state change — discard an event whose sequence is older than the state already applied.
  5. Cross-system ordering (an event from service A and a related one from service B) has no automatic guarantee at all; the consumer must tolerate either order or synchronize via a shared sequence.

Practical example

(illustrative). A payment-status partition, keyed by payment_id, delivers "Authorized" (seq 1) then "Captured" (seq 2). A network retry re-delivers "Authorized" after "Captured" is already applied. A consumer that checks "is this event's sequence greater than the last one applied for this key" discards the late duplicate instead of reverting captured state back to authorized.

When to use it

  • Use per-key ordering whenever business meaning depends on sequence — state transitions, financial events.
  • Avoid paying for global ordering (a single partition, a single consumer) unless the business genuinely needs one strict timeline across all events — that collapses all parallelism.
  • Failure scenarios: assuming "arrived in this order" means "happened in this order" without a sequence check — it fails exactly when a retry or rebalance (→ 108) reorders delivery; shipping ordering logic that was never tested against an actual reorder.
  • Simpler alternative: if the consumer's operation is naturally commutative or idempotent regardless of order (e.g. "keep the maximum severity seen"), no ordering logic is needed at all — design for that first.

Practice

(vocabulary and mechanism — classification drill; 20 min, lab Kafka). Produce events A, B, C for one key, then deliberately re-deliver A after C to a test consumer; implement the sequence-number check and confirm it discards the late A. Repeat with two different keys interleaved and confirm they're treated as unrelated. Done when the late duplicate is discarded and the two keys are shown to be processed independently.

Shared lab setup

Key takeaway

Order is a per-key guarantee the partitioning scheme gives you, never a global one, and consumers still need a sequence check for retries and rebalances. Done when you can: choose a partition key that preserves the ordering the business needs, and name one case where a retry can reorder delivery. Used next in → 115 (delivery semantics interact with redelivery and reordering), → 124 (stream processing's late-event handling is this problem at scale).

References

Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide