Webhook design

From FerroxLabs/wayland · View interactive version

Webhook design is an AI agent skill from the GitHub repository FerroxLabs/wayland. Guides expert-level webhook design implementation: api-design and automation decision frameworks, production-ready patterns, and concrete templates for webhook design workflows. Use when the user asks about webhook design, webhook design configuration, or api-design best practices for webhook projec

How to use this skill

Skill file

Webhook Design

When to Use

Use this skill when:

Do NOT use this skill when:


Process

1. Establish Delivery Requirements and Constraints

Before writing any code, characterize the delivery requirements precisely. These drive every major design decision.

2. Design the Event Schema and Versioning Contract

The payload schema is the public API contract. Design it with evolution in mind from day one.

3. Design the Delivery Infrastructure

The delivery system is what separates production-grade webhooks from brittle point-to-point HTTP calls.

4. Implement Security: Signing and Verification

Security is non-negotiable for production webhooks. Every payload must be signed.

5. Design Idempotency and Consumer Contracts

Reliable webhook consumers are as important as reliable delivery systems. Design both sides of the contract.

6. Implement Observability and Operational Tooling

Webhooks without observability are impossible to operate at production scale.

7. Handle Registration, Management, and Filtering

The management plane for webhooks is often more complex than the delivery engine.

8. Document and Version the Webhook API

The webhook contract is a public API. Treat it with the same rigor as REST endpoints.


Output Format

When helping a user design or evaluate a webhook system, structure the output as follows:

## Webhook Design Assessment

### Delivery Requirements Summary
| Requirement | Value | Implication |
|-------------|-------|-------------|
| Delivery guarantee | at-least-once / at-most-once | Idempotency required at consumer |
| Expected volume | X events/day, Y peak/sec | Queue infrastructure sizing |
| Acceptable latency | < N seconds p99 | Worker count, queue type |
| Consumer timeout | 30 seconds | Hard limit on delivery attempt |
| Ordering requirement | Per-entity / None | Queue partitioning strategy |
| Multi-tenant | Yes / No | Secret management, isolation |

### Event Schema Template
{
  "id": "evt_01HQZ7K3M4N5P6Q7R8S9T0U1V2",       // ULID or UUID v4
  "type": "{resource}.{action}",                   // e.g., payment.completed
  "version": "2024-01-15",                         // Schema version date
  "api_version": "2024-01-15",                     // Emitting API version
  "created_at": "2024-01-15T10:30:00.000Z",        // ISO 8601 UTC
  "data": {
    // Full current snapshot of the resource
  },
  "previous_attributes": {
    // Only changed fields (for update events), null for creates/deletes
  },
  "metadata": {}                                   // Extensible key-value store
}

### Infrastructure Decision Matrix
| Component | Choice | Rationale |
|-----------|--------|-----------|
| Event queue | PostgreSQL outbox / Redis Streams / SQS | Based on existing stack + volume |
| Retry schedule | Exponential backoff with jitter | 9 attempts over 48 hours |
| Signing algorithm | HMAC-SHA256 | Industry standard, widely supported |
| Circuit breaker | Per-endpoint, 50% error rate threshold | Protects unhealthy consumers |
| Delivery concurrency | N workers per endpoint | Based on consumer capacity |

### Retry Schedule
| Attempt | Delay After Previous | Cumulative Time |
|---------|---------------------|-----------------|
| 1 | Immediate | 0s |
| 2 | 5 seconds | 5s |
| 3 | 30 seconds | 35s |
| 4 | 2 minutes | ~2m |
| 5 | 10 minutes | ~12m |
| 6 | 30 minutes | ~42m |
| 7 | 2 hours | ~2h 42m |
| 8 | 8 hours | ~10h 42m |
| 9 | 24 hours | ~34h 42m |
| Dead letter | -- | After 48h or 10 failures |

