Skip to main content

ADR-0001: Transactional outbox for side effects

Status​

Accepted. Reconstructed from the implementation and from AGENTS.md; the original discussion was not in the repository. Revised in October 2026: consumers are now invoked directly per consumer instead of through EventEmitter2.

Context​

Order creation must record fulfillment work and a wallet debit, stock changes must reach channels, and courier status changes must move fulfillment, NDR and returns. Those effects cannot run only as in-process events on the API. The API process does not load the dispatcher, and an in-process emit is lost if the process stops.

The first version dispatched each claimed row through EventEmitter2.emitAsync. @nestjs/event-emitter swallows listener errors by default (suppressErrors: true), so a failing listener never failed the emit: the row was marked PROCESSED, nothing retried, and nothing reached Sentry. A shipment listener failed on every imported order this way, unnoticed.

Decision​

Writers call OutboxWriterService.enqueue with the Prisma transaction client, so the event commits or rolls back with the business row.

Consumers declare themselves with @OutboxHandler(eventType, consumerName, { lockOrder? }). At startup OutboxHandlerRegistry discovers every decorated method in the worker. For each claimed row, OutboxDispatcherService calls every consumer of that event type separately, inside tenant context:

  • each call runs in IdempotentEventHandlerService, which inserts a ProcessedEvent row for (event, consumerName) in a transaction and passes that transaction to the handler. A consumer that already succeeded is skipped on retry;
  • a consumer declared transactional: false (one that calls an external system or runs its own queries) is called with no transaction held open, and its ProcessedEvent row is written after it returns;
  • lockOrder locks the order the payload names for the length of that transaction;
  • if any consumer throws, the row stays PENDING with attempts + 1 and a lastError naming the consumer, and is retried with backoff. After 10 attempts it is DEAD;
  • a failure is reported to Sentry through reportError on the first attempt and again, as fatal, when the row goes DEAD, tagged with the event type, consumer, attempt and tenant.

Best-effort in-process events that do not need delivery guarantees use @OnLocalEvent, which reports a failure and does not retry. A bare @OnEvent is rejected by yarn architecture:check. yarn di:check fails if a class declaring @OutboxHandler is not a provider of the worker.

Bull remains a second mechanism for work that is queued directly (sync, labels, email, inventory flush, webhooks).

Alternatives​

  • Keep EventEmitter2 with suppressErrors: false. The emit would reject, but one failing listener would rerun every listener of the event, and there would still be no per-consumer record of what succeeded.
  • One Bull queue per consumer. Durable and per-consumer, but enqueueing to Redis cannot join the Postgres transaction, which is the reason for the outbox.

Consequences​

  • A throw before commit rolls back the business row and the outbox row together.
  • Delivery is per consumer and at least once. A transactional consumer is effectively exactly once: its writes and its ProcessedEvent row commit together. A transactional: false consumer can repeat its effect if recording fails after it succeeded, so it must be idempotent on its own (the wallet debit has a per-order key; a receivable recompute converges).
  • A transactional consumer must not do work on other connections while holding its transaction. The first version of this design did, for receivable recognition: under an import burst every pool connection was parked in an idempotency transaction while the recompute queued for another, and every transaction hit its 5-second timeout. transactional: false exists for these consumers, and the decorator rejects a consumer whose parameters do not match its mode.
  • consumerName is permanent. Renaming it makes the consumer process the event history again.
  • Side effects of a courier status change run after commit, on the worker, rather than inside the courier update.
  • There is no replay tool for DEAD rows yet.