In an API-first ecosystem, webhooks are the primary mechanism for real-time inter-system communication. While building a naive webhook dispatcher (a background job issuing an HTTP POST request) takes ten lines of code, operating a production webhook delivery engine capable of dispatching millions of events per hour across thousands of customer endpoints is a formidable distributed systems and security challenge.
Third-party recipient servers crash, drop TCP packets, respond with 502 Bad Gateway errors, or hang indefinitely behind slow firewalls. Furthermore, accepting arbitrary user-supplied URLs creates severe Server-Side Request Forgery (SSRF) and DNS rebinding vulnerabilities that can compromise internal cloud infrastructure.
In this architectural guide, we dissect how platforms like Stripe and Svix engineer webhook delivery engines: establishing asynchronous queue architectures, mitigating SSRF, implementing tamper-proof HMAC signatures, tuning exponential backoff with full jitter, and operating Dead Letter Queue (DLQ) reconciliation pipelines.
---1. The Production Dispatch Pipeline
A reliable webhook engine separates event generation from network transmission using a multi-stage asynchronous pipeline:
[ Transactional Database ]
│ (Transactional Outbox Pattern)
▼
[ Event Bus / Kafka / Redis Streams ]
│
▼
[ Webhook Fanout Worker ] ──> Evaluates active customer subscriptions
│
▼
[ Dispatch Priority Queues ] (High, Normal, Retry)
│
▼
[ Outbound HTTP Dispatcher Pool ] (Isolated Egress Proxy)
│
├───> [ Success 2xx ] ──> Mark Delivered
└───> [ Failure / Timeout ] ──> Schedule Retry (Jittered Backoff) ──> DLQ
By decoupling the fanout evaluation from outbound HTTP requests, slow third-party servers can never backpressure the primary application database.
---2. Security Architecture: Eliminating SSRF and DNS Rebinding
Allowing users to register arbitrary destination URLs (e.g., https://api.customer.com/webhooks) invites catastrophic Server-Side Request Forgery (SSRF). An attacker could register http://169.254.169.254/latest/meta-data/ to steal AWS IAM credentials or target internal services (http://10.0.1.15:6379).
2.1. The DNS Rebinding Trap
Naive URL validation checks the hostname IP before making the request. However, with DNS Rebinding, an attacker's DNS server returns a benign public IP on the first resolution, but responds with 127.0.0.1 or 169.254.169.254 when the HTTP client actually establishes the TCP connection.
2.2. Robust Egress Architecture
- Dedicated Egress Gateway: Route all outbound webhook traffic through an isolated egress proxy (such as Envoy or Smokescreen) operating in a network sandbox with zero access to internal VPC subnets.
- Pin Resolved IP at the Socket Level: Perform DNS resolution once, validate that the IP does not belong to private RFC 1918, RFC 3927 (link-local), or loopback ranges, and open the raw TCP socket directly to that validated IP:
// Go custom HTTP dialer pinning validated IP
dialer := &net.Dialer{Timeout: 5 * time.Second}
transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, _ := net.SplitHostPort(addr)
ips, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
if err != nil || len(ips) == 0 {
return nil, fmt.Errorf("DNS resolution failed")
}
ip := ips[0]
if ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
return nil, fmt.Errorf("SSRF attempt detected: forbidden destination %s", ip)
}
return dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), port))
},
}
---
3. Cryptographic Verification: HMAC-SHA256 Signatures
Receiving servers must cryptographically verify that an incoming payload originated from your platform and was not tampered with in transit. The industry standard uses HMAC-SHA256 with timestamped nonces:
POST /webhook HTTP/1.1
Host: api.customer.com
Webhook-Id: msg_2v8k109XzQ
Webhook-Timestamp: 1795248000
Webhook-Signature: v1,7d89ab52c93847291a...
Signature Construction
The signature string is generated by signing the timestamp concatenated with the exact raw byte payload:
Signature Payload = "msg_id." + timestamp + "." + raw_body_bytes
Signature = HMAC_SHA256(customer_secret, Signature Payload)
Including the timestamp protects consumers from replay attacks: receiving endpoints can reject any payload whose timestamp is older than 5 minutes.
---4. Retry Strategies: Exponential Backoff with Full Jitter
When an endpoint returns a non-2xx status code or times out (strictly enforced at 5 to 10 seconds), the engine must retry. However, naive exponential backoff (e.g. retry at 1s, 2s, 4s, 8s) causes thundering herd problems where thousands of retrying jobs hammer an already struggling consumer server at the exact same second.
The Full Jitter Formula
The mathematically proven optimal backoff algorithm introduces randomized uniform jitter:
import random
def calculate_backoff(attempt: int, base: float = 2.0, cap: float = 86400.0) -> float:
# Calculate exponential ceiling
exponential_limit = min(cap, base * (2 ** attempt))
# Full jitter selects uniform random sleep between 0 and ceiling
sleep_seconds = random.uniform(0, exponential_limit)
return sleep_seconds
| Attempt | Exponential Limit | Jittered Backoff Window |
|---|---|---|
| 1 | 4 seconds | 0 – 4 seconds |
| 2 | 8 seconds | 0 – 8 seconds |
| 3 | 16 seconds | 0 – 16 seconds |
| 5 | 64 seconds | 0 – 64 seconds |
| 10 | 34 minutes | 0 – 34 minutes |
5. Dead Letter Queues (DLQ) & Circuit Breaking
After a maximum retry count is reached (typically 5 to 10 attempts over 24 to 72 hours), the message transitions to a Dead Letter Queue (DLQ):
- Endpoint Circuit Breaking: If an endpoint fails 100 consecutive deliveries over a 24-hour period, automatically disable the webhook endpoint and notify the account administrator via email. This prevents zombie endpoints from consuming dispatcher capacity.
- Manual and Automated Redrive: Provide an administrative API and UI allowing customers to inspect failed payload bodies, view exact response status codes/headers, and trigger a batch redrive once their server is repaired.
By enforcing socket-level SSRF mitigations, timestamped HMAC signing, and jittered backoff pipelines, engineering teams transform fragile HTTP dispatches into a carrier-grade webhook delivery platform.