DEV Community

Gautam Govind
Gautam Govind

Posted on

How I Fixed Out‑of‑Order Webhook Events (When “Update” Arrives Before “Create”)

Webhooks are async by design — and that means sometimes events arrive out of order.

I hit this bug last week:

Expected customer.created before customer.updated.

But in production, updated arrived first.

My app tried to update a record that didn’t exist yet.

Why This Happens:

  • Providers don’t guarantee strict ordering.
  • Retries or network latency shuffle delivery.
  • Parallel processing makes it worse.

The Fix:

  1. Design for eventual consistency: Never assume order.
  2. Fetch latest state: On receiving an event, call the provider’s API to confirm resource state.
  3. Queue & reorder: Use Kafka/RabbitMQ/SQS to buffer events and enforce ordering.
  4. Idempotency + retries: Combine with deduplication logic.

Example (Stripe, Node.js):

if (!db.customer[event.data.id]) {
  const customer = await stripe.customers.retrieve(event.data.id);
  db.customer[customer.id] = customer;
}
Enter fullscreen mode Exit fullscreen mode

Takeaway:
Out‑of‑order events are sneaky but solvable. Build for eventual consistency, fetch latest state, and use queues if needed.

I’ve been building Hookmetry to replay events in different orders — so you can test how your system handles async chaos before production.

💬 Have you faced out‑of‑order events? Did you solve it with API fetches, queues, or another trick? Share your fix — let’s build a best‑practice thread together.

Top comments (0)