Outbox Pattern with Debezium – Part 2

To illustrate the implementation of the Outbox Pattern with Debezium, let’s consider a system with two services: student-service and other-service:

  • The student service will expose an API to add new student information to the database.
  • This newly added student information needs to be communicated to the Other Service for further processing of additional logic.

We will use the Outbox Pattern to ensure data consistency! The newly added student information will be captured by Debezium and published to the Other Service via Apache Kafka.

The high-level architecture of our system will be as follows:

For example, I will create a new Maven multi-project with two projects, student-service and other-service, as follows:

Student Service

Student Service is a Spring Boot application that uses Web Starter, Data JPA Starter, PostgreSQL driver, Lombok, and Docker Compose Support.

For the PostgreSQL database server, we will use Spring Boot’s Docker Compose support to run a PostgreSQL database when the application starts. This PostgreSQL database will have its data capture configurations enabled by default. The content of the Docker Compose file will be as follows:

services:

The compose.yaml file will be located in the root directory of your project.

In the db.sql file in the src/main/resources directory, the student table will be defined as follows:

The entities of the Student class and its Repository class have the following contents:

and:

Also in this db.sql file, I defined the contents of the outbox table as follows:

    • The id column is used as the unique ID for each message sent to Apache Kafka.
    • aggregate_id is the ID of the domain object associated with the event. In my example, it’s the student ID.
    • type is the domain object model associated with the event; in my example, it’s student.
    • event_type is the type of event associated with the domain object model. The type can be add, update, delete, etc.
    • payload is the JSON content of the event.

I also added the entity and Repository class to this outbox table as follows:

and:

StudentController will expose the API for adding new students with the following content:

Along with inserting a new record into the student table, we will also insert a record into the outbox table. This outbox table will be monitored by Debezium so that whenever a new record is added, Debezium will pick it up and send a message to Apache Kafka.

The contents of the AddNewStudentRequest and AddNewStudentResponse classes are as follows:

Other Service

For the Other Service, I’ll also create a Spring Boot project using Spring Boot Starter Kafka.

The Other Service will subscribe to an Apache Kafka topic, the same topic where Debezium will publish the Student Service’s data changes! Then it will print the received data to the console. I’ll write the simple code as follows:

outboxpattern.public.outbox will be the name of the topic where Debezium publishes the message data change.

The application.yaml file for this service contains the following content:

I will define a service to run Apache Kafka in the Docker Compose file above as follows:

Debezium Server

For the Debezium Server, I will define a new service in the Docker Compose above to run it, as follows:

The Debezium configuration file in the src/main/resource/conf directory of the project root will have the following content:

With the above configuration, a topic named outboxpattern.public.outbox will be created in Apache Kafka! All changes to the outbox table will be captured and published to this topic.

Please refer to the tutorial Change Data Capture with Debezium for a better understanding of this configuration!

Now, if you run all system applications and then request the Student Service http://localhost:8081/students using the POST method, you will see the following result:

In the Other Service’s console log, you’ll see a received message, as follows:

In both the student table and the outbox table, you will also see a new record like this:

and:

So, we have successfully implemented the Outbox Pattern with Debezium!

You can watch the video here:

Add Comment