Update database using events in Springboot - java

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?

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.

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

Related

Distributed transaction among services in a microservice system, using spring-cloud

The current situation
Got a project using: spring-boot, spring-cloud, postgresql, as a microservice system.
There are 2 services, say SA and SB, they operate on 2 RDBMS databases respectively, say DA and DB.
Now, there is an operation contains 2 sub steps:
Http client would make a request to service SA, to save a record RA, into DA.
Then, SA send a request to service SB, to save a record RB, into DB.
As a whole, the 2 sub steps should either both commit, or both rollback.
Analysis
If move both operations into a single service, then could use Spring's distributed transaction to sovled it with JTA (based on 2PC protocol).
But here, the 2 operations are in 2 services, and they communicated via http REST protocol. Maybe could use mq + compensation to solve this, but I am not sure is there a better approach.
The questions are
In this case, does JTA (based on 2PC protocol) still work?
If not, what is the preferred solution?
Possible solutions I can guess:
Refactor code to move the 2 operations into a single service.
Implementat the mq + compensation architecture to support this.
Maybe this project is helpful for you https://github.com/apache/servicecomb-pack
Apache ServiceComb Pack is an eventually data consistency solution for micro-service applications. ServiceComb Pack currently provides TCC and Saga distributed transaction co-ordination solutions by using Alpha as a transaction coordinator and Omega as an transaction agent

How to manage transaction across micro services using 2 phase commit protocol?

I have two micro services. One is in spring boot and other is using simple jdbc apis to perform database operation. I have to perform database operation using these two services.
(i.e.
Insert using jdbc service,
Insert using spring-boot service,
Insert using jdbc service).
These operation should follow acid property.
I have tried saga pattern using axon framework, it is working fine but I want to do it by using 2pc protocol. I have tried 2pc protocol in jdbc service but it is working only for transactions happened only in this service. I also used atomikos framework in spring service, it is working for this service only.
Is there any way to co-ordinate javax.transaction and springframework.transaction ?
For the scenario described in the question, you should try using Oracle Transaction Manager for Microservices (MicroTx). It is a free product that comes with a transaction manager and client library for microservices written in Java and node.js. With this, you can create XA (2PC) transactions involving multiple microservices. Oracle MicroTx - https://www.oracle.com/database/transaction-manager-for-microservices

How to invoke a method in spring boot, when table gets updated in mysql

My query is, how to invoke a method in spring boot project when a table is being updated in MySQL DB.
Case 1: Let's suppose I have a Mysql Db that is shared with two application and any particular table is updated frequently.
So, my issue is how can I notify another application (Spring-boot) to be aware of that update so that it can process that data.
Note I don't want to use the scheduler.
Thanks,
There could be many solutions depending on which technology you use. If you are using Spring-data, then you can use events and from within eventhandler notify other application (you can use rest integration, amqp, ...)
This question has nothing to do with Springboot or any other application.
Question here is how should two applications(web or non web) communicate with each other.
With respect to communication, it depends on the requirement, whether you need Synchronous communication or Asynchronous communication.
If its Synchronous communication, you may use Rest Template or any other, but it blocks the current request, if other application is running as service.
If its Asynchronous communication(where you don't want to wait for result and client is not blocked), you use Message Brokers like Kafka, ActiveMq
I think you can either make async call feature of spring boot application, but consider what should you if the request fails?

Intercepting requests in spring

I have my service layer 'S' calling data layer 'D_OLD'. Now I am upgrading my D_OLD database version to D_NEW. But I want to test stability for a few days and if not stable, I want to rollback. But if I rollback, I must be able to replay all the transactions happened on new database to old database.
What I am planning is to have two instances of D, one with new database and other running with old database.
I am planning to intercept all the PUT/POST/DELETE requests from S to D_NEW and put it in a kafka topic. I will have a job to replay that requests into D_OLD.
Now my S is a spring application using ribbon for making requests to D_NEW. I have tried ClientHttpRequestInterceptor, but seems it work only with RestTemplate.
Is there any way to intercept outgoing requests in ribbon/spring/tomcat?

spring data mongodb connect to multiple database instance for each tenant

I have a web application which uses Spring and connects to mongdb using spring data mongodb.
I want to extend it by connecting to multiple database instance for each tenant. There are some solutions which are available on stackoverflow. One was mentioned on Multi-tenant mongodb database based with spring data where the code was on github on the following link-
https://github.com/Loki-Afro/multi-tenant-spring-mongodb
I have a question regarding this? Will this approach work for concurrent requests?
I am also doing the same thing, autowiring the MongoTemplate and Repository.
Since these beans are singletons, they will be the same for each request. Then how will the database change for different request and what happens when multiple requests are being served in parallel?

Categories

Resources