### Signature Verification Implementation
// Pseudocode -- adapt to target language
function verifySignature(secret, rawBody, timestamp, receivedSignature):
  if abs(now() - timestamp) > 300:          // 5-minute replay window
    reject("timestamp too old")
  
  expectedSig = HMAC-SHA256(secret, timestamp + "." + rawBody)
  
  if not constantTimeEqual(expectedSig, receivedSignature):
    reject("signature mismatch")
  
  return valid

### Operational Checklist
- [ ] Transactional outbox or durable queue for event persistence
- [ ] Exponential backoff retry with jitter
- [ ] Dead letter queue with alerting on nonzero depth
- [ ] HMAC-SHA256 signing with per-endpoint secrets
- [ ] Replay attack prevention (timestamp validation)
- [ ] Circuit breaker per endpoint
- [ ] Delivery log with full attempt history
- [ ] Idempotency key tracking at consumer
- [ ] Manual resend capability
- [ ] Endpoint health dashboard
- [ ] Event catalog documentation

### Recommended Architecture
{Narrative description of the recommended approach specific to user's context,
explaining queue choice, worker topology, and trade-offs for their specific
volume, latency, and team constraints}

Rules

  1. NEVER deliver webhooks synchronously in the application request path. Synchronous delivery means a slow or unreachable consumer blocks your application's response time and couples consumer availability to your system's availability. Always write to a durable queue first and dispatch asynchronously.
  1. NEVER use shared secrets across endpoints or tenants. Each registered endpoint must have its own cryptographically random 32-byte secret. Shared secrets mean a compromised consumer can forge events for other consumers and make secret rotation catastrophic.
  1. ALWAYS sign with the raw request body bytes, not a re-serialized version. JSON serialization is not deterministic across libraries and versions. Re-serializing a parsed payload can change field order or numeric formatting, causing valid payloads to fail signature verification. Capture the raw bytes at send time and sign those.
  1. NEVER silently drop events. Every event that fails delivery after exhausting retries must go to a dead letter queue and trigger an alert. Silent drops make the system unreliable in ways that are invisible to operators until a customer reports missing data -- often hours or days later.
  1. ALWAYS use constant-time comparison for signature verification. Standard string equality returns early on the first non-matching byte, leaking information about how many bytes of the signature are correct. This timing oracle can allow an attacker to forge signatures byte-by-byte. Use hmac.compare_digest, crypto.timingSafeEqual, or equivalent.
  1. NEVER guarantee exactly-once delivery over plain HTTP. Network conditions mean you cannot know if a 200 response was lost in transit after processing. Design for at-least-once delivery and require consumers to implement idempotency. Claiming exactly-once semantics without acknowledgment infrastructure misleads consumers into building brittle systems.
  1. ALWAYS include the full resource snapshot in event payloads, not just the delta or a notification. "Notification-style" webhooks that say "something changed, go fetch it" require consumers to make an API call for every event, creating N+1 latency, coupling consumer processing to your API availability, and doubling infrastructure load during high-event periods.
  1. NEVER retry on HTTP 4xx responses (except 429). A 400, 401, or 404 response means the consumer has explicitly rejected the event -- retrying will never succeed and wastes resources. Only 5xx responses and network errors warrant retry. Honor 429 with the Retry-After header value.
  1. ALWAYS implement per-endpoint circuit breakers before deploying at scale. Without circuit breakers, a single failing consumer endpoint can exhaust your retry queue workers, delaying delivery to all other healthy endpoints. Circuit breakers isolate unhealthy endpoints and preserve delivery capacity for the healthy ones.
  1. NEVER use sequential integer IDs for webhook event IDs. Sequential IDs leak information about your event volume to consumers (an attacker can compute your throughput by comparing IDs over time). Use UUID v4 or ULID. ULID has the advantage of being lexicographically sortable by time, which simplifies delivery log queries without exposing monotonic counters.
  1. ALWAYS document the ordering guarantee -- or lack thereof -- explicitly. Most webhook systems provide no global ordering guarantee. If you do not document this, consumers will build systems that assume ordering and fail in subtle, hard-to-diagnose ways when events arrive out of order during a retry scenario.
  1. NEVER log the full webhook secret in application logs. Logging secrets exposes them to anyone with log access and makes secret rotation mandatory after any log breach. Log the first 4 characters followed by **** (e.g., sk_ab****) for correlation purposes if needed, nothing more.

