ADR-002: Persist email work in a PostgreSQL outbox before SMTP submission
Notification owns durable email records in PostgreSQL and marks them sent after the sender succeeds, without claiming atomic or exactly-once provider delivery.
This architecture decision has been marked as superseded.
This ADR is Superseded. Notification APIServicev1.0.0Notification APIInternal background worker that consumes order commands from Kafka, renders MJML email templates, and submits mail over ...SubscribesPlaceOrderCommand, CancelOrderCommand +1APIsAsyncAPIOwnerplatform-engineering-teamDiagramRepositoryOpen docs now operates as a stateless Kafka consumer with Redis idempotency and dead-letter queues rather than a PostgreSQL email outbox. See ADR-001: Consume order notification commands through KafkaDecision Recordv1.0.0ADR-001: Consume order notification commands through KafkaNotification consumes Finance order commands through Wolverine and Kafka, keeping email processing outside the business ...Ownerplatform-engineering-teamOpen docs and ADR-002: Use a durable outbox with bounded provider retriesDecision Recordv1.0.0ADR-002: Use a durable outbox with bounded provider retriesNotification delivery attempts are driven by durable outbox records and a bounded retry policy so provider outages do no...Ownerplatform-engineering-teamOpen docs.
1. Context & Problem
External email providers can fail or time out after an email has already been formatted. If Notification attempted to send emails directly in memory without saving them first, any crash, restart, or network drop would permanently lose the communication with no record left for operations teams to inspect or retry. Furthermore, BookWorm cannot rely on an external email provider as its own system of record for what notifications were requested or pending.
2. Business Value
- Protects pending communications: Saving email details before contacting the external provider ensures work is not lost during service restarts or pre-send crashes.
- Enables inspection and recovery: Unsent records remain visible in Notification’s own storage so scheduled maintenance or operators can inspect backlogs and retry failed sends.
- Preserves ownership: BookWorm retains an explicit local record of pending and submitted work rather than depending on third-party provider logs alone.
3. How It Works
- Save before sending: When a notification is ready, Notification writes the recipient details, subject, and rendered message body to its own database as an unsent record before calling the external mail sender.
- Submit to the external provider: Notification then invokes the mail sender to deliver the message externally.
- Update sent status: Once the sender returns without error, Notification updates the record’s sent status flag and records the submission timestamp.
- Resend behavior: When an existing unsent record is retried through the shared sender flow, the system creates a new record for that attempt before trying delivery, and marks the original record as sent only if the retry succeeds.
4. Trade-offs & Lessons Learned
- Sent status is not a delivery receipt: Marking a record as sent confirms only that the local mail sender completed without error—not that the provider delivered the email to the customer’s inbox or that the underlying business action succeeded.
- No atomic guarantee across database and mail provider: Saving to the database and submitting to an external mail provider are separate steps. If the provider accepts an email but the response is lost or the subsequent database update fails, the record stays marked unsent and can be retried, causing a duplicate delivery.
- Row counts do not equal unique customer emails: Because resends insert new rows through the shared sender wrapper (and a failed resend can leave both old and new rows unsent), backlog counts reflect database rows rather than distinct logical notifications. Monitoring must consider row age and outcomes alongside raw counts.
- Limited failure metadata: The current outbox table does not store attempt counters, next-attempt schedules, permanent vs. transient failure categories, full provider responses, or uniqueness constraints per business message. Operators should check for ambiguous outcomes before manually retrying items.
- Data sensitivity at rest: Because full rendered email content is saved before delivery, database access controls, backup/recovery, and retention policies apply even to pending messages.
- Alternatives rejected:
- Sending without persisting first: Rejected because crashes before or during submission would lose messages completely.
- Updating a single row in place for every retry: Would avoid row growth, but requires changing the shared sender wrapper and designing explicit retry-history storage.
- Treating database persistence and external mail delivery as one atomic transaction: Not possible because external mail providers do not participate in internal database transactions.
5. Technical Reference
- Status & Context:
superseded(2026-11-01). Complements/contrasts with ADR-002: Use a durable outbox with bounded provider retriesDecision Recordv1.0.0ADR-002: Use a durable outbox with bounded provider retriesNotification delivery attempts are driven by durable outbox records and a bounded retry policy so provider outages do no...Ownerplatform-engineering-teamOpen docs, which described bounded lifetime retries, terminal failures, provider-outcome recording, and an exactly-once local transition that are not fully implemented by this persistence model. - Authoritative Store: The Notification PostgreSQL database stores application email outbox records separately from Wolverine’s durable incoming and outgoing message envelopes, even though both reside in the Notification database.
- Outbox & Sender Symbols:
EmailOutboxServicesaves recipient details, subject, and rendered body before callingIEmailSender.- On
IEmailSendersuccess, it setsis_sent, updates the submission timestamp, and saves again. - The email table records identity, sequence ordering, creation time, and sent state (
is_sent). - The resend handler resolves the same
ISenderoutbox wrapper, inserting a new row before submission and marking the original row sent on success (a failed resend can leave both rows unsent).
- Operational Dependencies: PostgreSQL availability, schema migrations, retention controls, and database recovery are direct operational dependencies of the notification delivery workflow.