Quick revision
The short answer
The event from @TransactionalEventListener lives only in memory, so when the email provider was down the send failed and nothing ever retried it; a restart would lose it too. The fix is to persist a notification row, an outbox record, in the same transaction as the order, and let a worker send pending notifications with retries and exponential backoff, marking each one sent. An idempotency key or deduplication keeps retries from sending duplicates where the provider supports it, and an alert on the backlog of pending notifications tells us when delivery falls behind.
What to remember
In this answer, we saw that @TransactionalEventListener delivers an in-memory event after commit, so a failed send or a restart loses it with nothing to retry. We fixed it by persisting a notification row in the same transaction as the order, sending from a worker with exponential backoff and a maximum number of attempts, using the notification id as an idempotency key to limit duplicates, and alerting on the backlog of pending and failed notifications.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The event from @TransactionalEventListener lives only in memory, so when the email provider was down the send failed and nothing ever retried it; a restart would lose it too. The fix is to persist a notification row, an outbox record, in the same transaction as the order, and let a worker send pending notifications with retries and exponential backoff, marking each one sent. An idempotency key or deduplication keeps retries from sending duplicates where the provider supports it, and an alert on the backlog of pending notifications tells us when delivery falls behind.
Why this question is asked
Sending an email after saving an order looks like a solved problem in Spring: publish an event and handle it with @TransactionalEventListener, which runs after the transaction commits. It works perfectly in tests and most of the time in production. Then the email provider has a ten-minute outage and every customer who ordered in those ten minutes never gets a confirmation. Interviewers ask this debugging question to see whether we understand what in-memory events guarantee and how to make a side effect reliable. In this answer, we will see why the emails were lost, reproduce it, move to a persisted outbox, write the sending worker with retries, avoid duplicates and monitor the backlog.
1A Quick Recap of @TransactionalEventListener
Spring lets a service publish an application event with ApplicationEventPublisher. A method annotated with @TransactionalEventListener receives the event, but not immediately: by default it runs in the AFTER_COMMIT phase, after the surrounding transaction has committed successfully. That is useful, because we do not want to email a customer about an order that was rolled back.
But the event itself is just a Java object in memory, held by Spring until the commit and then passed to the listener on the same thread. Nothing writes it to disk, nothing tracks whether the listener succeeded, and nothing calls the listener again.
2Reproducing the Lost Email
Here is the typical code.
5 messages in order. Play them, or step through them one at a time.
After the commit, no durable record says an email is still owed.
The order is already committed when the listener runs, so the exception cannot roll anything back. Spring logs it, and the event object is discarded. During the ten-minute outage every send failed this way, and since nothing remembers which emails were not sent, nothing retries them. The same loss happens without any outage if the pod is stopped by a deployment between the commit and the send: the in-memory event disappears with the process.
3Persisting the Notification With the Order
To make delivery reliable, the fact that an email is owed must be stored durably, and atomically with the order: either both are saved or neither is. So instead of an in-memory event, we insert a notification row in the same transaction as the order. This is the transactional outbox pattern.
Now, if the order commits, the notification row exists with status PENDING, whatever happens afterwards: a provider outage, a crash or a deployment. If the transaction rolls back, neither exists, so we never email about an order that was not saved.
4A Worker That Sends With Retries and Backoff
A scheduled worker picks up pending notifications whose next attempt time has arrived, sends them, and marks them SENT. On failure it records the attempt and schedules the next one with exponential backoff, for example 1, 2, 4, 8 minutes up to a cap, so a provider outage does not turn into a flood of retries. After a maximum number of attempts, say 10, the notification is marked FAILED and raised for attention.
lockDueBatch uses SELECT ... FOR UPDATE SKIP LOCKED, so several instances of the service can run the worker at once without picking the same rows. The method should run in a transaction that covers the batch, or each notification can be updated in its own short transaction. After the ten-minute outage in the original incident, this worker would simply have sent all the delayed emails within a few minutes of the provider recovering.
5Avoiding Duplicate Emails
Retries introduce a new risk. If the provider accepted the email but the response was lost, or the worker crashed after sending but before marking the row SENT, the next attempt sends it again. Delivery is therefore at least once, and we reduce duplicates in two ways. Where the provider supports an idempotency key or a message id for deduplication, we pass the notification id, so a repeated send of the same notification is ignored by the provider. And we mark the row SENT as soon as the provider confirms, keeping the window between send and mark as short as possible. For order confirmation emails, a rare duplicate is far better than a lost email, but for SMS, which costs money and annoys more, the idempotency key matters more.
6Alerting on the Backlog
Finally, we need to know when delivery is falling behind before customers tell us. The worker publishes metrics: the number of PENDING notifications, the age of the oldest one, the send failure rate and the number marked FAILED. An alert fires when the oldest pending notification is older than, say, 15 minutes, which catches a provider outage, a stuck worker or a bug that stops sending. A second alert on FAILED notifications means nobody silently loses an email again.
7Follow-up questions
Could we keep @TransactionalEventListener and just add a retry around the send?
A retry in the listener helps with a few seconds of failure, but it blocks the request thread (the listener runs on it by default, unless made @Async), it still loses the event if the process stops, and a ten-minute outage outlasts any reasonable in-process retry. The durable row is what makes delivery survive outages and restarts, so the listener approach can be kept only for things we can afford to lose.
Would publishing to Kafka instead of a database table also work?
Publishing to Kafka directly inside the order transaction has the dual-write problem: the commit can succeed and the publish fail, or the other way round. The outbox row solves it, and a relay can then publish outbox rows to Kafka for a separate notification service to consume. For a single service sending its own emails, the database table plus a worker is enough and has fewer moving parts.
8Conclusion
In this answer, we saw that @TransactionalEventListener delivers an in-memory event after commit, so a failed send or a restart loses it with nothing to retry. We fixed it by persisting a notification row in the same transaction as the order, sending from a worker with exponential backoff and a maximum number of attempts, using the notification id as an idempotency key to limit duplicates, and alerting on the backlog of pending and failed notifications.
Open with
The cause is that the event handled by @TransactionalEventListener only exists in memory. The listener runs after the order commits, so when the provider was down the send threw an exception, Spring logged it, and the event was discarded.
Then, if they want more
Nothing records that an email is still owed, so nothing retries it, and the same would happen if the pod restarted between the commit and the send.
To make it reliable, I would store the obligation durably and atomically with the order: in the same transaction that saves the order, insert a notification row with status pending, which is the transactional outbox pattern. If the order commits, the notification exists; if it rolls back, neither does.
Then a scheduled worker picks up pending notifications with SELECT FOR UPDATE SKIP LOCKED so several instances can run it, sends each one, and marks it sent. On failure it records the attempt and schedules the next one with exponential backoff, and after a maximum number of attempts marks it failed for someone to look at.
With this, the ten-minute outage just delays the emails. Because delivery is now at least once, I would pass the notification id as an idempotency key where the provider supports it and mark rows sent immediately after success to keep duplicates rare.
Finally, I would alert on the age of the oldest pending notification and on failed ones, so we notice a backlog before customers do.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot notification service sends emails from a @TransactionalEventListener after...": https://scrivio-interview-library.netlify.app/architecture/backend-system-design/8f755c41e4/