Edge Cases

Consumer Endpoint Consistently Times Out at 25--30 Seconds

Some consumers perform synchronous database writes or third-party API calls in the webhook handler path, causing them to consistently respond near the timeout boundary. This is dangerous because network variance can push them over the limit, causing your system to treat delivered events as failures and retry them -- creating duplicate processing at the consumer.

Handling: Document the 5-second target response time prominently. For known slow consumers, implement an acknowledgment-then-process pattern: the consumer endpoint writes the raw payload to a local queue (Redis, SQS, database) and immediately returns 200. A separate worker processes from that local queue. Include this pattern with code samples in your integration documentation. If you observe a specific endpoint consistently responding in 20--30s, proactively alert the endpoint owner.

Webhook Event Storm After Downstream Outage Recovery

If a consumer endpoint is down for 2 hours, your retry queue accumulates hundreds or thousands of pending events. When the endpoint recovers, naive delivery immediately dispatches all queued events simultaneously, potentially overwhelming the freshly recovered consumer and causing a second failure.

Handling: Implement throttled recovery dispatch: when a circuit breaker transitions from open to half-open, limit dispatch to 10% of normal concurrency for the first 5 minutes. If success rate exceeds 95% in that window, increase to 50%, then to 100% after another 5 minutes. Never dump the full accumulated queue immediately. Also implement max burst rate per endpoint (e.g., no more than 100 events/minute to a single endpoint) independent of circuit breaker state.

Multi-Tenant Secret Rotation Without Breaking Active Consumers

A customer requests a secret rotation -- or rotation is required after a suspected compromise. The consumer has the old secret embedded in their code and cannot rotate instantly (their deployment pipeline takes 30--60 minutes).

Handling: Implement a dual-secret grace period: when a new secret is generated, mark the endpoint as having both secret_v1 and secret_v2. For every delivery, compute both signatures and include both in the header: X-Webhook-Signature: t=1699999999,v1=oldSigHex,v2=newSigHex. The consumer can verify against either. After 24 hours (or an explicit "rotation complete" confirmation from the customer), invalidate the old secret. Never extend the grace period beyond 72 hours.

Event Schema Breaking Change Required

A business requirement forces a field rename, type change, or field removal in an event payload that existing consumers depend on.

Handling: Never make breaking changes in-place. Instead: (1) introduce a new event type version with a date suffix (e.g., payment.completed becomes payment.completed.2024-06) -- serve both versions simultaneously, (2) add a X-Webhook-Deprecation-Notice: payment.completed is deprecated, migrate to payment.completed.2024-06 by 2025-01-01 header on the old version, (3) provide a migration guide with a diff of the schema change, (4) maintain both versions for a minimum of 6 months, (5) suppress delivery of the deprecated version only after confirming the endpoint has successfully received the new version. If your volume is high enough, offer consumers a "schema migration dry run" endpoint that shows them what their events will look like under the new schema.

Webhook Replay Attacks via Captured Requests

An attacker intercepts a valid webhook delivery (e.g., via a compromised network segment or a confused consumer that logs raw requests to an accessible location) and replays the identical request to the consumer endpoint.

Handling: The timestamp-in-signature approach (described in step 4) limits the replay window to 5 minutes. Enforce this rigorously at the consumer. Additionally, consumer-side idempotency (checking event.id against a processed-IDs store with a 48--72 hour TTL) provides a second layer: even a request replayed within the 5-minute window that somehow passes timestamp validation will be caught by the idempotency check. Document both layers as required in your consumer integration guide, not optional.

Fan-Out to Large Numbers of Registered Endpoints for a Single Event Type

A popular event type (e.g., payment.completed) has 5,000 registered endpoints across all tenants. A single high-volume event source generates 500 events/minute, resulting in 2.5 million delivery attempts/minute for that event type alone.

