Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is a domain event? How should it be named, and how does Spring Data help an aggregate publish events?

A domain event is an immutable record of something that happened in the domain that other parts of the system care about, such as OrderPlaced or PaymentRefunded. It is named in the past tense using business language, and it carries the data needed to understand it, such as the order id and key values. Spring Data publishes the events returned by a method annotated with @DomainEvents when the aggregate is saved through a repository, and AbstractAggregateRoot provides that method for us; by default the listeners run synchronously, in the same thread and transaction, unless we configure otherwise.

Why this question is asked

Domain events are how a well-designed domain tells the rest of the system what happened without knowing who is listening. They let us keep aggregates small, send emails and update other aggregates without tangling the code, and they are the starting point for integration events and event sourcing. Interviewers ask this question to check the basics: what an event is, how to name it, and how Spring helps an aggregate publish it, including the timing trap with transactions. In this answer, we will define domain events, look at naming and content, publish one from an aggregate with Spring Data, and see when listeners actually run.

1A Quick Recap: What Is a Domain Event?

A domain event records a fact: something happened in the business that matters to someone else. The customer placed an order. A payment was refunded. A shipment was delivered. It is different from a command, which asks for something to happen and may be refused. An event has already happened, so it cannot be refused, only reacted to.

The reason we model events explicitly is decoupling. When an order is placed, many things should follow: send a confirmation email, reserve stock, update loyalty points. If the Order aggregate called all of those directly, it would depend on email, inventory and loyalty code. Instead, it simply announces OrderPlaced, and each interested part reacts in its own way.

2Naming Events and Choosing Their Content

Events are named in the past tense and in the business's language, because they describe facts the business recognises: OrderPlaced, not CreateOrderEvent, and not OrderTableRowInserted. A good test is whether a domain expert would say the name out loud in a meeting. Past tense also makes clear to readers that the event is not a request.

An event carries the data needed to understand what happened without going back to ask: the ids of the aggregates involved and the key values, such as the order id, the customer id, the total and the time it happened. And it is immutable, because a fact does not change after it happened. Java records fit events naturally.

Java
public record OrderPlaced(OrderId orderId, CustomerId customerId, Money total, Instant occurredAt) { }
// Avoid:
// record OrderUpdated(Order order) -- vague name, and it leaks the whole mutable aggregate
// record PlaceOrder(...) -- a command, not an event

Notice that we do not put the whole Order object into the event. A mutable entity can change after the event is created, and it drags the aggregate's internal model into every listener.

3Publishing Events From an Aggregate With Spring Data

Spring Data has built-in support for this. If an aggregate root has a method annotated with @DomainEvents that returns the pending events, the repository publishes each of them through Spring's ApplicationEventPublisher when we call save() on that aggregate. A method annotated with @AfterDomainEventPublication is then called so the aggregate can clear its list. AbstractAggregateRoot is a base class that implements both, and gives us a protected registerEvent() method.

Java
@Entity
public class Order extends AbstractAggregateRoot<Order> {
@Id private UUID id;
private OrderStatus status = OrderStatus.DRAFT;
public Order place(Clock clock) {
if (status != OrderStatus.DRAFT) throw new IllegalStateException("already " + status);
status = OrderStatus.PLACED;
registerEvent(new OrderPlaced(new OrderId(id), customerId, total, clock.instant())); // kept in memory
return this;
}
}
@Transactional
public void placeOrder(UUID id) {
Order order = orders.findById(id).orElseThrow();
order.place(clock);
orders.save(order); // Spring Data publishes OrderPlaced here, then clears the list
}

There is a catch here: events are published only when the aggregate goes through a repository's save method (saveAll and, in recent versions, delete methods too; check your version). If we rely on JPA dirty checking and skip save(), the change is still written at commit, but no event is published. So with this approach we always call save() explicitly.

4When Do the Listeners Run?

