Skip to content

Lesson 6 of 9 · 24m

Ordering, Concurrency, and Versioning Without False Promises

Use sequencing, object versions, consumer-side reconciliation, and explicit schema versioning instead of pretending distributed webhooks arrive in perfect order.

Ordering questions sound theoretical until a customer receives subscription.cancelled before subscription.activated and disables access for a user who just upgraded. The unpleasant truth is that ordering is expensive in distributed systems and impossible to guarantee once independent workers, retries, and consumer outages enter the picture. Engineering is choosing where ordering is enforced and how much of it you actually need.

Separate event order from object order

You may be able to order events for one aggregate, but not across all objects. Per-object ordering is often sufficient:

customer cus_123: created -> updated -> deleted
invoice in_456:  created -> finalized -> paid

Trying to order cus_123 relative to in_456 usually buys little while destroying parallelism. Partition queues by aggregate ID if you need per-object sequencing. Kafka already provides this model when keyed correctly; simpler systems can shard worker assignments by hash.

Retries still break the illusion

Even a perfect FIFO queue cannot stop this sequence:

worker A delivers event 10, consumer times out
worker A schedules retry
worker B delivers event 11 successfully
consumer records 11 before late retry of 10

If strict ordering is mandatory, the consumer must participate using versions or sequences. Transport alone cannot do it.

Give consumers reconciliation primitives

Include monotonically increasing object versions or sequence numbers where possible:

{
  "id": "evt_01J9S55P",
  "type": "subscription.updated",
  "data": {
    "object": {
      "id": "sub_9182",
      "version": 42,
      "status": "active"
    }
  }
}

The consumer stores the highest processed version per object and ignores or reorders stale transitions. Be careful: numeric version comparison only works when every mutation of the same object shares the same version source. Distributed writers can otherwise allocate versions out of commit order.

Sometimes last-write-wins is the product

For state synchronization integrations, consumers often care about current truth, not every transition. They can process events as hints and then fetch the object. This is the least brittle pattern when intermediate states are not independently meaningful. It is a poor pattern for ledger-like events such as payments, where every transition matters.

Event schema versioning is a compatibility contract

Changing a payload is not merely adding a field. Consumers may persist raw payloads, validate strictly, or branch on enum values. Use explicit versions when semantics change:

2026-01-15: add refund_reason, backward compatible
2026-06-01: remove legacy invoice_number, breaking
2026-06-01: rename customer -> account, breaking

Endpoint-specific API versions help you migrate customers one integration at a time. Deprecate with dates, migration guides, and shadow fields. Do not rely on "JSON consumers ignore unknown fields" as a universal truth; strict validation is common and often wise.

Concurrency controls belong in the consumer too

Consumers processing webhooks asynchronously should lock by object ID, use conditional updates, or write event receipts into an ordered log. A worker fleet that applies state transitions concurrently can reorder even if your receiver accepted deliveries sequentially.

Field notes

  • Creation events often race dependent-object events. Document whether invoice.created implies line items are already fetchable.
  • Deletion events are ordering traps. Consumers may need tombstones rather than immediate row removal.
  • Sequence gaps should trigger reconciliation, not panic. Gaps can mean filtered subscriptions or unpublished failed events.
  • Publicly promise ordering only within the narrow scope you can enforce, and state the exception behavior during retries.
  • Version payload examples in documentation. A sample from two years ago becomes folklore faster than you think.

Was this lesson helpful?

Sign in to rate