Webhooks 00:00:01 Wait for changes or receive them A payment changes state while the merchant is busy with something else. The merchant can keep asking the provider, or the provider can send news when the change occurs. Neither choice guarantees exactly one arrival. Our app asks for the status of payment 123. The answer is pending. Every 5 seconds the same question gets the same answer. Each call costs both sides work and counts against a rate limit. The payment succeeds just after a poll. We hear it up to 5 seconds later, and 2.5 seconds later on average. We register one HTTPS URL and the events we want. Then we stop asking. When the payment succeeds, the payment company sends one POST to our URL. One event, one call. Polling spends requests to discover whether anything changed. A webhook shifts responsibility to an inbound endpoint that must remain reachable or have a reliable recovery procedure. 00:01:03 Receive before doing expensive work The whole handler: verify, save the event id once, queue, answer 200. The slow work happens afterwards. The sender posts JSON to our public HTTPS endpoint. Keep the raw bytes: the signature covers exactly those. A wrong or missing signature is refused before anything is parsed or stored. Insert the event under its unique id. If the id is already there, it is a duplicate and nothing else happens. The event id goes to the queue. Writing it to an outbox in the same transaction means a crash cannot lose it. The sender is told it worked, in milliseconds. It stops waiting and will not retry. The worker fulfils the order. If it fails, the queue retries it without the sender noticing. Store an accepted event durably before acknowledging the responsibility to process it. A fast acknowledgement made before anything survives a crash can turn a brief outage into permanent business loss. 00:02:03 Verify the provider's evidence Both sides know a secret that never travels. The sender signs the time and the body; we sign them too and compare. The secret was shown once when the endpoint was registered. It is stored on both sides and never sent with an event. It joins the timestamp, a dot and the body, and computes an HMAC with SHA-256 and the secret. The time and signature come in a header, the body comes as bytes. The secret does not travel. More than 300 seconds old (or from the future) is refused. This limits how long a copied message can be replayed. We compute the same HMAC and compare with compare_digest, which takes the same time wherever the first difference is. If someone edits the amount, our HMAC comes out completely different and the request is refused. Verify the signature using the provider's documented algorithm and the required original request bytes. Follow its replay protection guidance and rotate verification material under a planned overlap. 00:03:05 Send again after uncertainty The sender's side: one event, a growing wait after each failure, and a parking place at the end. Event evt_003 is sent at once. The receiver does not answer in time. The receiver answers 503. A 5xx means it is in trouble, so waiting longer is kinder. Still 503. The total so far is 6 minutes. The connection is refused: the receiver is being restarted. 200 OK. Delivered on attempt 5, after 2 hours and 36 minutes. Nobody had to do anything. After the last attempt the event is parked and the customer is told. Old events can be replayed from a dashboard. A delivery attempt can reach the merchant while its acknowledgement is lost. The provider's repeat is then correct behavior, so the consumer must distinguish an event identity from an individual attempt. 00:04:06 A duplicate and an older update arrive Delivery is at least once and order is not promised. Event ids and state checks make both harmless. We save evt_001 and send 200, but the 200 is lost on the way. The sender thinks it failed. The retry has the same event id. The insert finds it and does nothing. We answer 200 so the sender stops. order.shipped (version 3) arrives before order.paid (version 2) because an earlier delivery was retried. Version 2 is not newer than 3, so it is ignored. Without the check the order would go back to paid. Deduplicating one event identifier does not make two different events arrive in business order. A version or authoritative status lookup can prevent an older notification from reversing a later fact. 00:04:56 A customer payment is a business event The payment page belongs to the payment company. We are not in that conversation. The company posts payment.succeeded to us. The worker starts the parcel seconds after the payment, with no polling at all. Persist the decision to fulfill once against the payment identity and confirmed state. If notifications disappear, reconciliation against the provider's records gives the shop a way to discover the missing obligation. A webhook is a notification delivered across an unreliable boundary. Verify the sender, preserve the event, make retries safe and recover the current business state when delivery order is uncertain.