Q&A-1What is a webhook, and what makes them harder than they look?
Answer
The 90-second answer. A webhook is an HTTP POST the provider makes to a URL the consumer registered, so the consumer stops polling. It removes polling latency and the empty responses that polling wastes. The hard part is the direction of control: you are now an HTTP client calling an endpoint you do not own, do not monitor, and cannot fix, and the URL came from a user. Everything else follows from that: at-least-once delivery, no ordering guarantee, retries with backoff and a bounded window, dead-lettering, signature verification, and destination validation.
A receiver that survives it. Python 3.12, Flask 3.0:
import hmac, hashlib, time
from flask import Flask, request, abort
app = Flask(__name__)
SECRET = b"whsec_..." # from the provider dashboard
TOLERANCE = 300 # seconds; Stripe's default timestamp tolerance
@app.post("/hooks/stripe")
def hook():
parts = dict(p.split("=", 1) for p in
request.headers["Stripe-Signature"].split(","))
body = request.get_data() # raw bytes, not the parsed JSON
signed = parts["t"].encode() + b"." + body
expected = hmac.new(SECRET, signed, hashlib.sha256).hexdigest()
if not hmac.compare_digest(expected, parts["v1"]):
abort(400) # forged or corrupted
if abs(time.time() - int(parts["t"])) > TOLERANCE:
abort(400) # replayed old delivery
event = request.get_json()
if not claim(event["id"]): # INSERT ... ON CONFLICT DO NOTHING
return "", 200 # duplicate: already handled
enqueue(event) # do the work off the request
return "", 200Trace it against Stripe-Signature: t=1700000000,v1=<hex>. parts becomes {"t": "1700000000", "v1": "<hex>"}. The body must be the raw bytes: re-serialising the parsed JSON changes whitespace and the digest stops matching. Three branches decide the response: bad digest gives 400, |now - 1700000000| > 300 gives 400, and on a second delivery of the same evt_... id claim() returns False and the handler returns 200 with no side effect. The digest itself is not reproducible by hand, so the trace covers the branches, not the hash.
Why at-least-once is the only honest guarantee. Hypothetical retry schedule, endpoint down at first:
| attempt | t (s) | receiver | response | sender |
|---|---|---|---|---|
| 1 | 0 | DB connection refused | 500 | schedule retry |
| 2 | 10 | DB still down | 500 | schedule retry |
| 3 | 40 | processed, committed | 200 | stop |
| 4 | — | — | — | none |
Now the case that matters: attempt 1 commits and returns 200, but the TCP connection resets before the sender reads the response. The sender cannot distinguish that from a failure, so it retries. The receiver sees the same evt_... id, claim() returns False, and it returns 200 without reprocessing. That is why the dedupe key must be the provider's event id, not a hash of the payload: two legitimate events can carry identical bodies.
Failure modes and how you see them.
- Duplicates. Dedupe table keyed on the event id; alert on the duplicate rate, because a spike means your endpoint is timing out after committing.
- Out-of-order. Neither Stripe nor GitHub guarantees ordering. If order matters, carry a monotonic version per resource and drop stale deliveries; serialising per receiver instead means one slow endpoint stalls every other event for that receiver.
- Retry exhaustion. Stripe retries live-mode events for up to three days with exponential backoff, so a receiver down for a week loses those events permanently. Dead-letter queue plus an alert on the queue depth.
- SSRF. The destination URL is user-supplied. A naive sender will happily POST to
http://169.254.169.254/latest/meta-data/(the EC2 instance metadata endpoint) or to RFC1918 addresses. Allowlist hostnames, resolve DNS and check the resolved IP, and disable redirects or re-check after each hop. - Forgery and replay. Verify the HMAC and the timestamp window as above. GitHub uses
X-Hub-Signature-256(HMAC-SHA256) andX-GitHub-Delivery, a GUID per delivery; GitHub does not redeliver automatically, so a failed delivery is gone unless you redeliver it yourself.
When not to use one. Between systems you control, a durable queue or a retained log is usually better: buffering, backpressure, and replay without custom delivery machinery. Webhooks exist for crossing an organisational boundary where you cannot share a broker.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.