Denis Stephanov
Denis Stephanov

Reputation: 5241

Atomic operations between microservices

I need resolve atomic operations in my microservice based application based on spring boot. You can imagine 2 microservices as you can see on picture.

enter image description here

One service is using for saving user permissions and credentials as identity provider and another one as user account used in application to pair with orders, contacts, addresses and so on.

When user created new account I need create user for business purposes and user identity for authorization. Both operations should be executed as one atomic operation. It means if one failed, must bot failed and rollback. Cannot will be saved user credentials but without business user object. Can you tell me what is the best way to solve this or what steps should look like?

My first idea was call with RestTemplate of Feign one service from another, but I have no idea how to rollback operation if second failed. Thank you in advice.

Upvotes: 6

Views: 1341

Answers (1)

Jeff
Jeff

Reputation: 1955

If an atomic operation is necessary because of transactional consistency, these two services should be merged into one.

If the consistency can be more relaxed, you can look into sagas. In event-driven systems this is a way of managing complex business transactions with this kind of requirements. The concept of sagas provides a way to handle errors with compensating actions. So, if something goes wrong the system will recover to a consistent state. This prevents the use of two-phase commit in a distributed system.

Upvotes: 5

Related Questions