Home > Blockchain >  Update database using events in Springboot
Update database using events in Springboot

Time:01-06

I have a requirement to update a database using events in Springboot microservice. Each microservice has its own persistent layer. Microservices are communicating with each other using REST API's.

Scenario:

I have two microservices - Vendor microservice with vendor DB and Order microservice with order DB. When a request is received by the vendor microservice, it will update the vendor Db and also add an order in the order DB and all this should be done in one transaction.

I cannot use a REST API for calling the vendor service to update the order. If any transaction fails, everything should be rolled back. How can I achieve this using events or something similar?

CodePudding user response:

You can use a message queue like Kafka or RabbitMQ. Because you faced an issue known as two-phase commit. You can use transactional outbox pattern that is widely used in projects which consist of microservices.

CodePudding user response:

Spring already provides a consistent programming model for transactions. You can use

@Transactional

to achieve this.

You can read more about transactional annotation in the official documentation https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html

  •  Tags:  
  • Related