Outbox Pattern With Hibernate PDF
Outbox Pattern With Hibernate PDF
When you apply the Outbox pattern, you split the communication
between your microservice and the message broker into multiple
parts by providing an outbox table.
In the next step, you need an additional service that gets the
messages from your outbox table and sends them to your message
broker. This message relay service is the topic of another tutorial and
I only want to mention your 2 main implementation options here:
1. You can use a tool like Debezium to monitor the logs of your
database and let it send a message for each new record in the
outbox table to your message broker. This approach is called
Change Data Capture (CDC).
2. You can implement a service that polls the outbox table and
sends a new message to your message broker whenever it finds
a new record.
I prefer option 1, but both of them are a valid solution to connect
your outbox table with your message broker.
www.thoughts-on-java.org
Implementing the Outbox Pattern with Hibernate
You need to keep the structure of the table and the contained
messages stable.
You need to be able to change your microservice internally.
You should try to not leak any internal details of your service.
To achieve all of this, most teams use a table that’s similar to the
following one. They use a UUID as the primary key, a JSON column
that contains the payload of the message and a few additional
columns to describe the message.
The message is often times based on the aggregate for which the
message was created. So, if your microservice manages books, the
aggregate root might be the book itself, which includes a list of
chapters.
Whenever a book gets created or changed or when a chapter gets
added, a new message for the book gets added to the outbox table.
The payload of the message can be a JSON representation of the full
aggregate, e.g. a book with all chapters, or a message-specific subset
of the aggregate. I prefer to include the full aggregate in the message,
but that’s totally up to you.
www.thoughts-on-java.org
Implementing the Outbox Pattern with Hibernate
In general, I recommend using a simple SQL INSERT statement which
you execute as a native query. Using an entity doesn’t provide you
any benefits because it’s a one-time write operation. You will not
read, update or remove the database record. You will also not map
any managed association to it. So, there is no need to map the outbox
table to an entity class or to manage the lifecycle of an entity object.
In your business code, you can now call this method with an instance
of the Book entity and an enum value that represents the kind of
operation (create, update or remove) performed on the aggregate.
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
OutboxUtil.writeBookToOutbox(em, b, Operation.CREATE);
em.getTransaction().commit();
em.close();
www.thoughts-on-java.org