Third-party webhooks from payment gateways (Stripe), communication providers (Twilio, RingCentral), or healthcare EHR integrations are notoriously unpredictable. Providers will retry aggressively during network blips, send duplicate payloads, or blast thousands of events within seconds during batch syncs.
If your webhook endpoint directly executes database queries or third-party downstream calls, your backend will quickly encounter connection exhaustion, cascading timeouts, and 504 Gateway errors.
Here is the architectural blueprint I use to design high-throughput, idempotent webhook ingestion pipelines.
The Core Rule: Ingestion Must Be Decoupled from Processing
The incoming HTTP handler must do only three things:
- Validate signature / auth token (e.g., HMAC-SHA256).
- Buffer raw payload into a persistent message broker (Redis Streams or RabbitMQ).
- Acknowledge HTTP 202 Accepted immediately (within < 30ms).
[ External Provider ]
│ HTTP POST (Payload + HMAC)
▼
[ FastAPI Ingestion Gateway ] (< 25ms)
│
├── 1. Verify HMAC Signature
├── 2. Push to Redis Stream (`webhooks:incoming`)
└── 3. Return 202 Accepted
│
▼
[ Background Celery / Worker Pool ]
│
├── Idempotency Check (Redis SETNX key)
├── Atomic DB Upsert (PostgreSQL)
└── Event Dispatch
1. FastAPI Fast Ingestion Endpoint
Below is a production-tested FastAPI endpoint that verifies signatures using constant-time comparison and pushes the event into a Redis Stream:
| |
Notice that we don’t even parse JSON in the HTTP path if we don’t strictly need to. The raw bytes are verified and pushed directly into Redis.
2. Ensuring Idempotency: Handling Duplicate Retries
Third-party webhook providers guarantee at-least-once delivery. That means duplicate deliveries are not an exception—they are an inevitable guarantee.
To avoid double-billing or applying the same state change twice:
- Extract the provider’s unique event ID (e.g.,
evt_12345or Twilio’sMessageSid). - Use Redis
SET key value NX EX <seconds>(atomic set-if-not-exists with expiration) as a distributed deduplication lock.
| |
3. Graceful Degradation & Dead Letter Queues (DLQ)
When downstream services or databases fail intermittently, retries with exponential backoff + jitter prevent stampeding thundering herd problems.
Any payload that fails all retry attempts is routed to a Dead Letter Queue (DLQ) along with its stack trace. This ensures:
- No data is silently lost.
- Engineers can replay failed events with a single CLI script after patching bugs.
Summary Checklist
- Respond with
202 Acceptedwithin 50ms. - Push payloads directly to a durable stream or queue.
- Protect against duplicate deliveries with atomic idempotency locks.
- Store raw payloads for at least 7 days to facilitate manual audit and event replay.