r/softwarearchitecture • • Oct 20 '26

Discussion/Advice Webhook Reliability & Idempotency Trade-offs: Architecture Review of Relay

Hello architects and backend engineers,

I recently designed and deployed **Relay**, an open-source system focused on reliable asynchronous event delivery, idempotency guarantees, and operational observability.

### System Pipeline:
Webhook Provider -> NestJS API (Signature Validation) -> PostgreSQL (Persistence) -> Redis / BullMQ (Async Queue) -> Delivery Worker -> Downstream Receiver

### Key Design Principles:
- **At-Least-Once Semantics:** Enforcing safety via raw-body HMAC-SHA256 signature validation and database idempotency constraints.
- **Fault-Tolerant Retries:** Exponential backoff policies mapped to a Dead-Letter Queue (DLQ).
- **Observability & Operational Controls:** REST APIs and a standalone dashboard to monitor, inspect, and manually retry dead-lettered events.

I am looking for a peer review on failure modes—specifically around handling partial failures between relational storage and message queues under high concurrency.

Read the technical README, architecture diagrams, and run the reproducible failure suite:
https://github.com/Dieu-Merci-Ndenga/relay

I look forward to discussing your design choices and trade-offs.
6 Upvotes

14 comments sorted by

5

u/Icy-Blueberry7974 Oct 20 '26

just a tip - the next time u paste use ctrl/cmd + shift + v

0

u/Appropriate-Seat-787 Oct 20 '26

Thanks for the tip! Appreciate it, will definitely use Ctrl + Shift + V next time to keep the formatting clean.

2

u/[deleted] Oct 20 '26

[removed] — view removed comment

1

u/Appropriate-Seat-787 Oct 20 '26

100% Game changer for real. Thanks for jumping in

1

u/KraaZ__ Oct 21 '26 • Bravo

Take a look at hook0.

1

u/Appropriate-Seat-787 Oct 22 '26

Thanks for the pointer :) I'll definitely check out Hook0's architecture and design choices—always great to see how established open-source tools handle webhook lifecycle edge cases and API conventions. Appreciate the recommendation :)

1

u/MaintenanceAble5900 Oct 23 '26 • Bravo

wrote a retry system a while back, so from my own experience:

  1. retry window. backoffDelayMs is base * 2^(n-1) with 5 attempts, so 1+2+4+8 is about 15 seconds and the event is in the DLQ. any real outage lasts longer. make it configurable per destination

  2. circuit breaker, so you don’t finish off a receiver that’s already struggling

  3. jitter, same reason. right now everything that failed at the same moment retries at the same moment

  4. no expiry. if the receiver was down for hours, some events aren’t worth delivering anymore. i work in telecom, an OTP sent hours late is useless. TTL per event type, and when it expires i’d send it to the DLQ rather than drop it. from experience, that way you can look at it later if something goes wrong and reproduce the problem. ideally together with why it didn’t get through: the receiver was down, the contract changed and nobody noticed, the receiver is too slow, a firewall on the way started dropping everything

  5. tests: the unit tests mock the store and queue, the e2e runs in-memory. i’d add a couple of component tests on real postgres and redis plus chaos tests: kill redis between insert and enqueue, take the receiver down, slow it down

i also pointed an AI agent at the code, and it flagged a few more. haven’t verified these myself, so worth double-checking:

  • the failure reason is saved as error.message, and for network errors node’s fetch only says “fetch failed” there. the real reason (ECONNREFUSED, ECONNRESET, ENOTFOUND) is in error.cause, which isn’t saved, so “receiver down” and “firewall dropping” look the same. response bodies aren’t stored either, so a contract change shows up as a bare 400

  • the signature only covers the raw body. x-relay-event-id and :source aren’t signed and there’s one secret for all sources, so anyone who gets hold of one signed request can replay it with a new event id. it passes saveIfNew as a new event and gets delivered again. the dedup key should be inside what’s signed, plus a timestamp, and ideally a secret per source

  • queue-side dedup relies on BullMQ job ids, but the first job completes even when it only scheduled a retry, and removeOnComplete keeps 1000. under load that id gets trimmed, and a provider retry for an event that’s mid-retry or in the DLQ starts a second delivery chain

