Addressing idempotency and security
I need to focus on idempotency: the consumer should use an event ID, and the platform needs to aim for at-most-once delivery, even if it can’t guarantee it. There's a necessity for an idempotency key header and a "dedupe window." For the replay UI, it should display all events, allowing filters by endpoint, status, and type, along with attempts timeline. Security is also crucial: TLS is mandatory with mTLS support and an IP allowlist. I'll note other features like request size limits and endpoint verification.
Below is a practical blueprint / checklist for a “safe webhooks” platform that covers retries, idempotency, signatures (with rotation), and a replay/debug UI.
1) Delivery semantics (baseline)
- At-least-once delivery: assume duplicates can happen; design around it.
- Canonical event identifier: every webhook event gets a globally unique
event_id (UUIDv7/ULID).
- Stable payload: replay should send the exact same JSON bytes (or a canonicalized form) as the original event.
Event envelope (example)
{
"id": "evt_01J…",
"type": "invoice.paid",
"created_at": "2025-12-13T10:00:00Z",
"api_version": "2025-10-01",
"data": { ... }
}
2) Retries & backoff (robust and fair)
When to retry
- Retry on: network errors, timeouts,
408, 425, 429, and 5xx.
- Do not retry on:
2xx (success), most 4xx (treat as permanent failure), except 408/425/429.
- For
3xx: safest is treat as failure unless you explicitly support redirects (redirects can be abused and complicate signing).
Backoff strategy
- Exponential backoff with jitter, e.g.:
- 1m, 5m, 15m, 1h, 3h, 6h, 12h, 24h (cap), with ±20–50% jitter
- Max attempts: commonly 8–12
- Max retry window: commonly 24–72 hours
- Respect
Retry-After when present (esp. for 429/503).
Concurrency / rate limiting
- Per-endpoint concurrency cap (e.g. 5 in-flight requests).
- Per-endpoint QPS limit; if exceeded, queue and/or treat as retryable with delay.
- Use a queue + workers; record each attempt with timestamps, latency, status code, response snippet.
Ordering
- If ordering matters, support per-endpoint FIFO or “partition key ordering” (e.g., per customer/account). Otherwise document that ordering is not guaranteed.
3) Idempotency (receiver safety + platform support)
Receiver guidance
- Deliver
event_id in the body and as a header.
- In docs: “Store processed
event_ids for N days and ignore duplicates.”
- If you send “resource state” events, include resource versioning fields to help dedupe safely.
Platform features
- Header:
Webhook-Id: evt_…
- Optional:
Webhook-Delivery: del_… (unique per attempt) so customers can debug distinct deliveries.
- Optional: “Idempotency-Key” support if you also accept inbound calls; for outbound webhooks,
event_id is typically enough.
Recommended receiver algorithm
- Begin transaction
- If
event_id already processed → return 2xx
- Process → mark
event_id processed → commit → return 2xx
4) Signatures (authenticity + integrity)
Use HMAC signatures with timestamping (Stripe-style is a good proven pattern).
Headers
Webhook-Timestamp: 1734084000 (unix seconds)
Webhook-Signature: v1,kid=key_123,signature=base64…
Webhook-Id: evt_…
Signing string
- Common pattern:
signed = "{timestamp}.{event_id}.{raw_body_bytes}"
sig = HMAC_SHA256(secret, signed)
- Receiver validates:
- timestamp within tolerance (e.g. ±5 minutes)
- signature matches using constant-time compare
Why include timestamp + event_id
- Timestamp helps block replay outside a window.
- Event ID makes the signed message unique even if payloads repeat.
Transport
- Require HTTPS.
- Consider optional mTLS for high-security customers.
5) Secret/key rotation (without breaking customers)
Key model
- Maintain multiple keys per endpoint:
- 1 “active” key used for signing new deliveries
- 1+ “previous” keys accepted for verification for a grace period
Rotation flow
- Generate new secret (key id
kid_new)
- Mark as “active”; keep old as “verifying”
- Sign new deliveries with
kid_new
- Continue to include
kid in signature header so receivers know which key to try first
- After grace period, retire old key
Operational details
- Store secrets in a KMS / secrets manager; never show full secret after creation (show once).
- Provide an API/UI to rotate and to “roll back” quickly if customer misconfigures.
6) Replay / debug UI (safe re-delivery)
A useful UI usually has:
Event timeline
- List events by endpoint, type, status, time,
event_id
- Drill-in view:
- request headers/body (with redaction controls)
- response status, latency, headers
- error category (timeout, DNS, TLS, 5xx, 4xx, signature rejected, etc.)
- attempt history (attempt #, next retry time)
Replay controls
- “Replay event” (enqueue a new delivery attempt)
- “Replay to another endpoint” (test endpoint vs prod)
- “Replay as of now” (new timestamp/signature) vs “Replay original” (generally don’t reuse old timestamp; it will fail replay protection unless explicitly desired)
- Bulk replay by time range / event type (with strong warnings + RBAC)
Safety / governance
- RBAC: who can view payloads and who can replay
- Audit log: who replayed what, when, to where
- Retention policy: store payloads for X days; allow customer-configurable retention
- Redaction: mask secrets/PII fields in UI and logs, but keep raw for delivery if required (or store encrypted + “view requires elevated permission”).
7) Extra hardening that pays off
- Endpoint verification on setup (challenge-response) to avoid typos sending data to wrong host.
- DNS pinning / SSRF protections if you fetch redirects (best: don’t follow redirects).
- Payload size limits; compression support if needed.
- Clear customer docs with code samples to verify signature + dedupe.
- Dead-letter queue + alerting when endpoint has persistent failures.
- Webhook “test mode” separate from prod (separate secrets and event streams).
If you tell me your constraints (multi-tenant? ordering required? max volume/QPS? retention requirements? must support mTLS?), I can propose a concrete spec (headers, signing format, retry schedule) and a minimal schema for event/delivery tables plus UI screens.