Prompt
I'm building an API product and need webhook retry logic and delivery logs
Latest observation
A solid webhook system usually needs two separate but related pieces:
- Retry delivery logic — so failed webhook attempts are retried safely.
- Delivery logs / observability — so you and your customers can inspect what happened.
Here’s a practical design you can use.
1) Webhook delivery model
Each webhook event should create a delivery record per subscriber endpoint.
Core entities
-
Webhook Subscription
idcustomer_idurlsecretenabledevent_typescreated_at
-
Webhook Event
idevent_typepayloadcreated_atidempotency_key/event_uuid
-
Webhook Delivery
idevent_idsubscription_idattempt_numberstatus(pending,success,failed,retry_scheduled,dead_letter)http_statusrequest_headersresponse_body(truncated)error_messagenext_retry_atcreated_atupdated_at
2) Retry logic
Recommended retry policy
Use exponential backoff with jitter.
Example:
- Attempt 1: immediate
- Attempt 2: 1 min
- Attempt 3: 5 min
- Attempt 4: 15 min
- Attempt 5: 1 hour
- Attempt 6: 6 hours
- Then mark as failed/dead-letter
Add random jitter to avoid retry storms.
Retry only on transient failures
Retry for:
- Network timeouts
- DNS failures
- Connection resets
- HTTP
408 - HTTP
429 - HTTP
5xx
Do not retry for most 4xx responses:
400,401,403,404,422These typically indicate a permanent issue.
Success criteria
Treat as success if:
- HTTP status is
2xx
Optionally also allow: 3xxonly if you explicitly want to follow redirects, though most webhook systems avoid this.
Timeout behavior
Set a strict timeout for webhook delivery:
- Connect timeout: 2–5s
- Total request timeout: 5–10s
Don’t let delivery workers hang indefinitely.
Idempotency
Assume retries may duplicate events. Include:
event_iddelivery_idattemptsignature
Customers should dedupe using event_id or your X-Webhook-Id header.
3) Delivery request format
Send headers like:
POST /webhooks HTTP/1.1
Content-Type: application/json
X-Webhook-Id: evt_123
X-Webhook-Type: invoice.paid
X-Webhook-Timestamp: 1730000000
X-Webhook-Signature: sha256=...
User-Agent: YourProduct-Webhooks/1.0
Payload example:
{
"id": "evt_123",
"type": "invoice.paid",
"created_at": "2026-11-25T10:00:00Z",
"data": {
"invoice_id": "inv_456",
"amount": 2500,
"currency": "usd"
}
}
Sign the raw body using HMAC SHA-256 with the subscription secret.
4) Delivery logs
You want logs for both:
- internal debugging
- customer-visible audit history
Log fields to store
For each attempt:
- Delivery ID
- Event ID
- Subscription ID
- Attempt number
- Request timestamp
- Response timestamp
- Duration ms
- HTTP status
- Error type
- Error message
- Response body snippet
- Retry scheduled time
- Final outcome
What to expose in your UI/API
A customer should be able to see:
- Event name
- Endpoint URL
- Delivery status
- Number of attempts
- Last attempt time
- HTTP response status
- Error reason
- Full request headers
- Full response body if safe, or truncated
- Replay button
Suggested delivery states
pendingsendingsuccessretry_scheduledfaileddead_letterdisabled(if subscription turned off)
5) Replay support
Very useful for customers.
Allow them to manually replay:
- a failed delivery
- a successful delivery
- all deliveries for an event
Replay should:
- create a new delivery attempt or new delivery record
- preserve original event ID
- maintain an audit trail
6) Queue + worker architecture
A reliable pattern:
- Event created in your app
- Persist event + delivery records
- Enqueue delivery job
- Worker sends webhook
- Worker records result
- If retryable failure, schedule next retry
Good infrastructure options
- Queue: SQS, RabbitMQ, Redis queue, Kafka
- Scheduler: delayed jobs / retry queue / cron poller
- DB: Postgres for source of truth
Use the DB as the canonical record and the queue for execution.
7) Abuse and safety protections
Add:
- Per-endpoint rate limits
- Retry caps
- Circuit breaker for consistently failing endpoints
- Endpoint disablement after repeated hard failures
- Payload size limit
- Secret rotation support
If an endpoint fails repeatedly, consider:
- pausing deliveries
- notifying the customer
- requiring re-verification
8) API endpoints you may want
Manage subscriptions
POST /webhook-subscriptionsGET /webhook-subscriptionsPATCH /webhook-subscriptions/{id}DELETE /webhook-subscriptions/{id}
Delivery logs
GET /webhook-deliveriesGET /webhook-deliveries/{id}GET /webhook-events/{id}/deliveries
Replay
POST /webhook-deliveries/{id}/replay
9) Minimal retry algorithm
Pseudo-logic:
attempt delivery
if 2xx:
mark success
else if retryable and attempts < max_attempts:
compute backoff delay
mark retry_scheduled
enqueue next attempt
else:
mark failed or dead_letter
Retryable failure detection:
- timeout / network error
- 408 / 429 / 5xx
10) Recommended defaults
A good starting config:
- Max attempts: 8
- Timeout: 10s total
- Backoff: exponential with jitter
- Retry on: network errors, 408, 429, 5xx
- Log retention: 30–90 days
- Payload retention: depends on your privacy model
- Dead-letter after max attempts
If you want, I can also give you:
- a database schema for this,
- production-grade pseudocode for the retry worker,
- or a Node/Python implementation with logs and replay endpoints.