{"id":29,"date":"2019-11-11T00:20:42","date_gmt":"2019-11-11T00:20:42","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/microservices\/?p=29"},"modified":"2019-11-11T00:36:28","modified_gmt":"2019-11-11T00:36:28","slug":"how-choreography-based-saga-works-in-microservices","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/microservices\/how-choreography-based-saga-works-in-microservices\/","title":{"rendered":"How Choreography-based Saga Works in Microservices"},"content":{"rendered":"<p>In this article we would explain how a Choreography-based Saga works in microservices. Then present a simple algorithm.<\/p>\n<p>In Choreograph-based saga approach, each local transaction emits domain events. This events then triggers local transaction in other services subscribed to these events.<\/p>\n<p>So assuming there are three services A, B and C. When a local transaction occurs in A, it publishes an event that triggers a local transaction in B. Then B executes a local transaction an also publishes event. This event published by B triggers a transaction in C.<\/p>\n<p>You can now see that in choreography, there is no need for some central system to coordinate the communication. All the services communicate among themselves.<\/p>\n<p>This is illustrated in the Figure 1 below:<\/p>\n<figure id=\"attachment_30\" aria-describedby=\"caption-attachment-30\" style=\"width: 493px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-me.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-30 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-me-1024x516.jpg\" alt=\"\" width=\"493\" height=\"249\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-me-1024x516.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-me-300x151.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-me-768x387.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-me.jpg 1293w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/a><figcaption id=\"caption-attachment-30\" class=\"wp-caption-text\">Figure 1: Choreography-Based Saga<\/figcaption><\/figure>\n<h5><strong>How it Works<\/strong><\/h5>\n<p>Let&#8217;s take an online store for example. We have two services:<\/p>\n<p><em>Order Service<\/em>: responsible for handling orders<\/p>\n<p><em>Customer Service:<\/em> responsible for maintaining customer records<\/p>\n<p>In this case, a choreography-based saga would have the following steps:<\/p>\n<ol>\n<li>The Order Service creates a new order. It sets the state to pending<\/li>\n<li>The Order Service publishes an Order Created event<\/li>\n<li>The Customer Service receives this event<\/li>\n<li>The Customer Service attempts to reserve credit for this order<\/li>\n<li>If the credit was reserved successfully, then the Customer Service published a Credit Reserved event\n<ul>\n<li>This event is received by the Order Service<\/li>\n<li>The Order Service sets the state of the order to approved<\/li>\n<\/ul>\n<\/li>\n<li>If the credit was not sufficient, then the Customer Service publishes a Credit Limit Exceeded event\n<ul>\n<li>This event is received by the Order Service<\/li>\n<li>The Order Service sets the state of the order to approved<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong>Choreography-based Saga Algorithm in Detail<\/strong><\/h5>\n<p>Now let&#8217;s try to determine the time between when the saga starts and ends. Take a look at Figure 2. We would just use generic names for the services: MS1 and MS2. We also assume that the events pass through a service discovery gateway. We also assume the MS1 and MS2 are associated with databases DB1 and DB2.<\/p>\n<figure id=\"attachment_31\" aria-describedby=\"caption-attachment-31\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-in-details.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-31 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-in-details.jpg\" alt=\"\" width=\"550\" height=\"338\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-in-details.jpg 888w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-in-details-300x184.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Choreography-Based-Saga-in-details-768x472.jpg 768w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-31\" class=\"wp-caption-text\">Figure 2 &#8211; Choreography-Based Saga Analysis in details<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h5><strong>The steps would be as follows:<\/strong><\/h5>\n<p><b>Step 1<\/b>: External event fires a method is MS1 that\u00a0 changes the state attribute of C1 from S1 to S2. (This could be a client app)<\/p>\n<p><b>Step 2<\/b>: DB1 saves the time T1 of the change in the TimeStamp attribute of C1<\/p>\n<p><b>Step 3<\/b>: Once the updates is complete, MS1 fires an event called <b>MS1_state_change_success()<\/b><\/p>\n<p><b>Step 4<\/b>: MS1 calls a method <b>change_state_to_S2() <\/b>on MS2<\/p>\n<p><b>Step 5<\/b>: MS2 executes a logic on DB2 that changes the state attribute of collection C2 from S1 to S2<\/p>\n<p><b>Step 6<\/b>: The logic fails<\/p>\n<p><b>Step 7<\/b>: MS2 fires an event called <b>MS2_state_change_failure()<\/b><\/p>\n<p><b>Step 8<\/b>: The <b>MS2_state_change_failure() <\/b>event rolls back the transaction on MS2<\/p>\n<p><b>Step 9<\/b>: Once the rollback is complete, MS2 fires and event called <b>change_state_to_S1()<\/b> on MS1<\/p>\n<p><b>Step 10<\/b>: MS1 executes a logic on DB1 that rolls back the state of C1 from S2 to S1<\/p>\n<p><b>Step 11<\/b>: The new Timestamp is also recorded as T2<\/p>\n<p><b>Step 12<\/b>: Difference between T1 and T2 is recorded as time taken to complete the choreography<\/p>\n<p>&nbsp;<\/p>\n<h5><strong>Benefits and Limitations of Choreography-based Saga<\/strong><\/h5>\n<p>Some <a href=\"https:\/\/thesai.org\/Downloads\/Volume9No8\/Paper_4-Comparison_of_Event_Choreography.pdf\">research(by Chaitanya K. Rudrabhatla)<\/a> shows that event choreography has a better performance than orchestration. However, as the number of service increases, the implementation becomes very complex since there is the need to handle multiple events.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we would explain how a Choreography-based Saga works in microservices. Then present a simple algorithm. In Choreograph-based saga approach, each local transaction &hellip; <\/p>\n","protected":false},"author":1,"featured_media":32,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[11,10],"class_list":["post-29","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microservices","tag-choreography","tag-choreography-based-saga"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/29\/revisions\/37"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media\/32"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}