Concept lesson

Webhook Delivery & HMAC Signatures

Asynchronous webhook delivery, exponential backoff retries, and HMAC SHA-256 signature verification.

lesson
Freshness: current15 min read
Mastery
not started · 0%

Learning outcomes

  • Implement secure webhook delivery pipelines with HMAC signatures
  • Manage delivery retries with exponential backoff and jitter

Mental model

Webhooks reverse the traditional HTTP request model by having the server push real-time event notifications to a client-registered HTTP URL. Reliable delivery requires HMAC signature verification to prevent spoofing and exponential retries to handle target receiver downtime.

Event Triggers Webhook Dispatch
Compute HMAC-SHA256 Signature Header
POST HTTP Payload to Client Endpoint
Client verifies Signature with Shared Secret
Retry with Exponential Backoff on Failure
Conceptual teaching model synthesized from:FastAPI Framework Architecture & Dependency Injection Specification

Theory

  • HMAC Signatures: The sender signs the payload text using a secret key (X-Signature: t=17000000,v1=sha256_hash). The receiver recomputes the HMAC hash to verify authenticity.
  • Exponential Backoff & Jitter: Failed deliveries retry after increasing delay intervals (2^attempt * base_delay + random_jitter) to avoid thundering herd spikes when client servers recover.
import hmac
import hashlib
import time
import requests

def send_webhook(endpoint_url: str, payload_bytes: bytes, secret_key: str):
    timestamp = str(int(time.time()))
    # Format: t=timestamp.payload_bytes
    signature_payload = f"{timestamp}.".encode('utf-8') + payload_bytes
    signature = hmac.new(
        secret_key.encode('utf-8'),
        signature_payload,
        hashlib.sha256
    ).hexdigest()

    headers = {
        "Content-Type": "application/json",
        "X-Webhook-Signature": f"t={timestamp},v1={signature}",
        "X-Webhook-ID": "evt_100200300",
    }

    # Execute HTTP POST with timeout
    response = requests.post(endpoint_url, data=payload_bytes, headers=headers, timeout=5)
    return response.status_code

Alternatives and trade-offs

  • Synchronous Webhook Dispatch: Simple; blocks server worker threads waiting for third-party client endpoints.
  • Asynchronous Queue Dispatch: High throughput, isolated failures; requires task queues (Celery/Redis) and webhook event status tracking databases.

Failure modes and misconceptions

  1. Replay Attacks: Attackers intercept valid webhook requests and re-send them to the receiver. Remedy: Include timestamp t= in the signature payload and reject webhooks older than 5 minutes.
  2. Infinite Retry Storms: Retrying failed webhooks without exponential backoff and jitter overwhelms recovering client servers.
Reflect before revealing the guide

Decision scenario

Sign all outgoing webhook HTTP POST requests with HMAC-SHA256 headers including timestamps, and process deliveries asynchronously using Celery task queues with backoff retries.

Learning outcomes

  • Compute and verify HMAC SHA-256 webhook signatures.
  • Mitigate webhook replay attacks using timestamp signatures.
  • Implement resilient webhook delivery retries with exponential backoff and jitter.

Trade-offs

Asynchronous webhook dispatch guarantees event delivery reliability, but requires managing retry state databases and secret keys for receivers.

Evidence assessment

Theory and decision mastery

not-started · 0%
theory0%
decision0%
activityNot mapped
projectNot mapped
1. What is the core architectural principle governing webhook delivery signatures?
2. What primary operational trade-off must be managed when configuring webhook delivery signatures?
3. Which failure mode is most commonly observed when webhook delivery signatures is misconfigured?

Decision scenario

You are designing a high-concurrency production system requiring reliable execution of webhook delivery signatures under heavy traffic load.

Which architectural decision ensures maximum resilience, scalability, and system stability?

Primary sources