Overview
Curated: · Written: · Reviewed:
Webhooks & Asynchronous API Integration
In one line
A webhook is the provider POSTing an event to a consumer URL. The consumer must be an idempotent receiver that verifies signatures on the raw body, and the provider must run a retrying, per-endpoint delivery queue with a dead-letter path. Reach for webhooks across an organisational boundary where the consumer can expose a public endpoint. Pair them with a polling API so consumers can reconcile missed events.
Key definitions
- At-least-once: a delivery may repeat, because the sender cannot tell a lost response from a lost request, so it retries on any failure without a 2xx.
- Idempotent receiver: a repeat of the same event id produces no second effect.
- Raw-body signature: HMAC-SHA256 over the exact bytes received, checked before parsing.
- Timestamp tolerance: a signed timestamp outside a window (300 s in the Stripe-style example) is rejected, which blocks replay of captured requests.
- Dead-letter queue (DLQ): where an event goes after the retry window, with an alert and a replay path.
- SSRF: the sender is induced to call internal addresses through a consumer-supplied URL.
Formulas and complexity
The example retry policy's window:
Sizing the pool: in-flight deliveries are bounded by workers, so with a 10 s timeout and 50 workers, 5 hung endpoints hold the whole pool.
How it works
- Emit: the producer writes a delivery record durably and returns. It never delivers inline.
- Partition: records route to the partition owned by the endpoint, so one worker delivers them in order.
- Sign and send: the worker signs the exact bytes on the wire with a timestamp and sends with a hard timeout.
- Classify: 2xx is done. Timeout, connection error, 5xx and 429 retry with exponential backoff and jitter. Other 4xx is permanent.
- Receive: the consumer verifies the raw body, parses, inserts the event id under a unique constraint, returns 2xx, then processes asynchronously.
When to use and when not to
| Situation | Webhooks? | Better fit |
|---|---|---|
| Consumer is another organisation with a public endpoint | Yes | n/a |
| Both systems are yours | No | Durable queue or broker |
| Consumer is behind NAT or a firewall | No | Pull API or polling |
| You need replay or backfill | Only if you build it | Retained log |
| You need strict ordering | No | Serialised per-consumer queue, accepting the latency coupling |
| Low volume, latency tolerant | Often no | Polling |
Traps interviewers probe
- Acknowledging before the event is durable. Returning 200 and buffering in memory loses events on SIGTERM or SIGKILL, and the sender sees success. Fix: write the event durably, then return 2xx.
- Verifying re-serialised JSON. The framework encoder changes bytes, so digests disagree on some events. Fix: verify the raw bytes read off the socket, then parse.
- Deduplicating on the body hash or attempt headers. Retries change attempt counters and timestamps inside the body, so hashes differ. Fix: key deduplication on the provider's event id, and make the business effect idempotent under that key.
- Recording the event as processed before the effect commits. A crash between the two leaves the event marked done and the order unpaid. Fix: record the processed marker in the same transaction as the effect.
- Checking private addresses once at registration. DNS rebinding and redirects move the target after the check. Fix: re-validate the resolved address at request time, and after every redirect.
Interview tips
Opening: Delivery is at-least-once, so the consumer must be idempotent. The provider signs, retries with backoff and jitter, isolates endpoints, dead-letters and offers replay.
Expected follow-ups:
- The endpoint is down for six hours: retries over a bounded window, a per-endpoint circuit breaker, DLQ, replay.
- The consumer registers 169.254.169.254: SSRF, re-checked at delivery time.
- Why not Kafka: you cannot hand a broker to a stranger's account.
- Secret rotation: dual-sign grace period, accepting old and new signatures.
- Ordering: not guaranteed. Carry a timestamp or sequence number and ignore older events.
30-second outline:
- Guarantee: at-least-once with consumer idempotency. Exactly-once delivery is not available.
- Sender: enqueue durably, partition per endpoint, sign raw bytes with a timestamp, hard timeout, classify, backoff with jitter, circuit-break, dead-letter, replay.
- Receiver: verify the raw body in constant time, insert the event id under a unique constraint, return 2xx quickly, process asynchronously.
- Security: HMAC with timestamp tolerance, SSRF checks at request time, no response bodies returned to the consumer.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.