By default, a listener annotated with @EventListener runs synchronously: in the same thread, during the save() call, inside the same transaction. That has consequences. If the listener throws, the exception propagates into placeOrder and the whole transaction rolls back, order included. And if the listener sends an email, the email goes out before the transaction has committed, so a later failure could roll back an order the customer was already told about.

Step throughDefault timing of a domain event listener
ServiceRepositoryListenerDatabase
save(order)
publish OrderPlaced (same thread, same tx)
@EventListener runs now
returns
commit
commit may still fail

6 messages in order. Play them, or step through them one at a time.

A synchronous listener runs before commit, inside the caller's transaction.

5Configuring Other Timing

Spring gives us choices for when listeners run. @TransactionalEventListener runs the listener in a chosen phase of the surrounding transaction, by default AFTER_COMMIT, so the email is only sent once the order is safely stored. Adding @Async runs it on another thread so the caller does not wait. Spring Modulith's @ApplicationModuleListener combines these: asynchronous, after commit, in its own new transaction.

Java
@Component
class OrderPlacedListeners {
@EventListener // synchronous, inside the transaction
void reserveLoyaltyPoints(OrderPlaced e) { /* part of the same unit of work */ }
@TransactionalEventListener // phase = AFTER_COMMIT by default
void sendConfirmation(OrderPlaced e) { mailer.send(e.customerId(), e.orderId()); }
}

Notice the trade-off with after-commit listeners: if the application crashes between the commit and the listener, the event is lost, because it only existed in memory. For events that must not be lost, such as those sent to other services, we persist them as part of the transaction, using a transactional outbox or Spring Modulith's event publication registry.

6Follow-up questions

What is the difference between a domain event and an integration event?

A domain event is part of a service's internal model and can change with it. An integration event is a published contract for other teams, kept stable and versioned. Often a service translates selected domain events into integration events at its boundary.

Can an event listener modify another aggregate?

It can, ideally in its own transaction, so that each transaction still changes one aggregate. Doing it synchronously in the same transaction works but couples the two aggregates' consistency and locking.

Should events include the full state?

Usually not. Include ids and the values needed to understand and act on the fact. Listeners that need more can query the owner. Large payloads couple listeners to internal structure and make events hard to evolve.

7Conclusion

In this answer, we saw that a domain event records something that happened in the domain that other parts care about, such as OrderPlaced. Events are named in the past tense in business language, carry the data needed to understand them, such as ids and key values, and are immutable. Spring Data publishes the events returned by a @DomainEvents method when the aggregate is saved through a repository, and AbstractAggregateRoot provides this with registerEvent. Listeners run synchronously in the same transaction unless we use @TransactionalEventListener, @Async or Spring Modulith's listeners.

What to say in the interviewAbout 90 seconds aloud

Open with

A domain event is a record of something that happened in the business that other parts of the system care about, like OrderPlaced or PaymentRefunded. It is a fact, so unlike a command it cannot be refused, only reacted to.

Then, if they want more

That is why I name events in the past tense and in the language the business uses, OrderPlaced rather than CreateOrderEvent or OrderUpdated. They should be immutable, so I use records, and they carry what is needed to understand the fact, the order id, the customer id, the total, the time, but not the whole mutable aggregate.

The point is decoupling: the order just announces that it was placed, and email, inventory and loyalty each react on their own. Spring Data supports this directly. If the aggregate root has a method annotated with @DomainEvents, the repository publishes those events through the application event publisher when I call save, then calls the @AfterDomainEventPublication method to clear them.

AbstractAggregateRoot gives me both plus registerEvent. The catch is I must actually call save; dirty checking alone does not publish. And by default, @EventListener methods run synchronously in the same thread and transaction, so a failing listener rolls back the order and an email might go out before commit.

For side effects I use @TransactionalEventListener, which runs after commit, possibly with @Async, and for events that must never be lost, an outbox or Spring Modulith's publication registry.

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 "What is a domain event? How should it be named, and how does Spring Data help an...": https://scrivio-interview-library.netlify.app/java/domain-driven-design/999556b461/