Quick revision
The short answer
Webhook senders usually guarantee neither order nor single delivery, because retries and parallel delivery reorder events and repeat them. The receiver should verify the signature, store the raw event with its id under a unique constraint and return 2xx quickly, then process asynchronously; processing must never move a newer state back to an older one, which we enforce with allowed state transitions or by comparing event times or versions, or we treat each webhook as a signal and fetch the payment's current state from the provider. A periodic reconciliation catches orders left PENDING too long.
What to remember
In this answer, we saw that webhook senders reorder and repeat events, which let a late payment.processing overwrite SUCCEEDED and left orders in PENDING. We restructured the receiver to verify, store the raw event with a unique event id and acknowledge fast, then process asynchronously with allowed state transitions and event-time checks so older events cannot overwrite newer state, considered fetching the current state from the provider on every event, and added a reconciliation job for orders stuck in PENDING.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Webhook senders usually guarantee neither order nor single delivery, because retries and parallel delivery reorder events and repeat them. The receiver should verify the signature, store the raw event with its id under a unique constraint and return 2xx quickly, then process asynchronously; processing must never move a newer state back to an older one, which we enforce with allowed state transitions or by comparing event times or versions, or we treat each webhook as a signal and fetch the payment's current state from the provider. A periodic reconciliation catches orders left PENDING too long.
Why this question is asked
Receiving webhooks looks like the easy side of an integration: the provider calls us, we update a row. The bug in this scenario, orders stuck in PENDING although the payment succeeded, comes from assuming that events arrive once and in order. Neither is promised by most providers. Interviewers ask this because the fix is a set of habits that apply to every event-driven integration. In this answer, we will see why events arrive out of order and twice, restructure the receiver to acknowledge fast, deduplicate by event id, protect the state with transitions or versions, consider the 'webhook as a signal' approach, and add reconciliation as the final safety net.
1Why Events Arrive Out of Order and Twice
A provider sends payment.processing at 10:00:00.100 and payment.succeeded at 10:00:00.400. If the first delivery times out or hits a brief error on our side, the provider retries it a few seconds later, after the second event has already been delivered. Providers also deliver events in parallel from many workers, so even without errors two events sent 300 milliseconds apart can arrive in either order. And if our endpoint processed an event but the provider did not see our response in time, it retries, and we receive the same event twice.
Now look at a naive handler that sets the order's state to whatever the event says. It processes payment.succeeded and sets PAID, then processes the late payment.processing and sets PENDING. The order is stuck, even though the payment succeeded. Duplicates can cause similar damage, for example sending a confirmation email twice.
8 messages in order. Play them, or step through them one at a time.
The late retry overwrote the newer state.
2Acknowledge Fast, Then Process
The receiver should do as little as possible before answering. It verifies the signature, inserts the raw event into an inbox table keyed by the provider's event id, and returns 2xx. A unique constraint on the event id makes deduplication atomic: if the insert fails with a unique violation, we have seen this event, and we return 2xx without doing anything. Processing happens afterwards, in a background worker that reads the inbox.
This structure fixes several problems at once. Fast responses mean fewer provider timeouts, and so fewer retries and fewer reorderings. The stored raw event lets us reprocess after a bug. And deduplication happens once, at the door, instead of in every handler.
3Never Let an Older Event Overwrite a Newer State
The core fix is in processing: an event may only move the order forward. We model the payment states and the allowed transitions explicitly. PENDING can move to PROCESSING, SUCCEEDED or FAILED; PROCESSING can move to SUCCEEDED or FAILED; SUCCEEDED can move to REFUNDED, but never back to PROCESSING. A late payment.processing for an order already in SUCCEEDED is simply ignored and logged.
Where transitions are not enough, for example two updates of the same kind, we compare event order. Many providers include the event's creation time or a version or sequence number on the object. We store the time or version of the last event applied to the order and ignore any event that is older. Doing the check and the update in one SQL statement keeps it safe even when two workers process events for the same payment at the same time.
- Load event from inbox
- Transition allowed from current state?YesContinueNoIgnore and log: stale event
- Event newer than last applied?YesUpdate state and event time in one statementNoIgnore: older event
- Mark event processed
4 steps in order. Play them, or step through them one at a time.
4Alternative: Treat Webhooks as Signals
A simpler and very robust approach is to stop trusting the content of the event for state at all. On any payment event, the worker calls the provider's API, GET /payments/pay_77, and applies whatever the current state is. Order of arrival then no longer matters: a late payment.processing triggers a fetch that returns SUCCEEDED, so the order ends up correct. Duplicates are harmless, because fetching and applying the same state twice changes nothing.
The costs are an extra API call per event, which must stay within the provider's rate limits, and a dependency on the provider's API being available. For payment state, where correctness matters far more than a few hundred milliseconds, it is often the best choice.
5Reconciliation as the Safety Net
Even with all of this, some events never arrive: our endpoint was down for longer than the provider's retry window, or the provider had an incident. So we run a periodic reconciliation job, for example every 15 minutes, that finds orders in PENDING or PROCESSING for longer than a threshold, say 30 minutes, and queries the provider for each one. It applies the real state and records that reconciliation fixed it, which also gives us a metric: if reconciliation fixes many orders, the webhook path has a problem we should investigate.
6Follow-up questions
What happens if your endpoint is down for 6 hours?
The provider retries failed deliveries with backoff, and most providers keep retrying for a day or longer, so events usually arrive once we are back, in a burst and in no particular order, which our inbox and transition rules handle. Events beyond the provider's retry window are lost as webhooks, so reconciliation picks up the orders left pending, and many providers also offer an events API or a dashboard to resend events from a time range, which we can use to backfill. We should also alert on the outage itself, and size the receiver to absorb the burst on recovery.
Should the inbox be in the same database as the orders?
Yes, when possible. Then processing can update the order and mark the event processed in one transaction, so an event is never half-applied, and the dedup check and the state change are consistent.
7Conclusion
In this answer, we saw that webhook senders reorder and repeat events, which let a late payment.processing overwrite SUCCEEDED and left orders in PENDING. We restructured the receiver to verify, store the raw event with a unique event id and acknowledge fast, then process asynchronously with allowed state transitions and event-time checks so older events cannot overwrite newer state, considered fetching the current state from the provider on every event, and added a reconciliation job for orders stuck in PENDING.
Open with
The first thing to accept is that webhook senders usually promise neither order nor exactly-once delivery. A retry of payment.processing can arrive after payment.succeeded, and an event we processed can be redelivered if the provider did not see our response.
Then, if they want more
Our handler just applied whatever each event said, so the late processing event moved a paid order back to PENDING. I would restructure it in three parts. First, acknowledge fast: verify the signature, insert the raw event into an inbox table with the provider's event id as a unique key, and return 2xx.
A duplicate fails the insert, so we return 2xx and do nothing. Processing happens asynchronously from the inbox. Second, never let an older event overwrite a newer state.
I would model allowed transitions, so SUCCEEDED can never go back to PROCESSING, and also store the creation time or version of the last applied event and ignore older ones, doing the check and update in one conditional SQL statement.
An alternative I like for payments is to treat webhooks as signals: on any event, fetch the payment's current state from the provider's API and apply that, which makes order irrelevant. Third, a reconciliation job that queries the provider for orders left pending beyond, say, thirty minutes, because some events will never arrive at all.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How should a webhook receiver handle ordering and duplicates, and how would you...": https://scrivio-interview-library.netlify.app/architecture/api-design-at-scale/2e816af93a/