Handling: Fan-out must be parallelized at the infrastructure level, not a sequential loop. Publish the event once to a topic queue, then use a fan-out worker pool that reads from the topic and enqueues a per-endpoint delivery task for each registered endpoint -- do not deliver directly from the fan-out worker. Use separate worker pools for fan-out and for actual delivery. This means a burst of 500 events does not block delivery of earlier events while fan-out is computed. For the very highest volumes (>1M deliveries/minute), consider a two-tier architecture: aggregate fan-out decisions in a batch job that runs every 1--5 seconds and writes bulk delivery tasks.

Consumer Returns 200 But Logs Show It Did Not Process the Event

This happens when consumers swallow exceptions and always return 200 to avoid retries, even when processing failed internally. This is a consumer implementation bug, but it is common enough to design for.

Handling: You cannot detect this from the delivery side -- a 200 is a 200. Address it through: (1) documentation that explicitly warns against swallowing errors -- consumers should return 5xx if processing fails so delivery is retried, (2) providing a manual resend API (POST /webhooks/events/{event_id}/resend) that the consumer can trigger after they fix their processing bug, (3) suggesting consumers implement a consumer-side dead letter queue for events that fail local processing after acknowledgment. The resend API is essential here -- it is the recovery mechanism when the consumer acknowledges correctly but fails to process.


Example

Input: "We're building a SaaS payment platform. When a payment is processed, we need to notify our merchant customers' systems. We have about 200 merchants, expect up to 5,000 payment events per day, and some merchants are on legacy PHP systems that can take up to 20 seconds to respond. We're using PostgreSQL and Node.js. What webhook system design do you recommend?"

Output:

Webhook Design Assessment

Delivery Requirements Summary

| Requirement | Value | Implication |

|-------------|-------|-------------|

| Delivery guarantee | at-least-once | Idempotency required at merchant consumer |

| Expected volume | 5,000 events/day (~3.5/min average, ~50/min peak) | Single PostgreSQL-backed queue is sufficient |

| Acceptable latency | Under 5 minutes p99 for normal delivery | Standard retry schedule, no real-time constraint |

| Consumer timeout | 30 seconds (with 20s observed for slow consumers) | Publish consumer async-processing pattern prominently |

| Ordering requirement | Per-payment best-effort, no global ordering | Standard queue, no partitioning needed |

| Multi-tenant | Yes -- 200 merchant endpoints | Per-merchant secrets, isolation, circuit breakers |

At 5,000 events/day and 200 merchants, this is well within what a PostgreSQL-backed outbox table and a small Node.js worker pool can handle without specialized queue infrastructure. You do not need Redis Streams or SQS at this scale -- PostgreSQL is the right choice given your existing stack.


Event Schema

Every payment event must use this envelope:

{
  "id": "evt_01HQZ7K3M4N5P6Q7R8S9T0U1V2",
  "type": "payment.completed",
  "version": "2024-01-15",
  "api_version": "2024-01-15",
  "created_at": "2024-01-15T10:30:00.123Z",
  "data": {
    "payment_id": "pay_9XKL2MN3",
    "merchant_id": "merch_ABC123",
    "amount": 4999,
    "currency": "USD",
    "status": "completed",
    "customer_email": "buyer@example.com",
    "metadata": {
      "order_ref": "ORD-2024-8821"
    },
    "completed_at": "2024-01-15T10:29:58.000Z"
  },
  "previous_attributes": null,
  "metadata": {}
}

Key decisions:

Publish these event types initially: payment.completed, payment.failed, payment.refunded, payment.chargeback.initiated. Resist adding more until merchants request them -- a smaller catalog is easier to maintain.


Infrastructure Design

Transactional Outbox Table (PostgreSQL):

CREATE TABLE webhook_outbox (
  id            TEXT PRIMARY KEY,          -- ULID of the event
  merchant_id   TEXT NOT NULL,
  endpoint_id   TEXT NOT NULL,
  event_type    TEXT NOT NULL,
  payload       JSONB NOT NULL,
  status        TEXT NOT NULL DEFAULT 'pending',  -- pending, delivered, failed, dead_lettered
  attempt_count INTEGER NOT NULL DEFAULT 0,
  next_retry_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  last_attempted_at TIMESTAMPTZ,
  last_response_status INTEGER,
  last_response_body TEXT,
  delivered_at  TIMESTAMPTZ
);

