SBB
SBB

Reputation: 17

Transaction management in microservices

We are rewriting legacy app using microservices. Each microservice has its own DB. There are certain api calls that require to call another microservice and persist data into both DBs. How to implement distributed transaction management effectively in this case?

Since we are not migrated completely to the new micro services environment, we still writeback data to old monolith. For this when an microservice end point is called, we call monolith service from microservice api to writeback same data. How to deal with the same problem in this case as well.

Thanks in advance.

Upvotes: 1

Views: 4999

Answers (3)

Vassilis
Vassilis

Reputation: 1054

Check out this answer on "Why is 2-phase commit not suitable for a microservices architecture?": https://stackoverflow.com/a/55258458/3794744

Upvotes: 0

Arnon Rotem-Gal-Oz
Arnon Rotem-Gal-Oz

Reputation: 25939

If you can afford to write to the legacy system later (i.e. allow some latency between updating the microservice and the legacy system) you can use the outbox pattern.

Essentially that means that you write to the microservice database in a transactional way both to the tables you usually write and an additional "outbox" table of changes to apply and then have a separate process that reads that table and updates the legacy system.

You can also achieve something similar with a change data capture mechanism on the db used in the microservice(s)

Upvotes: 0

30thh
30thh

Reputation: 11406

There are different distributer transaction frameworks usually included and maintained as part of heavy application servers like JBoss and WebLogic.

The standard usually used by such services is Jakarta Transactions (JTA; formerly Java Transaction API).

Tomcat and Spring don't support distributed transactions out-of-the-box. You can add this functionality using third party framework like Atomikos (just googled, I've never used it).

But remember, microservice with JTA ist not "micro" anymore :-)

Here is a small overview over available technologies and possible workarounds:

https://www.baeldung.com/transactions-across-microservices

Upvotes: 1

Related Questions