Publishing Events - Introduction to the Outbox Pattern

5 important questions on Publishing Events - Introduction to the Outbox Pattern

What's a a message relay service ?

an additional service that gets the messages from your outbox table and sends them to your message broker. It splits the communication into 2 independent, local transactions

Give 2 main options to create a message relay service ?

  • Option 1: You implement a service that polls the outbox table and sends a new message to your message broker whenever it finds a new record.
  • Option 2: You can use a tool like Debezium.  It monitors the transaction logs of your database and sends a message for each new record in the outbox table to your message broker. This approach is called Change Data Capture (CDC)

How can you add messages to the outbox table ?

  • You can trigger a custom domain event, e.g., via CDI or Spring, and use an event handler to write a record to the outbox table,
  • Or you write the record programmatically using an entity or a native query,
  • Or you use a Hibernate-specific listener to write a record to the outbox table every time you persist, update or remove an entity.
  • Higher grades + faster learning
  • Never study anything twice
  • 100% sure, 100% understanding
Discover Study Smart

Disadvantage of using a Hibernate specific listener ?

you can’t aggregate multiple operations that happened within the same transaction.

How do you write the record in the outbox table ?


You need to implement a method that transforms your aggregate into its JSON representation and inserts it using a simple SQL INSERT statement there is no need to map the outbox table to an entity class
SQL INSERT statements are executed within the same transaction

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo