In distributed modern web architectures, polling an external API every thirty seconds to check if a payment succeeded or an order dispatched is terribly inefficient. Today, everything runs on Webhooks (HTTP callbacks): when an event occurs in Razorpay, Stripe, Shopify, or WhatsApp, their server dispatches an HTTP POST payload directly to your application endpoint within milliseconds.
Yet, building a naive webhook receiver—one that processes financial transactions synchronously inside the HTTP request loop—is one of the most dangerous vulnerabilities in web engineering. If your database takes 5 seconds to respond, the third-party gateway times out and resends the webhook, causing duplicate orders, double payment captures, and corrupted inventory balances.
At CodeAro Technologies, we architect resilient event-driven systems that process millions of webhooks with zero dropped events and strict data integrity. Here is our engineering guide to production webhook architecture.
1. The Three Golden Rules of Webhook Processing
Whenever an incoming webhook hits your server, your handler must adhere to three foundational rules:
Rule 1: Verify the Cryptographic HMAC Signature First
Anyone on the public internet can send an HTTP POST request to https://yourdomain.com/webhooks/payment-success with a fake JSON payload claiming a ₹50,000 transaction succeeded. You must never trust the payload without validating its cryptographic signature:
- Gateways include a signature header (e.g.,
X-Razorpay-SignatureorStripe-Signature). - Compute an HMAC SHA-256 hash using the raw, unparsed incoming request body and your secret webhook signing key.
- Perform a timing-safe string comparison (
hash_equals()in PHP orcrypto.timingSafeEqual()in Node.js) to prevent timing attacks. If the signature doesn't match, reject the request with HTTP 400 immediately.
Rule 2: Respond with HTTP 200 Fast (Under 500ms)
Payment gateways enforce strict response timeouts (typically 5 to 10 seconds). Never execute heavy operations (sending emails, updating external CRMs, generating PDF invoices) synchronously inside the webhook endpoint. Acknowledge receipt immediately with an HTTP 200 response and push the payload to an asynchronous background job queue (Redis Queue, Celery, or RabbitMQ).
Rule 3: Enforce Idempotency
Networks fail, and webhook providers guarantee at-least-once delivery, not exactly-once delivery. If a gateway sends a webhook and a brief network glitch drops your 200 OK acknowledgment, the gateway will resend the exact same webhook 30 seconds later.
2. Implementing Idempotency Keys
An idempotent operation produces the exact same outcome regardless of whether it executes once or ten times. Here is how CodeAro enforces idempotency at the database level:
- Extract the provider’s unique event identifier (e.g.,
event_id: "evt_1N4m9x..."). - Insert a record into an
idempotency_keystable with aUNIQUEconstraint on(provider, event_id). - Wrap the execution in a database transaction:
- If the
INSERTsucceeds: Process the event and update status tocompleted. - If the
INSERTthrows a unique constraint collision error: A previous worker is already processing or has completed this event. Safely acknowledge with HTTP 200 and terminate execution immediately without duplicate side-effects.
- If the
3. Outbound Webhooks: Exponential Backoff & Dead-Letter Queues
| Architecture Component | Operational Role | Failure Prevention |
|---|---|---|
| Exponential Backoff Retries | When your app dispatches webhooks to client servers that return 5xx errors, retry with increasing intervals: $10s ightarrow 1m ightarrow 5m ightarrow 30m ightarrow 2h$. | Prevents a down client server from being crushed by an aggressive retry storm. |
| Dead-Letter Queue (DLQ) | If a webhook fails repeatedly after 5 attempts, move the message to a DLQ for manual inspection. | Prevents failed webhooks from blocking healthy items in the main queue. |
| Circuit Breaker Pattern | If an external endpoint fails 50 consecutive times, temporarily suspend webhooks to that destination for 1 hour. | Preserves your own server's outbound HTTP connection pool and worker resources. |
Building Mission-Critical API Integrations?
Protect your financial transactions and customer records. CodeAro designs bulletproof, event-driven webhook architectures that never drop a transaction.
Consult Our Integration Specialists