1

u/Appropriate-Seat-787 Oct 24 '26

This is absolute gold - thank you for taking the time to share your real-world experience (especially the telecom context) :)

Quick update on how this aligns with Relay's latest iteration:

  1. Already Fixed (in the latest Update Post):

  • BullMQ Job Trimming & Retry Chain: Spot on ! We actually identified and fixed this exact edge case in Issue #5. We introduced a Transactional Outbox Pattern + Sweeper, and now check PostgreSQL delivery state before re-enqueueing duplicate requests so trimmed BullMQ job IDs no longer trigger double delivery chains.

  1. Added to the Roadmap:

  • Jitter & Longer Retry Windows: Fully agree. 15 seconds is way too aggressive for real-world outages. Adding random jitter and configurable backoff schedules per destination is a must-have.

  • Richer Error Diagnostics (error.cause): Great catch on Node's fetch behavior! Storing error.cause (e.g., ECONNREFUSED vs ENOTFOUND) in the attempt history will make debugging receiver failures infinitely easier.

  • Event TTL & Expiration to DLQ: Love the OTP example. Dropping expired events straight to DLQ with an explicit "EXPIRED" status keeps auditability intact without executing useless side-effects.

  • Real Postgres/Redis Chaos Tests: Expanding test coverage with testcontainers to test network partitions against real instances.

  1. Signature & Provider Constraints:

  • Regarding inbound signatures: Since Relay acts as a consumer receiving webhooks from standard providers (Stripe, GitHub, etc.), we adhere to their standard body-signing specs. However, scoping secrets per source and validating timestamp headers to mitigate replay windows is definitely the right direction for multi-source setups.

Thanks again for such a thoughtful review and for running it through your analyzer, this gives me a crystal-clear checklist for the next iteration ! :)

1

u/bezcurhq Oct 20 '26 • Bravo

The partial failure you're asking about is in acceptEvent. If the insert commits and then queue.add fails (Redis blip, pod killed), the provider sees a 5xx and retries, but saveIfNew now returns false, so you answer already_received and never enqueue. That event is lost without a trace. Simplest fix is to stop treating "row exists" as "queued" and enqueue on duplicates too, since jobId = eventId means BullMQ ignores the add if the job's still there. Worst case is a duplicate delivery, which at-least-once already allows. Or sweep rows with no attempts after N seconds, which is basically an outbox. Unrelated, but the BullMQ docs say custom job ids mustn't contain ":", and your manual-retry id does.

1

u/Appropriate-Seat-787 Oct 20 '26

This is an incredible review, thank you for taking the time to look directly at acceptEvent and the BullMQ mechanics!

  1. Partial Failure & Event Loss :  You hit the nail on the head regarding the dual-write edge case between PostgreSQL insert and queue.add. Your suggestion to re-attempt queue.add even on duplicates (relying on BullMQ's native jobId = eventId deduplication) is an elegant, immediate fix for at-least-once semantics without introducing a full Outbox background worker right away.

  2. BullMQ Custom Job ID Format : Excellent catch on the : character restriction in BullMQ custom job IDs for manual retries! I'll refactor the retry job ID formatting to ensure strict compliance with BullMQ specs.

I'm opening an issue/PR to address both points in the codebase. Really appreciate this level of feedback !

1

u/bezcurhq Oct 21 '26 • Bravo

Glad it helped. One thing to watch when you make that change: you have removeOnComplete: 1000, and BullMQ only ignores a duplicate jobId while the old job still exists. Once a delivered event's job has been trimmed, a late provider retry would get enqueued and delivered again. Checking the row's delivery state first and only re-adding when it isn't delivered yet closes that gap.

1

u/Appropriate-Seat-787 Oct 21 '26

That is another spot-on observation :)

Checking the event’s delivery status in PostgreSQL before re-enqueueing—so we only call queue.add if the event is NOT already marked as successfully delivered—completely closes that trimming gap with removeOnComplete.

I've updated the issue description to account for this state check during duplicate handling.

Thanks again for the incredible depth of this review !