{"id":3777,"date":"2017-01-16T13:52:43","date_gmt":"2017-01-16T17:52:43","guid":{"rendered":"http:\/\/www.acarlstein.com\/?p=3777"},"modified":"2017-01-17T13:37:17","modified_gmt":"2017-01-17T17:37:17","slug":"microservices-brownfield-transactions","status":"publish","type":"post","link":"http:\/\/blog.acarlstein.com\/?p=3777","title":{"rendered":"Microservices: Brownfield: Transactions"},"content":{"rendered":"<p>\u00a0<i class=\"fa fa-arrow-left\"><span style=\"color: transparent; display: none;\">icon-arrow-left<\/span><\/i>\u00a0<a href=\"http:\/\/www.acarlstein.com\/?p=3768\">Microservices: Brownfield: Migration: Database<\/a>\u00a0|\u00a0<a href=\"http:\/\/www.acarlstein.com\/?p=3792\">Microservices: Brownfield: Reporting<\/a>\u00a0<i class=\"fa fa-arrow-right\"><span style=\"color: transparent; display: none;\">icon-arrow-right<\/span><\/i>\u00a0<\/p>\n<p>When moving from a monolithic system to a micro-service architectured system, we need a different approach when dealing with transactions.<\/p>\n<p>Transactions are useful:<\/p>\n<ul>\n<li>They ensure data integrity.<\/li>\n<li>They allow us to updates several records as part of one transaction.<\/li>\n<li>If one or more updates (and\/or creates) fails, we can roll the entire transaction back.<\/li>\n<\/ul>\n<p>In monolithic transactions are simple.\u00a0We can have one process which is updating and creating records. These records are part of the transaction; therefore, the same process can either commit the transaction or roll it back if there are any issues.<\/p>\n<p> In micro-services, transactions spanning are complex because there are several processes. This means that several micro-services are involved in complete one transaction. Since our transaction is distributed\u00a0along multiple micro-services, it becomes a complex procedure to observe and solve problems; therefore, it becomes complex to roll back.<\/p>\n<p>For example, we can have a order being place. This process will take several micro-services working together.<br \/>\n <a href=\"http:\/\/www.acarlstein.com\/?attachment_id=3781\" rel=\"attachment wp-att-3781\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3781\" src=\"http:\/\/www.elblender.com\/wordpress\/wp-content\/uploads\/2017\/01\/complex-transactions.png\" alt=\"\" width=\"608\" height=\"387\" srcset=\"http:\/\/blog.acarlstein.com\/wp-content\/uploads\/2017\/01\/complex-transactions.png 608w, http:\/\/blog.acarlstein.com\/wp-content\/uploads\/2017\/01\/complex-transactions-300x191.png 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/a><\/p>\n<p>If one of these micro-services fails when trying to\u00a0create or update a record, we will need to rollback the entire transaction.<\/p>\n<p>How to handle fail transactions:<\/p>\n<ul>\n<li>Option 1: Try again later.\n<ul>\n<li>The part of the transaction that failed is put into a queue so another service can pick it up and process.\n<ul>\n<li>Transaction will eventually be completed.<\/li>\n<li>It relies on other instances not failing with the same part of transaction.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Option 2: Abort the entire transaction.\n<ul>\n<li>We detect our transaction has failed, then we issue an Undo transaction to all the micro-services involved so they undo any creates or updates\n<ul>\n<li>Problems:\n<ul>\n<li>Who issue the undo transaction?<\/li>\n<li>What happens when the undo transaction fails itself.<\/li>\n<\/ul>\n<\/li>\n<li>One way to overcome this problem is to use a\u00a0transaction manager software.<br \/>\n <a href=\"http:\/\/www.acarlstein.com\/?attachment_id=3786\" rel=\"attachment wp-att-3784\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3786  aligncenter\" src=\"http:\/\/www.elblender.com\/wordpress\/wp-content\/uploads\/2017\/01\/transaction-manager-1.png\" width=\"499\" height=\"374\" srcset=\"http:\/\/blog.acarlstein.com\/wp-content\/uploads\/2017\/01\/transaction-manager-1.png 605w, http:\/\/blog.acarlstein.com\/wp-content\/uploads\/2017\/01\/transaction-manager-1-300x225.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/a><\/p>\n<ul>\n<li>This software use a two phase commit.<\/li>\n<li>Phase 1: All micro-services involved indicates to the transaction manager if they are fine to commit to their part of the transaction.<\/li>\n<li>Phase 2: If they are fine to commit, then the transaction manager tells all participating micro-services to commit the transaction.<\/li>\n<li>If any of the micro-services doesn&#8217;t respond or responds with a &#8220;no to committing&#8221; then the transaction manager tells to all the participating micro-services to rollback the transaction.<\/li>\n<li>Problem using transaction manager?\n<ul>\n<li>We are heavily dependent of it.\u00a0<\/li>\n<li>It delays the processing of our transactions. Potential bottleneck.<\/li>\n<li>Complex to implement.<\/li>\n<li>More complex when we need to communicate with a monolithic system.\n<ul>\n<li>This can be accomplish with the message queue.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n\n<script>\nvar zbPregResult = '0';\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0icon-arrow-left\u00a0Microservices: Brownfield: Migration: Database\u00a0|\u00a0Microservices: Brownfield: Reporting\u00a0icon-arrow-right\u00a0 When moving from a monolithic system to a micro-service architectured system, we need a different approach when dealing with transactions. Transactions are useful: They ensure data integrity. They allow us to updates several records as part of one transaction. If one or more updates (and\/or creates) fails, we can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,1472,1480],"tags":[286,1482,1479,1518,1517],"class_list":["post-3777","post","type-post","status-publish","format-standard","hentry","category-programming","category-architecture","category-microservices","tag-data-integrity","tag-micro-services","tag-microservices","tag-transaction-manager-software","tag-transactions"],"_links":{"self":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/3777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3777"}],"version-history":[{"count":11,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/3777\/revisions"}],"predecessor-version":[{"id":3794,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/3777\/revisions\/3794"}],"wp:attachment":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3777"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}