Lesson 1 of 9 · 24m
Webhook Contracts and the Truth About Delivery Semantics
Design event envelopes, lifecycle semantics, and consumer expectations around the uncomfortable reality that webhooks are at-least-once, unordered messages.
The first webhook system I inherited had beautiful documentation and one fatal sentence in it: "events are delivered exactly once in order." Production believed that sentence for about six weeks. Then a deploy overlapped with a queue backlog, a customer received three invoice.paid events before invoice.created, and the billing integration credited the same invoice twice. Webhooks are not a function call. They are unreliable distributed messaging wearing an HTTP costume.
The honest delivery contract
Unless you have built a transactional protocol with consumer participation — and almost nobody should — your practical guarantee is:
at-least-once delivery
possibly duplicated
possibly reordered across objects and sometimes within one object
possibly delayed by minutes or hours
possibly missing if a subscription is disabled before recoveryThat is not pessimism; it is the design space created by crashes, timeouts, network partitions, deploys, and ambiguous HTTP semantics. If a consumer processes a message, writes state, and then fails before returning 200, you must retry. The consumer will see a duplicate. If two workers deliver different events concurrently, order can invert even when your queue was ordered.
Design the envelope before the endpoint
A mature webhook envelope carries enough metadata for safe consumption:
{
"id": "evt_01J9RB5TCRH6W0K4JVN3VZ6Z4E",
"type": "invoice.paid",
"created": "2026-11-30T18:22:41Z",
"livemode": true,
"api_version": "2026-11-15",
"account": "acct_8h42kq",
"data": {
"object": {
"id": "in_9fb311",
"amount_paid": 4900,
"currency": "usd"
}
}
}The important fields are boring. id supports idempotent processing. created helps diagnostics, not freshness checks by itself. api_version lets consumers pin behavior. type should describe a state transition, not merely a database row mutation. Include enough object data for common processing, but keep a fetch-back path for consumers that need authoritative current state.
Do not expose internal mutation noise
Internal events like row_inserted or job_finished make brittle contracts. Publish business-meaningful transitions: subscription.activated, payment.failed, certificate.issued. A consumer should not need to infer meaning from implementation artifacts. When the same business transition can result from several causes, keep the type stable and put the cause inside the payload.
State clearly what the event does not mean
invoice.paid does not necessarily mean the invoice is still paid when processed; a refund may have happened later. user.deleted does not mean every foreign reference vanished synchronously. Document whether payloads are snapshots, thin references, or deltas. Snapshot payloads are convenient but stale by construction. Thin events are current only after fetch-back, but add coupling and latency.
Field notes
- A timeout is not proof of non-delivery; it is ambiguity. Retry behavior must assume the consumer may have succeeded.
- Never use the HTTP request ID as the event ID. Retried delivery of one logical event must retain the same event ID.
- Version event schemas independently from your REST API version when their compatibility rules differ.
- Include test-mode events and make them impossible to confuse with live events;
livemodeis operationally cheap and incident-saving. - If consumers need strict ordering, offer a sequence or stream API. Do not pretend ordinary webhook retries can provide it.
Was this lesson helpful?