Quick revision
The short answer
Spring calls @EventListener methods synchronously inside publishEvent, so the listener runs within the publisher's transaction, before it commits; if the transaction later rolls back, the email has already gone. The fix is @TransactionalEventListener with phase AFTER_COMMIT, which runs the listener only after a successful commit. Even then the email can be lost if the process dies right after the commit, so for guaranteed delivery we write the event to an outbox table in the same transaction, and we make the email sending idempotent so that retries never send duplicates.
What to remember
In this answer, we saw that @EventListener methods run synchronously inside publishEvent, inside the publisher's still-open transaction, so an email can be sent before a rollback. @TransactionalEventListener with phase AFTER_COMMIT runs the listener only after a successful commit, but the event can still be lost on failure or a crash. An outbox row written in the same transaction makes delivery reliable, and an idempotent sender with a stable key makes the inevitable retries harmless.
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.
Spring calls @EventListener methods synchronously inside publishEvent, so the listener runs within the publisher's transaction, before it commits; if the transaction later rolls back, the email has already gone. The fix is @TransactionalEventListener with phase AFTER_COMMIT, which runs the listener only after a successful commit. Even then the email can be lost if the process dies right after the commit, so for guaranteed delivery we write the event to an outbox table in the same transaction, and we make the email sending idempotent so that retries never send duplicates.
Why this question is asked
This bug is a favourite because it looks impossible at first: how can the customer get a confirmation for an order that does not exist? The answer lies in when Spring runs event listeners relative to the transaction, a detail many developers never think about until a customer calls support. Interviewers ask it to see whether we understand the timing of transactions and events, and whether we know that fixing the timing is not the same as guaranteeing delivery. In this answer, we will recap how @EventListener is invoked, reproduce the bug, fix it with @TransactionalEventListener, look at what can still go wrong after commit, and add an outbox and idempotency for the cases where the email must never be lost or duplicated.
1How @EventListener Is Invoked
When a bean calls applicationEventPublisher.publishEvent(event), Spring's event multicaster looks up every listener for that event type and, by default, calls each one immediately, in the same thread, before publishEvent returns. There is no queue and no separate thread unless we configure one. That means the listener runs in the middle of whatever the publisher was doing.
If the publisher is a @Transactional method, the transaction is still open at that moment. The order row has been inserted but not committed, and other steps of the method may still be ahead. A transaction groups changes so that either all are committed or all are rolled back, and the rollback can still happen.
2Reproducing the Problem
Here is the service. It saves the order, publishes the event, and then charges the payment, which can fail.
7 messages in order. Play them, or step through them one at a time.
Notice the order of events. The email is sent at the second line of placeOrder, while the payment at the third line fails, and the exception makes Spring roll back the whole transaction, including the order insert. The database is consistent, but the email cannot be taken back. The same happens if the commit itself fails, for example on a constraint violation detected at commit.
3Fix 1: Run the Listener After Commit
Spring offers @TransactionalEventListener for exactly this. When the event is published inside a transaction, Spring does not call the listener immediately. It registers a transaction synchronization and calls the listener at a chosen phase of the transaction. The default phase is AFTER_COMMIT, so the listener runs only if the commit succeeded. The other phases are BEFORE_COMMIT, AFTER_ROLLBACK and AFTER_COMPLETION.
There are two details to know. If the event is published when no transaction is active, a @TransactionalEventListener is not called at all, unless we set fallbackExecution = true. And at AFTER_COMMIT the transaction is already finished, so if the listener writes to the database through a @Transactional method, that method needs propagation REQUIRES_NEW to get a fresh transaction; otherwise its changes may not be committed. Check your Spring version's documentation, because newer versions validate some of these combinations at startup.
4What Can Still Go Wrong After Commit
AFTER_COMMIT fixes the timing, but it does not guarantee delivery. The listener runs in memory after the commit. If the mail server is down, the send fails and the order is committed without an email; the exception is logged but does not undo anything. If the process crashes or is redeployed in the few milliseconds between the commit and the send, the event simply disappears, because it only ever existed in memory. For a marketing message that may be acceptable. For an order confirmation that a customer needs, or a message to a downstream system, it usually is not.
5Fix 2: An Outbox for Guaranteed Delivery
The transactional outbox pattern makes the event durable. Instead of sending the email inside the request, we insert a row describing it into an outbox table in the same transaction as the order. Either both the order and the outbox row are committed, or neither is. A separate worker then reads pending outbox rows, sends the emails, and marks the rows as sent, retrying on failure.
- Begin transactionIn
placeOrder - Insert order and outbox rowSame transaction
- Did payment and commit succeed?Exception means rollbackYesBoth rows committedNoNeither row exists, no email ever
- Worker reads pending rowsFOR UPDATE SKIP LOCKED
- Send email with idempotency keyProvider or our table drops repeats
- Mark row sentCrash before this means one retry
6 steps in order. Play them, or step through them one at a time.
Notice that a worker can crash after sending the email but before marking the row as sent. On restart it will send again. The outbox gives at-least-once delivery, never exactly-once, which is why the next step matters.
6Fix 3: Make Sending Idempotent
An operation is idempotent when doing it twice has the same effect as doing it once. We give every message a stable key, such as confirm-42, and record sent keys in a table with a unique constraint, or pass the key to an email provider that deduplicates on it. Before sending, the worker inserts the key; if the insert fails because the key exists, the email was already sent and we skip it. Combined with the outbox, this gives the behaviour the business wants: an email for every committed order, never for a rolled-back one, and never twice.
7Follow-up questions
Would @Async on the @EventListener fix it?
No. @Async moves the listener to another thread, but the event is still dispatched at publish time, before the commit, so the email may go out before the rollback, or even before the order is visible to other transactions. Combining @Async with @TransactionalEventListener runs it after commit on another thread, but it still has no durability.
Why not just send the email at the end of the @Transactional method?
Because the commit happens after the method returns, in the proxy. Code at the last line of the method still runs before the commit, so a commit failure would still leave a sent email. Only an after-commit hook or an outbox puts the send after the commit.
Is Spring Modulith relevant here?
Spring Modulith offers an event publication registry that stores events published to transactional listeners in a table and republishes those not completed, which is an outbox built into the framework. It is worth considering if the project already uses it; the same idempotency rule still applies.
8Conclusion
In this answer, we saw that @EventListener methods run synchronously inside publishEvent, inside the publisher's still-open transaction, so an email can be sent before a rollback. @TransactionalEventListener with phase AFTER_COMMIT runs the listener only after a successful commit, but the event can still be lost on failure or a crash. An outbox row written in the same transaction makes delivery reliable, and an idempotent sender with a stable key makes the inevitable retries harmless.
Open with
The cause is timing. A plain EventListener is called synchronously inside publishEvent, in the same thread, so it runs inside the publisher's transaction before that transaction commits. In this service we save the order, publish the event, the mailer sends the email immediately, and then the payment fails, the exception rolls back the transaction, and the order disappears.
Then, if they want more
The email cannot be rolled back. The first fix is TransactionalEventListener with phase AFTER_COMMIT, which is actually the default. Spring then holds the event until the transaction completes and only calls the listener if the commit succeeded, so a rollback means no email.
I would note two details: it does not fire without an active transaction unless fallbackExecution is set, and if the listener writes to the database it needs REQUIRES_NEW. But after-commit is still in memory. If the mail server is down or the process dies right after the commit, the email is lost.
If the confirmation must always be sent, I would use a transactional outbox: insert a row describing the email in the same transaction as the order, so they commit or roll back together, and have a worker send pending rows with retries.
That gives at-least-once delivery, so a crash after sending but before marking the row sent means a second attempt. To make that harmless, I give each message a stable idempotency key, like confirm plus the order id, and the sender skips keys it has already sent.
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 "An @EventListener for OrderPlacedEvent sends a confirmation email. Sometimes customers...": https://scrivio-interview-library.netlify.app/java/behavioral-patterns/d8b326a455/