← All work

Relay

A webhook delivery service that guarantees at-least-once delivery with exponential backoff, signing, and a replay log.

Year
2024
Role
Backend, operations
Status
Shipped
Stack
  • TypeScript
  • Node
  • Redis
  • Postgres

Relay delivers outbound webhooks on behalf of an API platform. It exists because “just POST the URL” fails in all the ways a queue does: endpoints are down, slow, or return 500 for a week.

Delivery model

The service is an at-least-once system with explicit trade-offs:

  • Each event is persisted before the API acknowledges it.
  • Delivery attempts are driven by a Redis-backed scheduler with exponential backoff and jitter.
  • After a configurable number of failures, the endpoint is marked unhealthy and deliveries pause.
  • Every attempt, response, and timing is written to a log that customers can inspect and replay.

Consumers are expected to deduplicate on the event ID. The documentation says so in the first paragraph, and the payload includes a stable X-Relay-Event header.

Signing

Payloads are signed with HMAC-SHA256 over a timestamp and the raw body. Signing the raw bytes matters — reserializing JSON breaks signatures in ways that are painful to debug.

const signature = sign({
  secret,
  timestamp,
  body: rawBody, // the exact string that will be sent
});

Operational lessons

  • Timeouts are not failures. A 30-second timeout can hide a successful delivery. Retries must therefore be safe on the consumer side.
  • Slow endpoints are worse than dead ones. A dead endpoint fails fast and moves on; a slow one consumes workers. Per-endpoint concurrency limits fixed this.
  • Replay is a feature, not a repair tool. Customers use it after fixing their own outages, and it needs to be a first-class, audited operation.

Result

Relay handles a few hundred thousand deliveries a day with a p99 dispatch latency under 300ms and no customer-visible message loss in two years of operation.