Skip to content

How do you avoid losing an email event after a booking commits?

1 min read · main part only; folded sections are optional

After this you can answer: How do you avoid losing an email event after a booking commits?

Calling SNS only after committing a booking leaves a gap: the API can crash before publishing. Publishing first is worse because a worker could act on a booking that later rolls back. The outbox makes the intent to publish part of the same durable database decision.

The relay selects up to ten due rows under row locks, skipping rows another relay already holds. It publishes and marks accepted rows sent inside that transaction. Failures store a future due time with exponential backoff capped at sixty seconds. A missing topic configuration pauses delivery; it does not discard the outbox rows.

Holding a database connection during network publishing bounds overlap between relay tasks but extends transaction time. SDK timeouts and a single relay loop per process help limit the cost. For larger scale, I would measure backlog age and database occupancy before considering a different relay or change-data-capture design.

What does a successful SNS response prove?
SNS accepted the publish. It does not prove either queue received the event, a worker completed it or an email reached an inbox.
Can a crash still create a duplicate?
Yes. SNS may accept before the database marks the row sent and commits. Retrying that row is safer than losing it, and requires idempotent consumers.
Why save event details rather than just an ID?
The message is a versioned snapshot of booking facts. The email worker can operate without a Postgres connection and does not accidentally use later edits to the event.

Read Outbox, then inspect the outbox piece →.

apps/api/src/services/outbox.ts → relayOnce

Read this book online: https://book.theversehub.in/