Idempotent Writes and Signed Webhooks
Retries must be safe, and events must be verifiable.
Hello. In this lesson I will take you through 4 ideas, one at a time.
Why it matters: Retries must be safe, and events must be verifiable. After each idea I will ask you one quick question.
In this lessonFollows you as you read
Idempotency keys
The short version: Networks fail and clients retry.
Networks fail and clients retry. For POST requests that create or charge, accept an Idempotency-Key header; if the same key arrives again, return the original result instead of repeating the action. Store keys with the response for a set period.
Reject reuse of a key with a different request body.
What does an idempotency key prevent?
Webhook design
The short version: Webhooks push events to customers' servers.
Webhooks push events to customers' servers. Send small payloads with an event type, an ID and the resource or a link to it. Sign each delivery, include a timestamp and an event ID, retry with backoff when the receiver fails, and let customers see delivery logs and resend events.
The open Standard Webhooks specification describes headers, signing with HMAC-SHA256 and retry behaviour; following it saves customers work.
Should webhook receivers rely on delivery order?
Ordering and duplicates
The short version: Do not promise ordered delivery; tell customers to use event IDs to deduplicate and to fetch the latest resource state when order matters.
Do not promise ordered delivery; tell customers to use event IDs to deduplicate and to fetch the latest resource state when order matters. Stripe's documentation gives the same advice for its events.
Include the resource's updated time so receivers can ignore stale events.
Which specification describes webhook signing headers?
Secrets
The short version: Give each endpoint its own signing secret, show it once and support rotation with an overlap period where two secrets are valid.
Give each endpoint its own signing secret, show it once and support rotation with an overlap period where two secrets are valid. Never send secrets in payloads.
Document verification with code samples in common languages.
- Accept idempotency keys on creating and charging requests
- Sign webhooks with timestamp and event ID
- Retry with backoff and offer delivery logs
- Tell receivers to deduplicate and not rely on order
- Rotate signing secrets with overlap
Add idempotency keys to one creating endpoint and design a webhook event with signing, retries and a delivery log. Write the verification sample for customers.
Mark it complete to track your progress on this device.