Webhooks, step by step
A customer pays, the payment provider calls your webhook, and the order is marked paid. Until it isn’t, or it ships twice. Watch each failure happen and pick the fix.
3 steps · about 5 minutes · free, no sign-up
Step 1: Lost payments
Your webhook does slow work first, so the provider times out and gives up.
The endpoint does everything before answering, so it takes 12 seconds; the provider gives up and those payments never mark orders paid. What do you do?
- Only deploy at night (Not this one): Fewer people pay at night, but payments still arrive, and the slow 12-second handler still times out at any hour.
- Save the event, answer 200 at once, process it from a queue (Right fix): The endpoint checks the signature, puts the event on a queue and replies in 50 ms. A worker does the slow work. If your endpoint is ever down, the provider retries later and nothing is lost. Acknowledge fast, process later: Webhook providers wait only a few seconds and retry failed calls for hours. Answer quickly after saving the event, and let a queue and worker do the work, so retries can catch up after any outage.
- Ask the provider for the payment list every hour (Helps for a while): A nightly or hourly check is a good safety net and catches anything missed. On its own, though, customers wait up to an hour to see their order paid.
Step 2: Shipped twice
A retry arrives after the first delivery already succeeded, and the order ships twice.
Providers deliver at least once, so the same event sometimes arrives twice, and your worker handles it twice. What do you do?
- Ask the provider to send each event only once (Helps for a while): They try, and duplicates are rare. But a network blip after your 200 means they can’t know you got it, so they must resend. Rare duplicates still happen.
- Turn off retries (Not this one): No duplicates, but any event that fails once is lost forever, and you are back to unpaid orders.
- Record each event ID and skip ones you have seen (Right fix): Before acting, the worker stores the event ID. If it is already there, the event is a repeat and is skipped. Deliveries can repeat; the effect happens once. Idempotency: Making an action safe to repeat: doing it twice has the same effect as once. Store a unique ID per event (or request) and ignore repeats. It is how payments, queues and APIs survive retries.
Step 3: Out of order
A refund arrives before the delayed payment event: the order ends up “paid”.
Events can arrive in any order, and your worker trusts whichever came last, so a refunded order is marked paid again. What do you do?
- Before acting, fetch the latest status from the provider (Right fix): The event is treated as a nudge: "something changed on payment 812". The worker asks the provider for the current status and saves that, so order doesn’t matter. Events as signals, not truth: Don’t trust the order webhooks arrive in. Re-read the current state from the provider (or compare version numbers and timestamps) and apply only changes that move forward.
- Wait 5 seconds before processing each event (Helps for a while): Most late events show up within 5 seconds, so it helps. But some arrive minutes late, and every order now takes 5 seconds longer to update.
- Process events faster, in parallel (Not this one): Parallel workers make ordering worse: two events for the same order can now finish in either order.
What you built
- Fast acknowledgement: Save the event, answer 200, and process it from a queue.
- Retries: Providers resend failed deliveries, so nothing is lost during outages.
- Idempotency: Store event IDs and skip repeats, so retries can’t double an action.
- Read the latest state: Treat events as signals and fetch the current status, so order doesn’t matter.
| Problem | Fix | Trade-off |
|---|---|---|
| Provider times out | Answer fast, queue the work | A queue and worker to run |
| Same event twice | Idempotency (event IDs) | A small table of processed IDs |
| Events out of order | Fetch the latest status | One extra API call per event |
Frequently asked questions
What is a webhook?
An HTTP call one service makes to your URL when something happens, like a payment succeeding. It saves you from asking repeatedly whether anything changed.
Why do webhooks arrive twice?
Providers deliver at least once: if they don’t get a clear success reply (because of a timeout or a network blip), they retry, even if your side already handled it.
How do I make a webhook handler idempotent?
Store each event’s unique ID in a table with a unique constraint and skip events already there, ideally in the same transaction as the change they cause.
How fast should a webhook endpoint respond?
Within a second or two. Verify the signature, store the event, reply 200, and do slow work afterwards from a queue.