<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wiratama Paramasatya on Medium]]></title>
        <description><![CDATA[Stories by Wiratama Paramasatya on Medium]]></description>
        <link>https://medium.com/@wiratamaparamasatya?source=rss-7c315919239d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VbnWH0k02q4cpFdeILl1XA.jpeg</url>
            <title>Stories by Wiratama Paramasatya on Medium</title>
            <link>https://medium.com/@wiratamaparamasatya?source=rss-7c315919239d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 00:11:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wiratamaparamasatya/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Adopting Choreography-based Saga pattern on your Microservice Architecture]]></title>
            <link>https://medium.com/@wiratamaparamasatya/adopting-choreography-based-saga-pattern-on-your-microservice-architecture-fe05d758942f?source=rss-7c315919239d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe05d758942f</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[saga-pattern]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Wiratama Paramasatya]]></dc:creator>
            <pubDate>Mon, 21 Sep 2020 05:34:53 GMT</pubDate>
            <atom:updated>2020-09-21T05:34:53.061Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Opening image" src="https://cdn-images-1.medium.com/max/1024/0*JQhotdOV6NKW9S1I" /><figcaption>Photo by <a href="https://unsplash.com/@thisisengineering?utm_source=medium&amp;utm_medium=referral">ThisisEngineering RAEng</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Microservice architecture could be harmful if your inter-service communication not wisely designed. <strong>Data</strong> is the most important thing to take care of if you decide to implement microservice on your project, because your services will always relies on data.</p><p>In microservice architecture, you might have distributed transaction across services for sure. Data integrity between these services will always been an issue for your distributed transaction, so it’s your responsibility how you manage your data between those services. If you can&#39;t take care of your data like you raise your own pet, then you are poisoning your application…. and also yourself.</p><blockquote>“Anyone might face this issue for sure, then what? you got something?”</blockquote><figure><img alt="Detective norman and thomas angelo discuss about business proposition" src="https://cdn-images-1.medium.com/max/690/1*33oEC1KFlvxFi-1fwUKyZA.gif" /><figcaption>This footage is taken from https<a href="https://www.youtube.com/watch?v=3_mUquU6mZo&amp;t=305s">://www.youtube.com/watch?v=3_mUquU6mZo&amp;t=305s</a> (Mafia: Definitive Edition Intro)</figcaption></figure><blockquote>My answer is: “Yeah, I might have somethin’”</blockquote><h3>Let’s role play our case!</h3><p>For example, we have a user story like this:</p><blockquote>As a bank customer, I would like to pay up my electrical bill using my mobile banking app, So that I can pay up my bill with ease and can keep using electricity in my apartment.</blockquote><p>To deliver this feature, we decide to use microservice architecture and define several services to handle each business context. Since each services have their own responsibilities how they handle the business logic and managing their data, we can also adopt <a href="https://microservices.io/patterns/data/database-per-service.html#:~:text=A%20service&#39;s%20transactions%20only%20involve,the%20structure%20of%20this%20pattern.&amp;text=Schema-per-service%20%E2%80%93%20each,has%20it&#39;s%20own%20database%20server."><strong>Database per Service pattern</strong></a><strong> </strong>alongside with microservice architecture. Take a look at Figure 1.</p><figure><img alt="Figure 1. Business Service." src="https://cdn-images-1.medium.com/max/1024/1*ZpmvfefK1BzTz3O6jCRaLw.png" /><figcaption>Figure 1. Business Service.</figcaption></figure><ul><li><strong>transaction_service</strong>: Whenever customers try to pay up bills using mobile app, this service is responsible to receive those request and immediately response to the customer whether the transaction is succeed or failed, also create transaction history for single payment request. In the other words, this service will be the main API to pay up customer bills.</li><li><strong>account_service</strong>: This service will be the heart of the application, because this service is responsible to manage the customer’s balance.</li><li><strong>aggregator_service</strong>: This service is responsible to communicate with external service / 3rd party API. In our case, transfer several amount to our bill payment partner.</li></ul><p>Since we already defined our business service, then how those services communicate to each other? At this point, this is where our nightmare started. So let’s find our solution!</p><h3>API Call approach</h3><p>As a <a href="https://martinfowler.com/bliki/BeckDesignRules.html">4 rules of simple design</a> says, we need to make things work first so that we can deliver to the customer as soon as possible, so the simplest approach is using <strong>API call.</strong> Take a look at Figure 2.</p><figure><img alt="Figure 2. API Call Approach." src="https://cdn-images-1.medium.com/max/1024/1*D1UtICazbfPE5jhMqgvQQg.png" /><figcaption>Figure 2. API Call Approach.</figcaption></figure><p>This approach is pretty straightforward. When our customers pay up their bills, the mobile app will call <strong>transaction_service</strong> and create new transaction history on <strong>Transaction DB</strong>. Then <strong>transaction_service</strong> will call <strong>account_service</strong> using API call to deduct the customer balance on <strong>Account DB</strong>. Upon successfully deduct the customer balance, <strong>account_service </strong>will call <strong>aggregator_service </strong>to make an external API call to pay bill with several amount to our partner.</p><p>— Case closed, we able to deliver to the customers.</p><p><strong>The problem</strong></p><p>As we depends on API call between services, our services are basically tightly coupled to each other. Your mobile app will wait for success / fail response as long as it takes. Even the first request from mobile will relies on those internal communication between services, which is implementation detail.</p><p>What if the 3rd party API have a slow response time when our <strong>aggregator_service </strong>make a request? Or what if there is a network issue on our infra and leads to slow response time on our internal service? Do our customers really need to wait those request to fulfilled? They don’t care about how our implementation detail works. With this slow, frustating, and unresponsive app, dollar to doughnuts our customers will uninstall the app :(</p><h3>Message Queue Communication approach</h3><p>After we deliver the feature with <strong>API call </strong>approach, we gathered feedback from our customers. They don’t want slow response app, they don&#39;t need to immediately see the transaction result either the payment is success or fail, and also they don’t need to wait all those complex and internal implementation detail to be fulfilled, they want fast and reliable app.</p><p>So we arrived at our second approach, we replaced our internal <strong>API call </strong>using <a href="https://www.cloudamqp.com/blog/2014-12-03-what-is-message-queuing.html"><strong>MQ (Message Queue)</strong></a>. A message queue provides an <strong>asynchronous communications protocol,</strong> which is a system that puts a message onto a message queue and does not require an immediate response to continuing processing. There are many MQ products that you can use to implement this approach, like RabbitMQ, NSQ, Kafka, etc. Each MQ have its pros cons but I will not deep dive onto that. Take a look at Figure 3.</p><figure><img alt="Figure 3. Message Queue Communication Approach." src="https://cdn-images-1.medium.com/max/1024/1*rkwgS53Y3cPrN4Du8Llbbw.png" /><figcaption>Figure 3. Message Queue Communication Approach.</figcaption></figure><p>Since we replaced our internal communication protocol from <strong>API call </strong>to <strong>MQ</strong>, here is our new scenario:</p><ol><li>Customer make a bill payment request using mobile app and make API call to <strong>transaction_service</strong>.</li><li><strong>transaction_service</strong> will create transaction history on <strong>Transaction DB</strong>.</li><li><strong>transaction_service</strong> publish message to <strong>“bill_payment_request” </strong>topic on MQ.</li><li><strong>account_service </strong>consume topic<strong> </strong>from<strong> “bill_payment_request”</strong>.</li><li><strong>account_service </strong>will deduct customer balance on <strong>Account DB</strong>.</li><li><strong>account_service </strong>publish message to<strong> “transfer_request” </strong>topic on MQ.</li><li><strong>aggregator_service </strong>consume topic from <strong>“transfer_request”</strong>.</li><li><strong>aggregator_service </strong>will directly make an API call to external / 3rd party API.</li></ol><p>With this approach, customer don’t need to wait those 8 steps to be fulfilled since we use <strong>asynchronous </strong>protocol. Is this our final approach?</p><p><strong>The Problem</strong></p><p>Let’s play what-ifs again and think of the worst case, so let’s list down our concerns:</p><ol><li>What if there are any external failure when <strong>aggregator_service </strong>make an API call with 3rd party API? When a message from “<strong>transfer_request”</strong> topic already consumed by <strong>aggregator_service</strong>, customer balance already deducted<strong> </strong>on<strong> account_service</strong>,<strong> </strong>but in fact, the payment is remain failed. How we gonna compensating the whole process?</li><li>What happen when customer balance suddenly less than payment amount? Of course it should not deduct customer balance, but how our customer know this problem occur? Since our internal communication is fully asynchronous.</li><li>For every failure occurs either on <strong>account_service</strong> or <strong>aggregator_service</strong>, <strong>transaction_service</strong> will never know this problem. However, the transaction history already created and the customer will expect the transaction is succeed. In fact, the payment is failed.</li></ol><p>This approach leads us to a new problem, which is our data consistency. If we talk about transactional process, then your whole process should be <strong>atomic</strong> —it means that if your process involving two or more discrete pieces of information, either all are committed or none are. In our case, if <strong>aggregator_service </strong>failed to transfer to our partner then customer balance should not be deducted. If our customer&#39;s balance is less than the payment amount then the transfer shouldn&#39;t have happen, and our customer not expect to see the transaction history.</p><p>What we really need is a proper rollback / fail handling mechanism. Can we achieve this?</p><h3>Choreography-based Saga</h3><figure><img alt="Choreography Dance Illustration" src="https://cdn-images-1.medium.com/max/1024/0*FoLIAR8bOAC3Bmhm" /><figcaption>Photo by <a href="https://unsplash.com/@rainierridao?utm_source=medium&amp;utm_medium=referral">Rainier Ridao</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>With our previous approach, if something went wrong in the middle of bill payment process, we need to compensate the whole process. Therefore, with this mindset we are ready to adopt <a href="https://microservices.io/patterns/data/saga.html">Saga Pattern</a>.</p><p>Saga is basically <strong>event based communication protocol</strong> pattern, and the idea came from <a href="https://microservices.io/patterns/data/event-driven-architecture.html"><strong>Event-Driven Architecture</strong></a> which already replaced by Saga pattern. The event is publishes sequentially into message broker based on our business case, every services that involved a series of specific event have a local transaction for sure.</p><p>Same like dancing, the <a href="https://en.wikipedia.org/wiki/Choreography">choreography</a> concept bring the idea that each local transaction will publishes specific event to trigger next local transaction in another service, with Saga pattern <strong>each event will be the single source of truth</strong> for our application. So, as a choreographer we will teach our services how to dance properly using choreography concept :D</p><p>Therefore, every single event publisher need to consume back the event result, whether it succeed or failed, so that the event publisher can take a decision for further action from the result. Take a look at Figure 4.</p><figure><img alt="Figure 4. Saga happy scenario." src="https://cdn-images-1.medium.com/max/1024/1*dq2XG6dhLJrUpuPCVDz5FQ.png" /><figcaption>Figure 4. Saga happy scenario.</figcaption></figure><p>Refer to Figure 4, here is our new scenario:</p><ol><li><strong>transaction_service </strong>create new transaction history on <strong>Transaction DB</strong> with <strong>“PENDING” </strong>status.</li><li><strong>transaction_service </strong>publishes new event to topic “<strong>bill_payment_request”</strong>.</li><li><strong>account_service </strong>will consume event from <strong>“bill_payment_request” </strong>topic and check balance availability from customer.</li><li><strong>account_service </strong>will publishes new event to topic <strong>“transfer_request”</strong>.</li><li><strong>aggregator_service </strong>will consume event from <strong>“transfer_request” </strong>topic.</li><li><strong>aggregator_service</strong> will make an API call to external / 3rd party API.</li><li>Upon success, <strong>aggregator_service </strong>will publishes event to<strong> </strong>topic<strong> “transfer_success”</strong>.</li><li><strong>account_service </strong>will consume event from <strong>“transfer_success” </strong>topic.</li><li><strong>account_service </strong>will confirm to deduct customer balance on <strong>Account DB</strong> along with balance checking.</li><li>Upon success, <strong>account_service</strong> will publishes event to topic <strong>“bill_payment_success”</strong>.</li><li><strong>transaction_service</strong> will consume event from<strong> “bill_payment_success” </strong>topic.</li><li><strong>transaction_service </strong>will update <strong>“PENDING” </strong>status on Transaction DB to <strong>“SUCCESS”</strong>.</li></ol><p>Then how we implement our fallback action when something went wrong on each process? So, let’s list down several failure possibilities and how we handle the fallback mechanism on bill payment process</p><p><strong>#1 Fail Scenario: Your customer’s bill payment amount is exceeding the customer’s balance itself.</strong></p><figure><img alt="Figure 5. #1 Fail Scenario — Bill payment amount exceeds customer’s balance." src="https://cdn-images-1.medium.com/max/1024/1*BJjiJvd0XJzaoZyKrkwP1g.png" /><figcaption>Figure 5. #1 Fail Scenario — Bill payment amount exceeds customer’s balance.</figcaption></figure><ol><li><strong>transaction_service</strong> create new transaction history on <strong>Transaction DB</strong> with “PENDING” status.</li><li><strong>transaction_service</strong> publishes new event to topic <strong>“bill_payment_request”</strong>.</li><li><strong>account_service</strong> will consume event from <strong>“bill_payment_request”</strong> topic.</li><li><strong>account_service</strong> will check customer’s balance availability on <strong>Account DB</strong>, and bill amount exceeds customer’s balance. <strong>account_service</strong> won’t deduct customer’s balance.</li><li><strong>account_service</strong> publishes new fail event to <strong>“bill_payment_failed”</strong> topic.</li><li><strong>transaction_service</strong> will consume fail event from <strong>“bill_payment_failed”</strong> topic.</li><li><strong>transaction_service</strong> will update <strong>“PENDING”</strong> status on <strong>Transaction DB</strong> to <strong>“FAILED”</strong>.</li></ol><p><strong>#2 Fail Scenario: There is an error with external / 3rd party API when our aggregator_service make a transfer request.</strong></p><figure><img alt="Figure 6. #2 Fail Scenario -Error on external / 3rd party API." src="https://cdn-images-1.medium.com/max/1024/1*B8CQ0b0-a1EpqmblPQxcAg.png" /><figcaption>Figure 6. #2 Fail Scenario —Error on external / 3rd party API.</figcaption></figure><ol><li><strong>transaction_service </strong>create new transaction history on <strong>Transaction DB</strong> with <strong>“PENDING” </strong>status.</li><li><strong>transaction_service </strong>publishes new event to topic “<strong>bill_payment_request”</strong>.</li><li><strong>account_service </strong>will consume event from <strong>“bill_payment_request” </strong>topic and check balance availability from customer.</li><li><strong>account_service </strong>will publishes new event to topic <strong>“transfer_request”</strong>.</li><li><strong>aggregator_service </strong>will consume event from <strong>“transfer_request” </strong>topic.</li><li><strong>aggregator_service</strong> will make an API call to external / 3rd party API.</li><li><strong>aggregator_service </strong>receive error from external / 3rd party API.</li><li><strong>aggregator_service </strong>publishes new fail event to topic <strong>“transfer_failed”</strong>.</li><li><strong>transaction_service</strong> will consume fail event from <strong>“transfer_failed”</strong> topic.</li><li><strong>transaction_service</strong> will update <strong>“PENDING”</strong> status on <strong>Transaction DB</strong> to <strong>“FAILED”</strong>.</li></ol><blockquote>Note: for every publishes event it’s mandatory to mark your event using Event ID, Event ID only created once when the event is publishes for the first time, so every services know which event they are processing. Also, this Event ID will help you to track the event sequence or mitigate some issue when something went wrong.</blockquote><h3>Conclusion</h3><p>Saga pattern only give you the idea of how you design the communication protocol between services. You need to design your own fail handling mechanism with your own business rule and again, it depends on your case.</p><p>The case I took is a simple business case, some of you maybe have more complex scenario in mind like how is the proper retry mechanism, how&#39;s each services workload? For now maybe it&#39;s just 2-5 events, someday it&#39;ll be hundred or maybe thousand of events. In another case maybe your customer don’t ever want failed transaction and they only want success scenario. That’s another thing to take care of and we can discuss in comment section or maybe another article, but we already know how the Saga works.</p><p>If you don’t want to implement Saga from scratch by yourself you can see <a href="https://axoniq.io/">https://axoniq.io/,</a> which you can play also with Spring Boot <a href="https://www.baeldung.com/axon-cqrs-event-sourcing">https://www.baeldung.com/axon-cqrs-event-sourcing</a> (if you are using Java).</p><p>Also, kindly check <a href="https://github.com/wiratamap/saga-pattern">my repository</a> for another usage example of choreography-based Saga pattern.</p><blockquote>Disclaimer: The point of view, service name, database, and scenario written in this article purely using <strong>simple case example</strong> and does not represent my employer.</blockquote><blockquote>“The most complex challenge in realizing microservice architecture is not building the services themselves, but the communication between services.” — Kasun Indrasiri</blockquote><h3>References:</h3><ul><li><a href="https://microservices.io/patterns/data/saga.html">Microservices Pattern: Pattern: Saga</a></li><li><a href="https://microservices.io/patterns/data/database-per-service.html#:~:text=A%20service&#39;s%20transactions%20only%20involve,the%20structure%20of%20this%20pattern.&amp;text=Schema-per-service%20%E2%80%93%20each,has%20it&#39;s%20own%20database%20server.">Microservices Pattern: Database per service</a></li><li><a href="https://www.cloudamqp.com/blog/2014-12-03-what-is-message-queuing.html">What is message queuing? - CloudAMQP</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe05d758942f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>