Understanding When, How Many Times, and in What Order Webhooks Arrive

Contents

Introduction

My understanding of webhooks was only "a mechanism where the other side POSTs to my URL when something happens." This time I built a stand-in for the sending side and reproduced duplicates, reordering, and a stopped endpoint on my own machine to check what happens.

What is a webhook?

It's a mechanism for having another service tell your app about something that happened there. An email delivery service tells you "delivered" or "opened", and a payment service tells you "the payment is complete". From here on, I call these things that happened events.

With polling, you go and ask. A webhook goes the other way: the other side POSTs to you when something happens.

Sending serviceMy appSending serviceMy appSomething happensRegister the endpoint URL in advancePOST (event)200
Mermaid source
sequenceDiagram
    participant A as My app
    participant S as Sending service
    A->>S: Register the endpoint URL in advance
    Note over S: Something happens
    S->>A: POST (event)
    A-->>S: 200

The sender treats a 2xx response as delivered, and many senders send again if they don't get one (some services don't resend automatically). This is where the number of deliveries and their order start to drift.

How I checked

I built my own stand-in for a service that reports email delivery results. It imitates SendGrid's Event Webhook, and I can trigger duplicates, reordering, and resends through settings.

The receiving side is a small app that stores events and updates the state of each email (NestJS + Prisma + Postgres).

The same event arrives more than once

The trouble is when the receiver has finished saving, but the 200 never reaches the sender.

ReceiverSenderReceiverSenderSaved itTreats it as a failurePOST (event A)200 (lost to a timeout or disconnect)POST (event A) again
Mermaid source
sequenceDiagram
    participant S as Sender
    participant A as Receiver
    S->>A: POST (event A)
    Note over A: Saved it
    A--xS: 200 (lost to a timeout or disconnect)
    Note over S: Treats it as a failure
    S->>A: POST (event A) again

From the sender's point of view it's a failure, so the event you already saved comes again. What's promised is "one or more times", which is apparently called at-least-once.

You build the receiver so that receiving the same event any number of times doesn't change the result. If events carry an ID, you can check it against the saved IDs and drop the second one.

They don't always arrive in order

Resending breaks not only the count but also the order. Failed POSTs are resent separately, so something that happened later can arrive first.

I turned off the stand-in's signing so the receiver kept returning 403 to POSTs without a signature header, and turned it back on after a while (I cover signatures later).

Receiving appSender (stand-in)Receiving appSender (stand-in)11:22:34 processed happens11:22:35 delivered happensResent separately. Signing turned back on midwayprocessed403delivered403delivered (11:22:51)200processed (11:23:05)200
Mermaid source
sequenceDiagram
    participant S as Sender (stand-in)
    participant A as Receiving app
    Note over S: 11:22:34 processed happens
    S->>A: processed
    A-->>S: 403
    Note over S: 11:22:35 delivered happens
    S->>A: delivered
    A-->>S: 403
    Note over S,A: Resent separately. Signing turned back on midway
    S->>A: delivered (11:22:51)
    A-->>S: 200
    S->>A: processed (11:23:05)
    A-->>S: 200

delivered, which happened one second later, arrived 14 seconds earlier. If you overwrite in the order events arrive, the final state goes back to processed.

The receiver needs to be careful that an old event arriving late doesn't roll the state back. This time I defined the order processed → delivered → open → click and only updated when the event moved the state forward. If the sender's API lets you fetch the latest state again, you can also leave it to that.

Resends have a deadline, and what counts as failure depends on the sender

What counts as a failure, and how long resends continue, differs by sender. The stand-in this time treats everything other than 2xx as a failure, so returning 400 doesn't stop resends.

I split the responses by whether resending would fix the problem.

Situation Response Reason
Signature missing or doesn't match 403 It might not be the real sender
A malformed event is mixed in 200 Resending won't fix it no matter how many times. Drop only that one event
An event that doesn't map to any of my data 200 Same as above. Save it as is, unmapped
The DB is down or timed out 500 It will be fixed with time

If you return 400 for a malformed event, the same POST is resent until the deadline, and the valid events that came with it are not saved that whole time.

If the endpoint stops, nothing is lost, but it's delayed

Right after sending, I stopped the endpoint for 40 seconds and then started it again. The stand-in's resend interval doubles: 2, 4, 8, 16, 32 seconds.

11:23:37  attempt=4 error=fetch failed
11:23:53  attempt=5 error=fetch failed   ← the endpoint starts right after this
11:24:25  attempt=6 statusCode=200

Nothing was lost, but after the endpoint started I had to wait 30 seconds for delivery. That's because resends only come at fixed times.

Events past the deadline never come again. If you keep returning 403 or 500 without noticing, the events from that period silently disappear.

Anyone can POST, so verify the signature

Anyone can POST to a webhook URL, so many senders sign the body and send the signature in a header. The scheme differs by sender. The stand-in this time signs the timestamp concatenated with the body before parsing.

Running JSON.stringify on the parsed body again doesn't give you back the original bytes. The stand-in signs the body with \r\n at the end, and just missing that resulted in a 403.

as received  [{"event":"delivered",...}]\r\n  → verification passes
stringify    [{"event":"delivered",...}]      → 403

Many frameworks parse the body as soon as they receive it, so you need a setting that keeps the body before parsing. In NestJS, with rawBody: true it's kept in req.rawBody.

Conclusion

"A mechanism that POSTs to you" alone didn't decide how to build the receiver. Given the premise that events arrive late, any number of times, and in any order, the deduplication, the updates that don't roll the state back, and the way the response codes are split all follow from it.

I hope this helps.

References

Translated from the Japanese original.

Thanks. I'll look into it.

Spotted a mistake?

Tell me what's wrong and I'll fix it. Only I see what you send.