Video in progress

When do Spring's in-process events stop being enough, and when do you move to Kafka or an outbox?

In-process events live inside one JVM: if the service dies they are lost, other services don't see them, and there is no backpressure. When losing an event is costly, or when another team's service needs to listen, I move to a broker and an outbox.

📖 Answer

ApplicationEventPublisher is great for cutting dependencies inside a module: the orders package doesn't know about the notifications package and just publishes an event. But its guarantees are very weak.

The questions I ask myself:

  1. What happens if the event is lost? Cache eviction or statistics, fine. A payment confirmation or bonus calculation, no. @TransactionalEventListener(AFTER_COMMIT) doesn't guarantee anything either: if the pod dies between the commit and the listener, the event vanishes without a trace.
  2. Who listens? If a separately deployed service needs to listen, an event inside the JVM won't reach it.
  3. Is the listener slow? A synchronous listener slows down the request. If you add @Async, the queue sits in the executor's memory and can fill up.

There is an intermediate step on the way. Spring Modulith has an event publication registry: the event is written to the database, marked complete when the listener finishes successfully, and incomplete ones can be resubmitted, either on restart (spring.modulith.events.republish-outstanding-events-on-restart=true, off by default) or by you through IncompleteEventPublications. Inside a monolith this is enough without Kafka in many cases.

If another service needs it, use a transactional outbox: the business data and an outbox row are written in one transaction, then a relay (Debezium or polling) forwards it to Kafka. This gives you at-least-once delivery, so the consumer must be idempotent.

Account for the cost of adding a broker too: one more piece of infrastructure, monitoring, schema management, and explaining eventual consistency to the user.