A developer integrating a Stripe-like flow discovered that returning a 200 OK in 12ms masked a critical failure. The issue wasn't network latency or client timeout, but a logic error in the handler's execution order. While the webhook receiver logged the event and reported success, the downstream job never processed half of the payloads.

The Ordering Bug That Broke Idempotency

The root cause was a race condition in the handler's sequence of operations. The code stored the event, returned a 200 OK immediately, and only then attempted to enqueue the downstream job. When the enqueue operation failed due to a transient queue blip, the sender had already received a success signal. Because the sender saw a 200 OK, it assumed the transaction was complete and never triggered its retry policy. The response code acted as a false promise, indicating that the work was done when the critical side effectβ€”job enqueuingβ€”had actually failed. This highlights a common misconception: the HTTP status code reflects the server's state, not just the receipt of data.

Fixing the Contract: Enqueue First, Respond Later

The solution requires reordering the handler to ensure the side effect the sender cares about completes before the response is sent. By moving the enqueue operation to occur before the return statement, the server can catch failures and return a 5xx error, prompting the sender to retry. Additionally, the receiver must be made idempotent to handle these retries safely. The developer implemented keying on the sender's idempotency key and switched to upsert operations instead of simple inserts. This ensures that if the sender retries after a perceived failure, the receiver doesn't duplicate the event or corrupt the state.

Testing with Ephemeral Receivers

To catch these subtle ordering bugs, the developer utilized a single-use HTTPS receiver that holds up to 100 events and auto-expires. This tool, available at x402.freeq.one, allowed for the replay of exact captured payloads against a fixed handler without standing up heavy infrastructure. The ability to replay real-world event sequences made the ordering bug visible. Without this specific testing methodology, the intermittent nature of the queue blip might have remained hidden in production logs for much longer, leading to silent data loss.

Key Takeaways

  • Treat HTTP status codes as promises about state, not just code execution paths.
  • Always enqueue jobs or complete critical side effects before returning a 200 OK.
  • Implement idempotency keys and upsert logic to make retry-safe handlers.
  • Use ephemeral webhook receivers to replay real payloads and test ordering logic.

The Bottom Line

Retry logic is a two-party contract. If your server returns success before the work is committed, you are breaking the client's trust and silently dropping data.