Search code examples
javahibernatetransactionsakkamicroservices

Transaction when inter-communicate microservices


I'm looking for best approach to keep data consistent while inter-communicate between microservices in my project.

Project built with Jersey + Guice and uses Akka to glue services in whole system. I use separate transaction across call to another service to avoid long-running transactions. I use Hibernate as ORM and Postgres as DB. Process looks like:

  1. Begin transaction
  2. Do some work
  3. Commit transaction
  4. Call to another service using Akka, receive or send some data
  5. Begin transaction
  6. Do remaining work
  7. Commit transaction

So if step 4 failed my data becomes in non consistent state. I spent some hours to find solution to avoid it but I was failed. Is there some right way to keep it in consistent state?


Solution

  • Well if you want to avoid long-running transactions, but want to have a logical equivalent of them which is still (possibly) long-running, then you are effectively pushing the "transaction" abstraction to the application level.

    The simplest solution is to just accept that and invent some column for your data indicating that the records are dirty, then make them non-dirty in step (6).