Microservices architecture and distributed transaction











up vote
2
down vote

favorite












microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it. Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api) and it has no knowledge about distributed transaction it rises some issues you have to deal with when something goes wrong: suppose u have microservice "make payment for item". When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work. If every part of this sequence succeded everything is good but the question is how to deal with errors, another api you are calling is unavailable, but your have changed state in many other systems, how to recover from there? is there some good approch to such situations?










share|improve this question




























    up vote
    2
    down vote

    favorite












    microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it. Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api) and it has no knowledge about distributed transaction it rises some issues you have to deal with when something goes wrong: suppose u have microservice "make payment for item". When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work. If every part of this sequence succeded everything is good but the question is how to deal with errors, another api you are calling is unavailable, but your have changed state in many other systems, how to recover from there? is there some good approch to such situations?










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it. Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api) and it has no knowledge about distributed transaction it rises some issues you have to deal with when something goes wrong: suppose u have microservice "make payment for item". When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work. If every part of this sequence succeded everything is good but the question is how to deal with errors, another api you are calling is unavailable, but your have changed state in many other systems, how to recover from there? is there some good approch to such situations?










      share|improve this question















      microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it. Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api) and it has no knowledge about distributed transaction it rises some issues you have to deal with when something goes wrong: suppose u have microservice "make payment for item". When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work. If every part of this sequence succeded everything is good but the question is how to deal with errors, another api you are calling is unavailable, but your have changed state in many other systems, how to recover from there? is there some good approch to such situations?







      microservices






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 21:34

























      asked Nov 10 at 21:14









      Macko

      13119




      13119
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          There is no right or wrong question actually. But here is my point of view.



          Let's break it down:




          microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it




          Exactly, normally, you are to avoid having distributed transactions, that is one of important points.




          Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api)




          Normally, you don't call another microservice, otherwise it turns to become distributed monolith where all your so called microservices depend on each other as if they would be in single executable. When talking about microservices, it is all about to make them as independent as possible. This can be achieved with various techniques, one of which is Event Sourcing for example. Where you define your events and microservices processing them.




          When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work.




          In terms of Event Sourcing here you are talking about Sagas. The processes that orchestrate the work done.




          but your have changed state in many other systems, how to recover from there




          This is design issues, as pointed before, having microservices forming distributed monolith is not actually microservices.



          In general, microservice is not just a separated executable. It is design practice. Where you are designing your system in the way, such kind of questions are not happening. I would suggest reading authors on DDD (Domain Driven Design), Event Sourcing, CQRS, Bounded Context etc. This probably will make things more clear to you. Like Martin Fowler, Greg Young. Will try adding names here is it will come to my mind.






          share|improve this answer























          • mentioned flow of calls is only simple case and calling other apis from microservice is rather standard way of doing things synchronously. Requirement is that call to my microservice has to return execution result right now so before returning info to client i have to be sure everything ended with success including resolving errors in comunication to other apis which mostly can't be changed to my needs.
            – Macko
            Nov 10 at 22:02










          • That is the point. If you are putting that to be sure everything ended is requirement, how your microservice can make sure that everything is ended. Microservice is responsible for some Bounded Context, for single type of task. Otherwise it seems to be not microservice if it knows everything. It is either distributed monolith or Saga, choose one. The point of microservices is to split your architecture in the way that you have identified boundaries and responsible microservices, then you need to either orchestrate them, or design your front facing API accordingly.
            – muradm
            Nov 10 at 22:11










          • still not clear: so my microservice should only response to client "work has been initiated with success"? what if client want to have result of entire work to check if work is done - has to subscribe to bus or use polling?
            – Macko
            Nov 10 at 22:16










          • Not necessarily. That depends on your design. You will have like microservice to send email notification, microservice to create customer, microservice to put order etc. Each of them is responsible for a some clearly bounded context. Then how you initiate them, also depends. For instance look at this: microservices.io/patterns/data/saga.html. If you are ok with initiating commands and polling result, that is one way. If you require to "block", you can make kinda of wrapping process or saga, that will keep track of execution. That all depends on context, domain and requirements.
            – muradm
            Nov 10 at 22:22










          • ok, so this is my requirement: microservice should respond in sync pattern to client. How to do this to be ok with microservice architecture?
            – Macko
            Nov 11 at 17:33











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243468%2fmicroservices-architecture-and-distributed-transaction%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          There is no right or wrong question actually. But here is my point of view.



          Let's break it down:




          microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it




          Exactly, normally, you are to avoid having distributed transactions, that is one of important points.




          Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api)




          Normally, you don't call another microservice, otherwise it turns to become distributed monolith where all your so called microservices depend on each other as if they would be in single executable. When talking about microservices, it is all about to make them as independent as possible. This can be achieved with various techniques, one of which is Event Sourcing for example. Where you define your events and microservices processing them.




          When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work.




          In terms of Event Sourcing here you are talking about Sagas. The processes that orchestrate the work done.




          but your have changed state in many other systems, how to recover from there




          This is design issues, as pointed before, having microservices forming distributed monolith is not actually microservices.



          In general, microservice is not just a separated executable. It is design practice. Where you are designing your system in the way, such kind of questions are not happening. I would suggest reading authors on DDD (Domain Driven Design), Event Sourcing, CQRS, Bounded Context etc. This probably will make things more clear to you. Like Martin Fowler, Greg Young. Will try adding names here is it will come to my mind.






          share|improve this answer























          • mentioned flow of calls is only simple case and calling other apis from microservice is rather standard way of doing things synchronously. Requirement is that call to my microservice has to return execution result right now so before returning info to client i have to be sure everything ended with success including resolving errors in comunication to other apis which mostly can't be changed to my needs.
            – Macko
            Nov 10 at 22:02










          • That is the point. If you are putting that to be sure everything ended is requirement, how your microservice can make sure that everything is ended. Microservice is responsible for some Bounded Context, for single type of task. Otherwise it seems to be not microservice if it knows everything. It is either distributed monolith or Saga, choose one. The point of microservices is to split your architecture in the way that you have identified boundaries and responsible microservices, then you need to either orchestrate them, or design your front facing API accordingly.
            – muradm
            Nov 10 at 22:11










          • still not clear: so my microservice should only response to client "work has been initiated with success"? what if client want to have result of entire work to check if work is done - has to subscribe to bus or use polling?
            – Macko
            Nov 10 at 22:16










          • Not necessarily. That depends on your design. You will have like microservice to send email notification, microservice to create customer, microservice to put order etc. Each of them is responsible for a some clearly bounded context. Then how you initiate them, also depends. For instance look at this: microservices.io/patterns/data/saga.html. If you are ok with initiating commands and polling result, that is one way. If you require to "block", you can make kinda of wrapping process or saga, that will keep track of execution. That all depends on context, domain and requirements.
            – muradm
            Nov 10 at 22:22










          • ok, so this is my requirement: microservice should respond in sync pattern to client. How to do this to be ok with microservice architecture?
            – Macko
            Nov 11 at 17:33















          up vote
          2
          down vote













          There is no right or wrong question actually. But here is my point of view.



          Let's break it down:




          microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it




          Exactly, normally, you are to avoid having distributed transactions, that is one of important points.




          Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api)




          Normally, you don't call another microservice, otherwise it turns to become distributed monolith where all your so called microservices depend on each other as if they would be in single executable. When talking about microservices, it is all about to make them as independent as possible. This can be achieved with various techniques, one of which is Event Sourcing for example. Where you define your events and microservices processing them.




          When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work.




          In terms of Event Sourcing here you are talking about Sagas. The processes that orchestrate the work done.




          but your have changed state in many other systems, how to recover from there




          This is design issues, as pointed before, having microservices forming distributed monolith is not actually microservices.



          In general, microservice is not just a separated executable. It is design practice. Where you are designing your system in the way, such kind of questions are not happening. I would suggest reading authors on DDD (Domain Driven Design), Event Sourcing, CQRS, Bounded Context etc. This probably will make things more clear to you. Like Martin Fowler, Greg Young. Will try adding names here is it will come to my mind.






          share|improve this answer























          • mentioned flow of calls is only simple case and calling other apis from microservice is rather standard way of doing things synchronously. Requirement is that call to my microservice has to return execution result right now so before returning info to client i have to be sure everything ended with success including resolving errors in comunication to other apis which mostly can't be changed to my needs.
            – Macko
            Nov 10 at 22:02










          • That is the point. If you are putting that to be sure everything ended is requirement, how your microservice can make sure that everything is ended. Microservice is responsible for some Bounded Context, for single type of task. Otherwise it seems to be not microservice if it knows everything. It is either distributed monolith or Saga, choose one. The point of microservices is to split your architecture in the way that you have identified boundaries and responsible microservices, then you need to either orchestrate them, or design your front facing API accordingly.
            – muradm
            Nov 10 at 22:11










          • still not clear: so my microservice should only response to client "work has been initiated with success"? what if client want to have result of entire work to check if work is done - has to subscribe to bus or use polling?
            – Macko
            Nov 10 at 22:16










          • Not necessarily. That depends on your design. You will have like microservice to send email notification, microservice to create customer, microservice to put order etc. Each of them is responsible for a some clearly bounded context. Then how you initiate them, also depends. For instance look at this: microservices.io/patterns/data/saga.html. If you are ok with initiating commands and polling result, that is one way. If you require to "block", you can make kinda of wrapping process or saga, that will keep track of execution. That all depends on context, domain and requirements.
            – muradm
            Nov 10 at 22:22










          • ok, so this is my requirement: microservice should respond in sync pattern to client. How to do this to be ok with microservice architecture?
            – Macko
            Nov 11 at 17:33













          up vote
          2
          down vote










          up vote
          2
          down vote









          There is no right or wrong question actually. But here is my point of view.



          Let's break it down:




          microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it




          Exactly, normally, you are to avoid having distributed transactions, that is one of important points.




          Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api)




          Normally, you don't call another microservice, otherwise it turns to become distributed monolith where all your so called microservices depend on each other as if they would be in single executable. When talking about microservices, it is all about to make them as independent as possible. This can be achieved with various techniques, one of which is Event Sourcing for example. Where you define your events and microservices processing them.




          When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work.




          In terms of Event Sourcing here you are talking about Sagas. The processes that orchestrate the work done.




          but your have changed state in many other systems, how to recover from there




          This is design issues, as pointed before, having microservices forming distributed monolith is not actually microservices.



          In general, microservice is not just a separated executable. It is design practice. Where you are designing your system in the way, such kind of questions are not happening. I would suggest reading authors on DDD (Domain Driven Design), Event Sourcing, CQRS, Bounded Context etc. This probably will make things more clear to you. Like Martin Fowler, Greg Young. Will try adding names here is it will come to my mind.






          share|improve this answer














          There is no right or wrong question actually. But here is my point of view.



          Let's break it down:




          microservices are present some time ago, there are pros and cons of this approach but one of the points u will eventually have to face with is transaction atomicity or rather not having it




          Exactly, normally, you are to avoid having distributed transactions, that is one of important points.




          Enterprise apps usually have some kind of Unit Of Work on api level but in environment where your microservice is calling another microservice (or api)




          Normally, you don't call another microservice, otherwise it turns to become distributed monolith where all your so called microservices depend on each other as if they would be in single executable. When talking about microservices, it is all about to make them as independent as possible. This can be achieved with various techniques, one of which is Event Sourcing for example. Where you define your events and microservices processing them.




          When client calls your microservice api this method internally: place some data in owned database, create invoice file, sends it to another microservice, send email, maybe call another system which knows nothing about your unit of work.




          In terms of Event Sourcing here you are talking about Sagas. The processes that orchestrate the work done.




          but your have changed state in many other systems, how to recover from there




          This is design issues, as pointed before, having microservices forming distributed monolith is not actually microservices.



          In general, microservice is not just a separated executable. It is design practice. Where you are designing your system in the way, such kind of questions are not happening. I would suggest reading authors on DDD (Domain Driven Design), Event Sourcing, CQRS, Bounded Context etc. This probably will make things more clear to you. Like Martin Fowler, Greg Young. Will try adding names here is it will come to my mind.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 21:38









          Sergio Tulentsev

          177k29286301




          177k29286301










          answered Nov 10 at 21:35









          muradm

          732419




          732419












          • mentioned flow of calls is only simple case and calling other apis from microservice is rather standard way of doing things synchronously. Requirement is that call to my microservice has to return execution result right now so before returning info to client i have to be sure everything ended with success including resolving errors in comunication to other apis which mostly can't be changed to my needs.
            – Macko
            Nov 10 at 22:02










          • That is the point. If you are putting that to be sure everything ended is requirement, how your microservice can make sure that everything is ended. Microservice is responsible for some Bounded Context, for single type of task. Otherwise it seems to be not microservice if it knows everything. It is either distributed monolith or Saga, choose one. The point of microservices is to split your architecture in the way that you have identified boundaries and responsible microservices, then you need to either orchestrate them, or design your front facing API accordingly.
            – muradm
            Nov 10 at 22:11










          • still not clear: so my microservice should only response to client "work has been initiated with success"? what if client want to have result of entire work to check if work is done - has to subscribe to bus or use polling?
            – Macko
            Nov 10 at 22:16










          • Not necessarily. That depends on your design. You will have like microservice to send email notification, microservice to create customer, microservice to put order etc. Each of them is responsible for a some clearly bounded context. Then how you initiate them, also depends. For instance look at this: microservices.io/patterns/data/saga.html. If you are ok with initiating commands and polling result, that is one way. If you require to "block", you can make kinda of wrapping process or saga, that will keep track of execution. That all depends on context, domain and requirements.
            – muradm
            Nov 10 at 22:22










          • ok, so this is my requirement: microservice should respond in sync pattern to client. How to do this to be ok with microservice architecture?
            – Macko
            Nov 11 at 17:33


















          • mentioned flow of calls is only simple case and calling other apis from microservice is rather standard way of doing things synchronously. Requirement is that call to my microservice has to return execution result right now so before returning info to client i have to be sure everything ended with success including resolving errors in comunication to other apis which mostly can't be changed to my needs.
            – Macko
            Nov 10 at 22:02










          • That is the point. If you are putting that to be sure everything ended is requirement, how your microservice can make sure that everything is ended. Microservice is responsible for some Bounded Context, for single type of task. Otherwise it seems to be not microservice if it knows everything. It is either distributed monolith or Saga, choose one. The point of microservices is to split your architecture in the way that you have identified boundaries and responsible microservices, then you need to either orchestrate them, or design your front facing API accordingly.
            – muradm
            Nov 10 at 22:11










          • still not clear: so my microservice should only response to client "work has been initiated with success"? what if client want to have result of entire work to check if work is done - has to subscribe to bus or use polling?
            – Macko
            Nov 10 at 22:16










          • Not necessarily. That depends on your design. You will have like microservice to send email notification, microservice to create customer, microservice to put order etc. Each of them is responsible for a some clearly bounded context. Then how you initiate them, also depends. For instance look at this: microservices.io/patterns/data/saga.html. If you are ok with initiating commands and polling result, that is one way. If you require to "block", you can make kinda of wrapping process or saga, that will keep track of execution. That all depends on context, domain and requirements.
            – muradm
            Nov 10 at 22:22










          • ok, so this is my requirement: microservice should respond in sync pattern to client. How to do this to be ok with microservice architecture?
            – Macko
            Nov 11 at 17:33
















          mentioned flow of calls is only simple case and calling other apis from microservice is rather standard way of doing things synchronously. Requirement is that call to my microservice has to return execution result right now so before returning info to client i have to be sure everything ended with success including resolving errors in comunication to other apis which mostly can't be changed to my needs.
          – Macko
          Nov 10 at 22:02




          mentioned flow of calls is only simple case and calling other apis from microservice is rather standard way of doing things synchronously. Requirement is that call to my microservice has to return execution result right now so before returning info to client i have to be sure everything ended with success including resolving errors in comunication to other apis which mostly can't be changed to my needs.
          – Macko
          Nov 10 at 22:02












          That is the point. If you are putting that to be sure everything ended is requirement, how your microservice can make sure that everything is ended. Microservice is responsible for some Bounded Context, for single type of task. Otherwise it seems to be not microservice if it knows everything. It is either distributed monolith or Saga, choose one. The point of microservices is to split your architecture in the way that you have identified boundaries and responsible microservices, then you need to either orchestrate them, or design your front facing API accordingly.
          – muradm
          Nov 10 at 22:11




          That is the point. If you are putting that to be sure everything ended is requirement, how your microservice can make sure that everything is ended. Microservice is responsible for some Bounded Context, for single type of task. Otherwise it seems to be not microservice if it knows everything. It is either distributed monolith or Saga, choose one. The point of microservices is to split your architecture in the way that you have identified boundaries and responsible microservices, then you need to either orchestrate them, or design your front facing API accordingly.
          – muradm
          Nov 10 at 22:11












          still not clear: so my microservice should only response to client "work has been initiated with success"? what if client want to have result of entire work to check if work is done - has to subscribe to bus or use polling?
          – Macko
          Nov 10 at 22:16




          still not clear: so my microservice should only response to client "work has been initiated with success"? what if client want to have result of entire work to check if work is done - has to subscribe to bus or use polling?
          – Macko
          Nov 10 at 22:16












          Not necessarily. That depends on your design. You will have like microservice to send email notification, microservice to create customer, microservice to put order etc. Each of them is responsible for a some clearly bounded context. Then how you initiate them, also depends. For instance look at this: microservices.io/patterns/data/saga.html. If you are ok with initiating commands and polling result, that is one way. If you require to "block", you can make kinda of wrapping process or saga, that will keep track of execution. That all depends on context, domain and requirements.
          – muradm
          Nov 10 at 22:22




          Not necessarily. That depends on your design. You will have like microservice to send email notification, microservice to create customer, microservice to put order etc. Each of them is responsible for a some clearly bounded context. Then how you initiate them, also depends. For instance look at this: microservices.io/patterns/data/saga.html. If you are ok with initiating commands and polling result, that is one way. If you require to "block", you can make kinda of wrapping process or saga, that will keep track of execution. That all depends on context, domain and requirements.
          – muradm
          Nov 10 at 22:22












          ok, so this is my requirement: microservice should respond in sync pattern to client. How to do this to be ok with microservice architecture?
          – Macko
          Nov 11 at 17:33




          ok, so this is my requirement: microservice should respond in sync pattern to client. How to do this to be ok with microservice architecture?
          – Macko
          Nov 11 at 17:33


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243468%2fmicroservices-architecture-and-distributed-transaction%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          The Sandy Post

          Danny Elfman

          Pages that link to "Head v. Amoskeag Manufacturing Co."