23

I have read some articles about microservices architecture, but no one takes the topic of transaction. All that they says that this is hard to do it. Maybe someone can describe how to handle this?

But not from domain side, but from technology side. Lets say that we have business case where we need to invoke two different services and both of them make some changes on database. But how to rollback if some error occurs on the second one?

Who knows some libraries or design patter for this problem?

Kostas
  • 8,356
  • 11
  • 47
  • 63
KirkoR
  • 788
  • 6
  • 13

6 Answers6

9

I may not be the ultimate expert in this, but I'm sure you're heading towards the Distributed Transactions. In order to have them running, all the application service components need a common shared transaction id, and you have to make sure that every component is informed about the state of the transaction. It is asynchronous, so you'll require substantial prog skills.

Here are distributed transactions mentioned or discussed:

https://en.wikipedia.org/wiki/Distributed_transaction

http://contino.co.uk/microservices-not-a-free-lunch/

http://martinfowler.com/articles/microservices.html

It would seem people try to avoid it as it is difficult. Maybe that's why you don't find much about.

Hope this helps a step forward :-)

peter_the_oak
  • 3,529
  • 3
  • 23
  • 37
6

The best design is having isolated services: each service just do its work within its own transaction and your workflow expects failures on the single service.

If you really need to commit only if all the services are called without errors you should create an higher level service that perform those calls inside an external transaction.

davmcpaul
  • 76
  • 2
  • 1
    What do you mean: > you should create an higher level service – KirkoR Jul 10 '14 at 13:05
  • 1
    I mean that you should have another service that will initialize a global transaction and will call your other services using this transaction to commit/rollback depending on the result of all your calls. I don't know how this result can ben achieved exactly, it depends on the technologies you're using. I think that this article can provide some useful information (http://docs.oracle.com/cd/E17904_01/web.1111/e13734/transaction.htm). Anway, as told before, it's a really challenging context and would be much better using individual transaction for each service. – davmcpaul Jul 10 '14 at 13:48
  • Here you can find a useful real-life realted example of how your problem can be approached: http://www.eaipatterns.com/ramblings/18_starbucks.html – davmcpaul Jul 10 '14 at 14:02
2

The first raw thing which came to my mind after reading this question is to create every add api with a delete api with ,lets say, an extra boolean flag delFlag.

boolean flag delFlag;

For POST, it will be 0. For DELETE, it will be 1.

Now you maintain a Transaction Manager which is a super service to all your micro services. In this service maintain the calling queue of all the services and the APIs. As and when a service fails, get the calling api and call the delete method of that service and undone whatever u have done.

PS- Just a raw thought. Correct me if you think it is wrong.

deep
  • 1,586
  • 1
  • 18
  • 29
-1

Building on top of previous answers, distributed transactions are the solution. In my opinion you don't want to build your own mechanisms for tracking global transactional state, rather you want to use some kind of product - there are several out there. I have written a lengthy blog article about solving this issue with a Java application server:

http://blog.maxant.co.uk/pebble/2015/08/04/1438716480000.html

Ant Kutschera
  • 6,257
  • 4
  • 29
  • 40
-1

two-phase commit can be option.Coordinator send commit request message to cohorts.Cohorts send back ok.After then coordinator sends commit message to cohorts.If any failure happpens coordinator sends rollback messages to cohorts.

-1

You can use a workflow engine(like JBPM,Activiti) to orchestrate the logic and handle transaction failures or compensatory transactions in it to achieve data integrity. This is the similar case you use in SOA architecture with ESB,BPMN and Web services

Rohitdev
  • 866
  • 6
  • 15