CREATE INDEX idx_outbox_next_retry ON webhook_outbox (next_retry_at)
  WHERE status = 'pending';

CREATE INDEX idx_outbox_endpoint_status ON webhook_outbox (endpoint_id, status);

Insert into this table within the same database transaction as recording the payment. This is the outbox pattern -- if the payment write succeeds, the event will eventually be delivered. If the transaction rolls back, no event is emitted.

Worker process (Node.js):

Run a background worker on a 5-second poll interval:

async function dispatchPendingEvents() {
  // Claim a batch of due events with a row-level lock
  const events = await db.query(`
    SELECT * FROM webhook_outbox
    WHERE status = 'pending' AND next_retry_at <= NOW()
    ORDER BY next_retry_at
    LIMIT 50
    FOR UPDATE SKIP LOCKED
  `);
  
  // Process up to 10 events per endpoint concurrently, all endpoints in parallel
  await Promise.all(events.map(event => deliverEvent(event)));
}

FOR UPDATE SKIP LOCKED allows multiple worker processes to run safely without double-delivering -- this is PostgreSQL's built-in advisory lock for queue patterns.

Delivery function with retry logic:

const RETRY_DELAYS_MS = [0, 5000, 30000, 120000, 600000, 1800000, 7200000, 28800000, 86400000];

async function deliverEvent(event) {
  const endpoint = await getEndpoint(event.endpoint_id);
  
  if (endpoint.circuit_open && endpoint.circuit_open_until > new Date()) {
    // Skip -- circuit is open, reschedule for after circuit-open period
    await rescheduleAfterCircuit(event, endpoint.circuit_open_until);
    return;
  }
  
  const timestamp = Math.floor(Date.now() / 1000).toString();
  const rawBody = JSON.stringify(event.payload);
  const signature = computeHmacSha256(endpoint.secret, `${timestamp}.${rawBody}`);
  
  let responseStatus, responseBody;
  
  try {
    const response = await fetch(endpoint.url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Webhook-ID': event.id,
        'X-Webhook-Timestamp': timestamp,
        'X-Webhook-Signature': `t=${timestamp},v1=${signature}`,
        'User-Agent': 'YourPlatform-Webhooks/1.0'
      },
      body: rawBody,
      signal: AbortSignal.timeout(30000)  // 30-second hard timeout
    });
    
    responseStatus = response.status;
    responseBody = (await response.text()).substring(0, 500);
  } catch (networkError) {
    responseStatus = 0;  // Network error
    responseBody = networkError.message;
  }
  
  const succeeded = responseStatus >= 200 && responseStatus < 300;
  const permanentFailure = responseStatus >= 400 && responseStatus < 500 && responseStatus !== 429;
  const nextAttempt = event.attempt_count + 1;
  
  if (succeeded) {
    await markDelivered(event.id, responseStatus, responseBody);
    await recordEndpointSuccess(endpoint.id);
  } else if (permanentFailure || nextAttempt > RETRY_DELAYS_MS.length) {
    await markDeadLettered(event.id, responseStatus, responseBody);
    await alertOpsTeam(event, endpoint, responseStatus);
  } else {
    const delayMs = RETRY_DELAYS_MS[nextAttempt] * (0.8 + Math.random() * 0.4);  // ±20% jitter
    await scheduleRetry(event.id, nextAttempt, delayMs, responseStatus, responseBody);
    await recordEndpointFailure(endpoint.id, responseStatus);
  }
}

Retry Schedule

| Attempt | Delay After Previous | Cumulative Time |

|---------|---------------------|-----------------|

| 1 | Immediate | 0s |

| 2 | ~5 seconds | ~5s |

| 3 | ~30 seconds | ~35s |

| 4 | ~2 minutes | ~2.5m |

