The reality of asynchronous events
When you integrate with a third-party service via webhooks, you are entering an asynchronous world. The service promises to notify your system when something happens, but it makes no guarantees about the order in which those notifications arrive.
In a perfect world, events follow a linear timeline. In the real world, network latency, retries, and distributed system quirks mean that an event that happened at 10:00 AM might not arrive until after an event that happened at 10:05 AM.
The race condition problem
Imagine a hypothetical user profile service. A user, Alice, updates her profile twice in quick succession:
- Update A (Version 1): Sets name to "Alice Smith"
- Update B (Version 2): Sets name to "Alice Jones"
If your system receives these updates in the correct order (A then B), the final state is correct: "Alice Jones".
However, if Update B arrives at your server before Update A, a naive implementation will process B, set the name to "Alice Jones", and then process A, effectively reverting the name to "Alice Smith". Your database is now in a stale state because it accepted an older version of the truth.
Solving for order and idempotency
To build a resilient integration, you need two things: a way to know which event is newer, and a way to ensure that processing the same event multiple times doesn't cause side effects.
1. Ordering Evidence
The most reliable way to handle out-of-order delivery is to include ordering evidence in the webhook payload. This is typically a monotonically increasing integer (a version number) or a high-precision timestamp generated by the source system.
In our example, the payload would look like this:
// Update A
{
"user_id": "user_123",
"name": "Alice Smith",
"version": 1
}
// Update B
{
"user_id": "user_123",
"name": "Alice Jones",
"version": 2
}
When your system receives a webhook, it should compare the incoming version against the last_seen_version stored in your database for that specific entity.
If the incoming version is greater than the stored version, you update the record and the version counter. If the incoming version is less than or equal to the stored version, you recognize it as a delayed or duplicate event and ignore the update.
2. Idempotency and Replay Verification
This logic not only handles out-of-order events but also provides idempotency. If the source system retries the same webhook (Update B) multiple times, your system will see that version 2 has already been processed and will safely ignore the subsequent requests.
When dealing with replays—where a system might re-send events from a previous day—this versioning strategy is your first line of defense. By ensuring that you only ever move forward in the timeline, you protect your system from being rolled back to a previous state by a delayed or replayed event.
Summary
Handling webhooks requires moving away from the assumption of linear time. By requiring explicit ordering evidence and implementing version-aware updates, you can build integrations that remain consistent even when the network delivers events in a chaotic sequence.

