Call a function on db update

Viewed 45

I want to create a Spring Boot App that has an API that clients query to get data from. Also I'd like to update any change that is made on the db appear in the front in real time. Something like firebase does when updating a document. I'm using in-memory H2 Database at the moment as it is not something that I need to persist between runs (for now I guess...)

The API is not a problem but the real-time updates as that part is already done.

I thought about implementing pub-sub strategy or something like that but I'm a bit lost about it actually.

I also know the existence about WebFlux and have read info about it but I'm not sure that fulfill my needs.

1 Answers

I had implemented CDC (Change Data Capture) using Debezium.

To set it up, you create a relevant connector for your DB (example), it uses Kafka connect and keeps track of your DB operations.

When any DB operation (Insert, Update, Delete) occurs then it publishes CDC messages to a Kafka topic.

You can setup a Spring Kafka Consumer application to listen to this Kafka topic, consume the CDC events and react on the basis of the type of operation [ op = c (create), u (update), d (delete) ].

Here is a sample project that I have created. You can use it as a reference.

This is how do the Kafka messages look like - link

Related