| 5 | ~10 minutes | ~12.5m |

| 6 | ~30 minutes | ~42.5m |

| 7 | ~2 hours | ~2h 43m |

| 8 | ~8 hours | ~10h 43m |

| 9 | ~24 hours | ~34h 43m |

| Dead letter | -- | After 9 failures or >48h |


HMAC Signature Verification (PHP -- for your legacy merchants)

function verifyWebhookSignature(string $secret, string $rawBody, string $signatureHeader): bool {
    // Parse header: "t=1699999999,v1=abc123..."
    preg_match('/t=(\d+)/', $signatureHeader, $timestampMatch);
    preg_match('/v1=([a-f0-9]+)/', $signatureHeader, $sigMatch);
    
    if (!$timestampMatch || !$sigMatch) {
        return false;
    }
    
    $timestamp = (int) $timestampMatch[1];
    $receivedSig = $sigMatch[1];
    
    // Reject events older than 5 minutes
    if (abs(time() - $timestamp) > 300) {
        return false;  // Replay attack or clock skew beyond tolerance
    }
    
    $expectedSig = hash_hmac('sha256', $timestamp . '.' . $rawBody, $secret);
    
    // Constant-time comparison -- never use === for signature comparison
    return hash_equals($expectedSig, $receivedSig);
}

// Usage in webhook handler:
$rawBody = file_get_contents('php://input');
$signatureHeader = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';

if (!verifyWebhookSignature(WEBHOOK_SECRET, $rawBody, $signatureHeader)) {
    http_response_code(401);
    exit;
}

// Idempotency check
$event = json_decode($rawBody, true);
if (hasProcessedEvent($event['id'])) {
    http_response_code(200);  // Already processed -- acknowledge without reprocessing
    exit;
}

// Write to local processing queue BEFORE doing any real work
enqueueForProcessing($event);
markEventReceived($event['id']);

http_response_code(200);  // Respond immediately -- processing is async

Provide this exact code in your merchant documentation. Do not make merchants figure out PHP HMAC from scratch -- every merchant integration bug you prevent is a support ticket you avoid.


Handling the 20-Second Response Time Problem

For your legacy PHP merchants, the 20-second response time is a red flag. Document and enforce this pattern in your integration guide:

// CORRECT: Write to queue immediately, return 200, process asynchronously
register_shutdown_function(function() use ($event) {
    // This runs AFTER the response is sent
    processPaymentEvent($event);
});
// Or better: use a proper job queue (Beanstalkd, database-backed queue)
enqueueJob('process_payment_webhook', $event);
http_response_code(200);
exit;  // Returns 200 immediately

// WRONG: Do not do this
processPaymentEventSynchronously($event);  // 15-20 second operation
http_response_code(200);  // Too late -- our system may have already timed out

Set your delivery timeout to 30 seconds (the standard), not 20 seconds. The 20-second response time from legacy merchants is within the window -- they will not time out consistently. But flag any endpoint with p99 response time above 10 seconds in your monitoring and proactively contact those merchants to fix their handlers.


Circuit Breaker Configuration

CREATE TABLE endpoint_health (
  endpoint_id       TEXT PRIMARY KEY,
  consecutive_failures INTEGER DEFAULT 0,
  circuit_state     TEXT DEFAULT 'closed',  -- closed, open, half-open
  circuit_open_until TIMESTAMPTZ,
  last_success_at   TIMESTAMPTZ,
  failure_rate_1h   NUMERIC(5,2) DEFAULT 0
);

Circuit breaker rules for your 200-merchant scale:


Operational Checklist

What to Build First vs. Later

Week 1 -- Minimum viable (ship this):

Week 2--3 -- Production hardening:

Month 2 -- Full feature set:

At 5,000 events/day across 200 merchants, this system will handle your current scale with headroom to grow 10x without architectural changes. When you reach 50,000 events/day, revisit whether the PostgreSQL outbox needs to be replaced with a dedicated queue -- at that point you will have the operational data to make that decision with real numbers rather than speculation.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.