Webhooks Explained: Signed Payloads and Retries

Backend2026-11-23TryQuickToolBox

You've just integrated a third-party payment provider, and now you need to receive real-time notifications when a customer completes a purchase. You set up a webhook endpoint, but soon you're facing duplicate events, missed notifications during downtime, and security concerns about forged requests. This is a common scenario: webhooks are simple in concept but tricky to implement reliably.

In this article, we'll walk through the essentials of building robust webhook consumers and producers. You'll learn how to sign and verify payloads to ensure authenticity, how to design retry mechanisms that handle failures, and how to avoid common pitfalls like duplicate processing. By the end, you'll have a clear blueprint for implementing webhooks that work in production.

What Are Webhooks?

Webhooks are user-defined HTTP callbacks. When an event occurs in a source system (like a payment being processed), that system sends an HTTP POST request to a URL you provide, containing details about the event. Unlike polling, where you repeatedly ask for updates, webhooks push data to you as it happens, reducing latency and server load.

Webhooks are widely used for integrations: payment gateways notify e-commerce sites of successful transactions, CI/CD platforms trigger deployments on code pushes, and SaaS applications sync data across services. However, because webhooks rely on public HTTP endpoints, they introduce security and reliability challenges.

Securing Webhooks with Signed Payloads

When you expose a webhook endpoint, anyone can send requests to it. Without verification, attackers could forge events, leading to unauthorized actions or data corruption. Signed payloads solve this by including a cryptographic signature that proves the request originated from the expected sender.

How HMAC Signatures Work

The most common approach uses HMAC (Hash-based Message Authentication Code) with a shared secret. The sender computes a hash of the request body using the secret and includes it in a header (e.g., X-Signature). The receiver recomputes the hash and compares it to the header value. If they match, the payload is authentic.

Here's a simplified example in Python using the hmac module:

import hmac
import hashlib
import json

def verify_signature(payload, secret, received_signature):
    expected = hmac.new(
        secret.encode(),
        payload,
        hashlib.sha256
    ).hexdigest()
    return hmac.compare_digest(expected, received_signature)

# In your webhook handler:
# payload = request.body
# signature = request.headers.get('X-Signature')
# if not verify_signature(payload, 'your_secret', signature):
#     return 401

Always use a constant-time comparison function (like hmac.compare_digest) to prevent timing attacks. Also, ensure you're hashing the raw request body, not a parsed version, to avoid discrepancies.

Best Practices for Signed Payloads

Designing Reliable Retries

Webhook delivery is not guaranteed. Networks fail, servers go down, and endpoints may return errors. A robust webhook system must retry failed deliveries, but naive retries can cause duplicate processing or overwhelm the receiver.

Retry Strategies

Common retry strategies include:

Most systems combine exponential backoff with a maximum retry count (e.g., 5 attempts) and a dead-letter queue for persistent failures.

Idempotency: Handling Duplicates

Retries can cause the same event to be delivered multiple times. To handle this, design your webhook consumer to be idempotent. Include a unique event ID in the payload and store processed IDs. Before processing, check if the ID has already been handled; if so, return a success response without reprocessing.

Here's a simple idempotency check using a database:

def process_event(event_id, payload):
    if db.exists(f'processed:{event_id}'):
        return 200  # Already processed
    # Process the event
    db.set(f'processed:{event_id}', '1', ex=86400)  # Expire after 24h
    return 200

Use a TTL to avoid unbounded growth of processed IDs.

Implementing Webhook Delivery: A Step-by-Step Guide

If you're building a webhook producer, follow these steps to ensure reliable delivery:

  1. Generate a unique event ID for each event (e.g., UUID).
  2. Sign the payload using HMAC with a shared secret. Include the signature and timestamp in headers.
  3. Send the HTTP POST request to the subscriber's URL with a reasonable timeout (e.g., 10 seconds).
  4. Log the delivery attempt and response status.
  5. On failure (non-2xx response or network error), schedule a retry using exponential backoff with jitter. Store the event in a queue for later processing.
  6. After maximum retries, move the event to a dead-letter queue for manual inspection.

For consumers, the key steps are:

  1. Verify the signature using the shared secret.
  2. Check the timestamp to prevent replay attacks.
  3. Ensure idempotency by tracking event IDs.
  4. Process the event quickly and return a 2xx response. If processing takes time, acknowledge receipt and process asynchronously.

Common Pitfalls and How to Avoid Them

FAQ

How do I verify a signed webhook payload?

Recompute the HMAC signature using the shared secret and compare it to the received signature in constant time. Always hash the raw request body, not a parsed version.

What's the best retry strategy for webhooks?

Use exponential backoff with jitter, capped at a maximum number of retries (e.g., 5). This balances timely delivery with avoiding overwhelming the receiver.

How can I prevent duplicate processing of webhook events?

Include a unique event ID in the payload and store processed IDs with a TTL. Before processing, check if the ID exists; if so, skip processing and return success.

Ready to test your webhook payloads? Use our JSON formatter to pretty-print and validate JSON payloads quickly.