<?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 Crishantha Nanayakkara on Medium]]></title>
        <description><![CDATA[Stories by Crishantha Nanayakkara on Medium]]></description>
        <link>https://medium.com/@crishantha?source=rss-716e01d34c33------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-wTKneSqkgll9CvONKLAow.jpeg</url>
            <title>Stories by Crishantha Nanayakkara on Medium</title>
            <link>https://medium.com/@crishantha?source=rss-716e01d34c33------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 23:05:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@crishantha/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[Visualizing TOGAF]]></title>
            <link>https://crishantha.medium.com/building-the-architecture-roadmap-in-togaf-f6259ed2180f?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6259ed2180f</guid>
            <category><![CDATA[togaf]]></category>
            <category><![CDATA[togaf-certification]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Tue, 22 Apr 2025 12:16:44 GMT</pubDate>
            <atom:updated>2025-06-27T17:44:28.937Z</atom:updated>
            <content:encoded><![CDATA[<p>As we all know, TOGAF has been the de-facto Enterprise Architecture Framework (EAF) currently we mostly adopt. However, when you are diving into details of the Framework, you will probably be overloaded with models, artifacts, deliverables, etc. In each phase of the ADM cycle, there are multiple steps, inputs, outputs, deliverables involved. If you are new to the framework, you will probably might not like the amount of content that you need to produce or learn.</p><p>Therefore, it is very important to create your own mind maps or any other means to conceptualize most of the concepts that this great framework offers. Once you get used to it, it may not be that difficult to remember.</p><p>As most of us do, I also went through the TOGAF information overloading. But I am happy to say that I am slowly getting the hang of it now. The best part of my learning was that I was able to apply to some of the projects that I have worked recently. That really helped me to practically grasp some of the key concepts that the framework tries to offer.</p><p>So, in this blog post, I am trying to visualize how the Architecture Roadmap is developed and how it is realized in a typical TOGAF ADM cycle. In this process some of the key deliverables are also highlighted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*iZyOYcFVhHdZmfGs69yk6g.png" /><figcaption>Key Phases of TOGAF (Source: Author)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*O0aJBspgzzbHWnlx3sv2-A.png" /><figcaption>TOGAF Phase Deliverables (Source: Author)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*5MipnXQPBUKFezGNJUbCZQ.png" /><figcaption>Requirements Management (Source: Author)</figcaption></figure><p>Thank You! Happy learning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6259ed2180f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sri Lanka Government Agriculture Enterprise Architecture and Interoperability Framework]]></title>
            <link>https://crishantha.medium.com/sri-lanka-government-agriculture-enterprise-architecture-sl-gaea-93b957c2a5bf?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/93b957c2a5bf</guid>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Tue, 05 Nov 2024 11:50:00 GMT</pubDate>
            <atom:updated>2026-01-22T16:30:31.833Z</atom:updated>
            <content:encoded><![CDATA[<p>In 2023–2024 period, I got the opportunity to develop the <strong>Enterprise Architecture (EA)</strong> and the <strong>Interoperability Framework (IF)</strong> for the agriculture sector in Sri Lanka. This was a project carried out by FAO-UN (Food and Agriculture Organization of the UN) and with the support of Ministry of Agriculture, Livestock, Land, Irrigation, Ministry of Plantation, and Ministry of Fisheries.</p><p>(P. Note. Ministries in Sri Lanka are subject to change every year of so :)).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*tOIVcMSIP1xAN1vIzxzCWQ.png" /><figcaption>Ministires in the Agriculture Sector</figcaption></figure><p>The study adopted the Open Group TOGAF as the base EA framework. For the IF development, it followed the European Interoperability Framework (EIF).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*pBPY9XkjJyDusudgiWRH6Q.png" /><figcaption>TOGAF and EIF</figcaption></figure><p>Leveraging the TOGAF BDAT Framework (BDAT = Business, Data, Application and Technology), the study managed to complete current architecture by covering 80% of government organizations. This gave the study the much-needed understanding of the current setting and the gaps involved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*LjOivyYpF9zs39bQmoLzig.png" /><figcaption>TOGAF BDAT Framework</figcaption></figure><p>The gaps were identified by comparing the target architecture proposed. In the target architecture, the primary target was to identify data owners based on the current scope of each organization (Single Source of Truth) . This process was able to identify most of the duplicated business processes, information systems and databases while identifying key registries.</p><p>I am sure, if the proposed architecture is implemented in the future, it will have a greater impact in terms of data sharing within the sector and beyond. Most importantly, it will be a good case study or a reference to other sectors in the Sri Lankan Government, which desperately need some sort of an integration. If not, Connected Government will be just a dream for us like it has been for decades.</p><p>You can reach both EA and IF for the Sri Lanka Agriculture Sector by following the below links.</p><ol><li>Sri Lanka Government Agriculture Enterprise Architecture (SL-GAEA) — <a href="https://agrimin.gov.lk/wp-content/uploads/2026/01/SL-GAEA_V1.3.pdf">SL-GAEA_V1.3.pdf</a></li><li>Sri Lanka Government Agriculture Interoperability Framework (SL-GAIF) — <a href="https://agrimin.gov.lk/wp-content/uploads/2026/01/SL-GAIF_V1.3.pdf">Agri_IF_V1.3.pdf</a></li></ol><p>Thank You!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93b957c2a5bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Mesh: The decentralized data platform]]></title>
            <link>https://crishantha.medium.com/data-mesh-a-decentralized-data-platform-1cc41e0d3d03?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cc41e0d3d03</guid>
            <category><![CDATA[data-mesh-architecture]]></category>
            <category><![CDATA[data-mesh]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Sat, 07 Oct 2023 03:55:00 GMT</pubDate>
            <atom:updated>2024-08-02T14:06:17.861Z</atom:updated>
            <content:encoded><![CDATA[<p><em>An Introduction</em></p><h4>Overview</h4><p>As Enterprise Architects we have witnessed how enterprise software architectures have been evolved over the years.</p><p>The typical “operational plane” of a typical enterprise software architecture, evolved from a centralized monolithic design to a distributed microservices design. Now we are seeing the same paradigm shift slowly being adopted in the “data analytical plane” as well.</p><p>So, let&#39;s have a look at what it really means.</p><h4>Enterprise Data Platforms</h4><p>During the past few decades, we have been witnessing three (03) basic generations of enterprise data platforms.</p><ol><li>Generation 01 —&gt; Data Warehouses</li><li>Generation 02 —&gt; Data Lakes / Lake houses</li><li>Generation 03 — -&gt; Modern Cloud Data Architectures (The Hybrid Approach)</li></ol><p>In the first era, companies invested heavily by moving the organization wise data to Data Warehouses for data retrieval / analytical purposes. In the next generation, they slowly moved the data workloads to more robust Data Lakes / Data Lakehouses replacing legacy Data Warehouses. However, slowly most of the modern data architectures introduced a hybrid model by having the best of both architectures (The warehouse and the lake). However, these modern data architectures have a centralized data approach where you collect and process your data in a central manner.</p><p>Though this approach made your life easier in terms of its ability to access your data in a more centralized manner, slowly it started to show some cracks. For example, data consumers can get overly dependent on a central data engineering team to get access probably to your own organizational data. On top of that, the data engineers who manage the central data repository (The warehouse or the lake) may not have the required business know-how to manipulate data consumer generated data. By looking at this, you can see that there is a clear problem of the ownership of the data.</p><p>The “data ownership” is a mandatory requirement for many regulatory institutions / ecosystems such as Governments, Banks, etc. With the advent of evolving “Data Protection Acts” in many regulatory environments (especially in the public or banking sectors), we cannot deny the importance of data ownership and its governance. Hence, adopting a more decentralized approach to your own data is slowly becoming a must.</p><h4>Data Mesh</h4><p>The “data mesh” is more of a “process” than a “technology stack or and implementation style”. It is basically a data architectural approach that delegates the responsibility of enterprise data to those areas of business that have the expertise in them.</p><p>In the “data mesh” approach, the data engineering team can easily break the entire data architecture into smaller, domain-oriented components (Data Products) allowing them to be more agile and scalable. This is exactly maps with what we do with microservices as well.</p><p>With the “data mesh” approach, a group of experts within a selected “domain” manage its datasets within the “domain”. They are responsible to build “data products” within that “domain”, which can be later consumed as APIs within the organization or outside the organization, depending on the sensitivity of the data.</p><p>The respective “domain” basically owns and controls the “data products” under its purview. These “data products” could be disseminated via APIs (i.e. REST) just like you do in a typical Microservices setup.</p><h4>Data Mesh Principles</h4><p>Zhamak Dehghani [2], who conceptualized the Data Mesh design, explains its architecture with <strong>four key principles</strong>.</p><ol><li>Domain-driven decentralization</li><li>Data as a product</li><li>Self-service data platform</li><li>Federated governance</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*AP7ndRNzVQsW5f8_rCZsgA.png" /><figcaption>Data Mesh Principles</figcaption></figure><p><strong>Domain-driven Decentralization </strong>— Data is owned by those who know it best. These domain teams basically own their data and will grant access by request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*1ZCSdrfJKS1jTqWaQcuN_g.png" /><figcaption>The Data Mesh — An Example</figcaption></figure><p><strong>Data as a product</strong> — Domain teams are responsible for the quality of data, unlike central data lakes had data pipelines to cleanse the data which are ingested. These domain level data are shared as a product to another domain if required. These data are discoverable, addressable, trustable, and secure. A “Data Product” is known as a “Node” in a Data Mesh Domain. For example, the “Farmer” domain has three Data Products / Nodes. (See the Agriculture Data Mesh example below). Each of this product can work as a “microservice for the data world”. Each Data Product is an encapsulated component, which comprises as a combination of (data + code + infrastructure).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*jCKIokAFTsZRS6ueMzB-0Q.png" /><figcaption>Data as a Product</figcaption></figure><p><strong>Self-serve Data Platform </strong>— Data is available via centralized self-service platform.</p><p><strong>Federated Data Governance</strong> — This principle is there to make sure all the autonomous data products under each domain are working together. This is established by having global standards such as data interoperability standards, that can apply to all data products in each domain in the data mesh. These are basically implemented and enforced <em>centrally</em> by the data mesh platform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*CPmzooTsznWZuDUpx5cOtw.png" /><figcaption>Federated Data Governance</figcaption></figure><h4>Is it a silver bullet?</h4><p>Data Mesh is never a silver bullet like any other software architecture pattern. It is recommended for a more complex and larger data architectures, which needs some agility, data ownership, etc. If your data architecture is a smaller one, with a smaller number of domains, with a lesser regulatory background, then having a Data Mesh may be an over-kill considering the effort and its complexity. This is same as what we experience in a typical Microservices adoption.</p><p>Hope you were able to get a basic understanding about what “Data Mesh” is trying to achieve. There is more to it. Let&#39;s drill them down in later articles.</p><p>Thank You!</p><h4>References</h4><ol><li>How to move beyond a Monolithic Data Lake to a Distributed Data Mesh — <a href="https://martinfowler.com/articles/data-monolith-to-mesh.html">https://martinfowler.com/articles/data-monolith-to-mesh.html</a></li><li>Data Mesh: Delivering Data-Driven value at Scale [Book] — By Zhamak Dehghani</li><li><a href="https://www.thoughtworks.com/en-us/insights/books/data-mesh">https://www.thoughtworks.com/en-us/insights/books/data-mesh</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cc41e0d3d03" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices Patterns: API Gateway and BFF Patterns]]></title>
            <link>https://crishantha.medium.com/microservices-patterns-api-gateway-and-bff-patterns-e1d4634a953?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1d4634a953</guid>
            <category><![CDATA[api-gateway-pattern]]></category>
            <category><![CDATA[backends-for-frontends]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Mon, 24 Jul 2023 19:31:24 GMT</pubDate>
            <atom:updated>2023-07-28T15:22:33.249Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Miroservices Patterns Series — Part 08</em></p><h4>API Gateway Pattern</h4><blockquote>API Gateway Pattern: Implementing a service that is the entry point into a microservices based application from external API clients. [1]</blockquote><p>Due to a lot of drawbacks with clients trying to access microservices directly via the Internet, it is recommended to apply the API Gateway pattern as a solution and a design pattern. It basically works as the entry point into a microservices application from the outside your system boundary.</p><p>This pattern is quite similar to the <em>Facade Pattern</em> in object orientation, which encapsulates the microservices application internal architecture and provides and API to its clients.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S3t0B3vMZ2UEle6N9FTWRw.png" /><figcaption>Figure 01 — API Gateway Advantage</figcaption></figure><p>Some of the key responsibilities of the API Gateway Pattern are as follows:</p><ol><li>Request Routing</li><li>API Composition</li><li>Edge Functions</li><li>Protocol Transformations</li></ol><p>Out of four (04) responsibilities mentioned, both <em>request routing</em> and <em>API compositions</em> are the main key features of any API Gateway.</p><p>The third option, which is the Edge function can be helpful to utilize one of the following.</p><p>1 Authentication — Verifying the client identity</p><p>2. Authorization — Verifying the client is authorized to perform a particular API operation</p><p>3. Rate limiting — Limiting the incoming requests from clients</p><p>4. Caching — Caching the responses to reduce the number of incoming requests to the back-end service.</p><p>5 Matrix Collection — Collecting metrics on API usage</p><p>6. Request logging — Logging client requests</p><p>All these edge operations can be implemented at three different levels in your deployment architecture.</p><ol><li>Back-end services level — [Less secure]</li><li>Edge service level — [This is to have a separate service layer for an Edge service. This is better than the first option, but requires an additional hop]</li><li>API Gateway level — [This requires less hops than the the second option and it is more secure and convenient to govern]</li></ol><p>However, generally the API Gateway governance is taken care of by a separate API Gateway team and not by other individual API teams. This situation will definitely lead to governance conflicts.</p><p>Therefore, it is always good to separate the API Gateway governance by having separate API Gateway for each API / microservices team. This approach is known as Backends for Frontends (BFF) pattern.</p><h4>Backends for Frontends (BFF) Pattern</h4><blockquote>BFF Pattern: Implement a separate API Gateway for each type of client.[1]</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0bw_TqKZ6DADIzM_w3OD6A.png" /><figcaption>Figure 02 — Without BFF</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9uDwIO-x7gPhe3cx5dz3cQ.png" /><figcaption>Figure 03 — With BFF</figcaption></figure><p>As you see on figure 02, there is a common API Gateway for all types of clients and on figure 03, there are separate API Gateways (micro) for all types of clients.</p><p>By having micro API Gateways for each client type, basically isolate the business logic at the API Gateway level for each client type based on certain API logic variations. This allows the reliability for each client type, just as you experience in a typical microservices back-ends.</p><p>As a live example, Netflix uses multiple API Gateways for its each client type to improve its scalability and performance.</p><h4>Implementing an API Gateway</h4><p>You can take multiple approaches for this.</p><ol><li>Use a built-in API Gateway cloud based products in the market (<a href="https://aws.amazon.com/api-gateway/">AWS API Gateway</a>, etc)</li><li>Use an Open Source built products (i.e. <a href="https://apim.docs.wso2.com/en/latest/deploy-and-publish/deploy-on-gateway/api-gateway/overview-of-the-api-gateway/">WSO2 API Gateway</a>, <a href="https://konghq.com/products/kong-gateway">Kong</a>, <a href="https://traefik.io/solutions/api-gateway/">Traefik</a>, etc)</li><li>Building your own API Gateway using some existing open source frameworks such as <a href="https://github.com/Netflix/zuul">Netflix Zuul</a> or <a href="https://cloud.spring.io/spring-cloud-gateway/reference/html/">Spring Cloud Gateway</a>. Out of these two, Spring Cloud Gateway is an API Gateway framework built on top of several frameworks such as Spring 5, Spring Boot 2, Spring Webflux and Project Reactor.</li></ol><h4>References</h4><ol><li>API Gateway Pattern: <a href="https://microservices.io/patterns/apigateway.html">https://microservices.io/patterns/apigateway.html</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1d4634a953" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices Patterns: API Composition Pattern]]></title>
            <link>https://crishantha.medium.com/microservices-patterns-api-composition-pattern-27040cae5bd3?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/27040cae5bd3</guid>
            <category><![CDATA[microservices-pattern]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[distributed-design]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Sat, 08 Jul 2023 18:59:45 GMT</pubDate>
            <atom:updated>2023-07-17T16:03:25.604Z</atom:updated>
            <content:encoded><![CDATA[<h3>Microservices Patterns: API Composition and CQRS Patterns</h3><p><em>Microservices Patterns Series — Part 07</em></p><p>In the typical microservices design, the <a href="https://microservices.io/patterns/data/database-per-service.html"><em>Database per service pattern</em> </a>indicates that each microservice should be responsible for its own data. However, if the consumer wants to access data, which spans across multiple microservices, we should be able to find a solution for that. There are two data query patterns, which we can introduce here.</p><ol><li>The API Composition Pattern [1]</li><li>The Command Query Responsibility Pattern (CQRS) [2]</li></ol><p>Out of these two patterns, the <em>API Composition Pattern</em> is more simpler to use and adopt. Comparatively, The <em>CQRS Pattern</em> is more powerful and complex. In this blog, lets compare these two patterns in detail.</p><h4>The API Composition Pattern</h4><blockquote>The API Composition Pattern: Implement a query that retrieves data from several services by querying each service via its API and combining the results. [1]</blockquote><p>Structurally the API Composition Pattern deals with <strong>two key participants</strong>. (1. The API Composer 2. The Provider Services / Microservices).</p><p>The API Composer implements the query operation by querying the provider services.</p><p>The API Composer invokes all involved provider services and combines the results together with an in-memory join before presenting to the consumer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*0DzTc9L1CWBJmWV07VWRaA.png" /><figcaption>Figure 01 — API Composition Pattern</figcaption></figure><p>In the API Composition pattern, these provider service invocations could happen in parallel or in sequence. For example, in figure 01, all three provider services (<em>order, payment, deliver</em>) should be called in sequence or in parallel (in a quick time) in order to get an output.</p><h4>API Composition Pattern — Drawbacks</h4><ol><li><strong>The overhead of invoking multiple service providers</strong> using multiple database queries can be time consuming. In such a situation, more computing and networking resources are required and that would add some additional cost as well to the application in general.</li><li><strong>Having multiple service endpoints can be a hindrance to the overall availability </strong>of the application and <strong>increases the coupling with related service endpoints</strong>. There can be situations, where at least one failed service endpoint, which would result a less reliable system.</li><li><strong>The data inconsistencies</strong> could be there in the API composer result due to the fact that, API composer does retrieve data from multiple external service providers. Each service provider can have data with inconsistencies attached to them. For example as in figure 01 example, if a consumer decides to cancel an already placed order, which the consumer had completed a few minutes ago, the <em>Order</em> microservices is now updated as a CANCELLED order. However, with eventual consistency around there could be a possibility that the <em>Purchasing </em>microservice is not yet updated with a payment cancellation, which could lead to an inconsistency of the final data query output via the API composer. In addition to that, sometimes certain service provider queries need data from other service providers to respond. In such situations, having data replicas in many service provider level databases are required. Maintaining multiple data replicas of other service providers could lead to data inconsistencies as well.</li><li>The inner joins, which are being used by the API composer can be quite confusing at times and difficult to apply.</li></ol><h4>CQRS (Command and Query Responsibility Segregation) Pattern</h4><blockquote>The CQRS Pattern: Implement a query that needs data from several services by using events to maintain a read only view that replicates data from the services</blockquote><p>In order to overcome some of the drawbacks, which have been identified under the API Composition Pattern, the CQRS Pattern can be applied to many microservices based applications.</p><h4>So, what can CQRS can do for us?</h4><p>In simple terms, it is all about segregation or the separation of commands and queries. As you see in figure 02, CUD (Create, Update and Delete) operations are routed through the <strong>domain / command model</strong> and the R(Read) operations are routed through the <strong>query model</strong>.</p><p>Furthermore, the command model publishes domain events whenever its data changes to the query model as an eventual consistency transaction via an Event Bus.</p><p>With this approach, all the data queries pertaining to the given service is accommodated via the Query DB and will “not” be depending on the Command level DB anymore. This will certainly improve the performance and the scalability of the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zk90MBvjf781jAQB5Mqj7A.png" /><figcaption>Figure 02 — CQRS Pattern</figcaption></figure><h4>Conclusion</h4><p>Hope the above simple comparison will give you the importance of the CQRS method and its application primarily on a microservices architecture compared to the API composition pattern, which we mostly tend to implement.</p><p>Thank You!</p><h4>References</h4><ol><li>API Composition Pattern: <a href="https://microservices.io/patterns/data/api-composition.html">https://microservices.io/patterns/data/api-composition.html</a></li><li>CQRS Pattern: <a href="https://microservices.io/patterns/data/cqrs.html">https://microservices.io/patterns/data/cqrs.html</a></li><li>Database per Service Pattern: <a href="https://microservices.io/patterns/data/database-per-service.html">https://microservices.io/patterns/data/database-per-service.html</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27040cae5bd3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices Patterns: Event Sourcing]]></title>
            <link>https://medium.com/cloud-native-daily/microservices-patterns-event-sourcing-7c6e765681c1?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c6e765681c1</guid>
            <category><![CDATA[event-sourcing]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[eventstore]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Mon, 03 Jul 2023 16:41:27 GMT</pubDate>
            <atom:updated>2023-07-09T17:45:02.743Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is Event Sourcing? It is a different way of structuring the business logic and persisting aggregates.</h4><p><em>Microservices Patterns Series — Part 06</em></p><p>In a typical traditional persistence model, we store the “current state” of an aggregate on a relational database table. If we are to track any events that happened around the aggregate, we will basically use <em>transaction log files </em>to store them. However, these transactional logs may not give you the complete picture of a transaction state. It will probably give you some high-level information related to it. So let&#39;s find out what Event Sourcing can offer you in this aspect.</p><h4>Event Sourcing</h4><blockquote>Event Sourcing is a different way of structuring the business logic and persisting aggregates. It persists an aggregate as a sequence of events and each event represents a state change of the aggregate.</blockquote><p>By doing this, it helps to preserve the history of aggregates, which is valuable for auditing and regulatory purposes. With this history, it will provide some valuable information related to all the events that occurred for each entity. This is a way better approach compared to the traditional transactional log method.</p><h4>Event Store</h4><p>In Event Sourcing, event aggregates are stored as a sequence of events in a database, which is known as an <em>Event Store</em>.</p><p>In this approach, applications can insert or retrieve aggregate events by their primary key. At any given point, the aggregate “current state” could be derived from the event store value for the particular aggregate. With this, you will have a less chance of loosing any valuable information related to the aggregate that you are concerned with.</p><h4>Event Publishing</h4><p>Once events are stored in an event store, these events could be published to an<em> Event Broker</em> for its subscribers to consume them further. You can use an Event Broker (i.e. Apache Kafka) to handle this situation (See Figure 01).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SQikYTvGayDrXBd-v4RVSg.png" /><figcaption>Figure 01 — Event Sourcing</figcaption></figure><p>The above (Figure 01), is quite similar to the choreography-based saga transactional approach, which we discuss under <a href="https://crishantha.medium.com/microservices-patterns-part-04-saga-pattern-a7f85d8d4aa3">Saga Pattern</a>. In this, each microservice is subscribed to events published by other microservices, where there is no central orchestration.</p><p>Some of the key benefits of Event Sourcing are:</p><ol><li>Can reliably publish events whenever the state of an aggregate changes</li><li>It can provide an audit log that is guaranteed to be accurate.</li><li>Stores the entire history of each aggregate allowing to be queried them at a later stage</li></ol><p>However, there are some drawbacks as well:</p><ol><li>Learning curve could be more than the traditional state handling. Moving from a traditional monolithic application to a more event-based microservices application could be a difficult proposition for a programmer or a designer</li><li>Handling multiple versions of an event-based application could be tricky. If an aggregate schema changes over time, you will probably need to have another event store version to store the new changes in the schema.</li><li>Deleting data could be tricky since event sourcing is there to preserve the history of aggregates.</li><li>Querying an event store could be challenging. The CQRS (Command and Query Responsibility Segregation) method is specifically used for this.</li></ol><h3>Conclusion</h3><p>So it was a brief introduction to Event Sourcing in general. Hope you got some good insight into the Microservice Event Sourcing Pattern by reading this blog. You can find my other blogs related to Microservices Patterns and Concepts using the following links.</p><ol><li><a href="https://crishantha.medium.com/microservices-patterns-part-01-inter-process-communication-ipc-76010b07452e">Microservices Patterns: Inter Process Communication</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-part-02-the-circuit-breaker-pattern-ddba14adbbd4">Microservices Patterns: The Circuit Breaker Pattern</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-part-03-service-discovery-patterns-97d603b9a510">Microservices Patterns: The Service Discovery Patterns</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-part-04-saga-pattern-a7f85d8d4aa3">Microservices Patterns: The Saga Pattern</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-business-service-patterns-48c12ecbe686">Microservices Patterns: Business Services Patterns</a></li></ol><p>Thank You!</p><h3>References</h3><ol><li>Microservices Patterns: with Examples in Java [Book] — By Chris Richardson, Manning Publications, 2018</li><li>Event Sourcing Pattern: <a href="https://microservices.io/patterns/data/event-sourcing.html">https://microservices.io/patterns/data/event-sourcing.html</a></li><li>Event Sourcing (Martin Fowler) : <a href="https://www.youtube.com/watch?v=ck7t592bvBg">https://www.youtube.com/watch?v=ck7t592bvBg</a></li><li>Many Meanings of Event Driven Architecture (Martin Fowler): <a href="https://www.youtube.com/watch?v=STKCRSUsyP0&amp;t=202s">https://www.youtube.com/watch?v=STKCRSUsyP0&amp;t=202s</a></li><li>Building a CQRS event store with Amazon DynamoDB — <a href="https://aws.amazon.com/blogs/database/build-a-cqrs-event-store-with-amazon-dynamodb/">https://aws.amazon.com/blogs/database/build-a-cqrs-event-store-with-amazon-dynamodb/</a></li></ol><h4>Further Reading:</h4><ul><li><a href="https://gethelios.dev/blog/cut-engineering-costs-save-time-helios?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Microservices Monitoring: Cutting Engineering Costs and Saving Time</a></li><li><a href="https://gethelios.dev/blog/5-microservices-challenges-and-blindspots-for-developers?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">5 Microservices Challenges and Blindspots for Developers</a></li><li><a href="https://gethelios.dev/blog/testing-microservices-with-helios/?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Testing Microservices - Trace Based Integration Testing Example</a></li><li><a href="https://gethelios.dev/opentelemetry-a-full-guide/?utm_source=medium&amp;utm_medium=cloud+native+daily">OpenTelemetry: A full guide</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c6e765681c1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloud-native-daily/microservices-patterns-event-sourcing-7c6e765681c1">Microservices Patterns: Event Sourcing</a> was originally published in <a href="https://medium.com/cloud-native-daily">Cloud Native Daily</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices Patterns: Business Service Patterns]]></title>
            <link>https://medium.com/cloud-native-daily/microservices-patterns-business-service-patterns-48c12ecbe686?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/48c12ecbe686</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[domain-driven-design]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Tue, 27 Jun 2023 08:05:02 GMT</pubDate>
            <atom:updated>2023-07-16T13:00:21.181Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Microservices Patterns Series — Part 05: Hexagonal Architecture, Domain Model Pattern, </em>Domain Driven Design (DDD), Aggregate Pattern and more.</h4><p>Building business logic/service for an enterprise application whether it is a monolithic or microservice has more similarities than differences. In both approaches, it will comprise classes such as services, entities, repositories, etc. However, you will find some distinct differences as well. In this blog, I will try to emphasize those differences in a conceptual manner by revisiting some of the core design patterns and principles built into each architecture.</p><p>So, let&#39;s start with Hexagonal Architecture and how it relates to the enterprise application business logic.</p><h4>Hexagonal Architecture</h4><p>Any business service in an enterprise application theoretically leverages the <a href="https://en.wikipedia.org/wiki/Hexagonal_architecture_(software)">Hexagonal Architecture</a> at its core.</p><blockquote>The Hexagonal Architecture / Ports and Adapter Architecture is an architectural pattern used in software architecture design. It aims at creating <a href="https://en.wikipedia.org/wiki/Loose_coupling">loosely coupled</a> application components that can be easily connected to their software environment by means of ports and <a href="https://en.wikipedia.org/wiki/Adapter_pattern">adapters</a>. (Wikipedia)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LhuutCQMhlf47FlnOEGvLw.png" /><figcaption>Figure 01 — Hexagonal Architecture</figcaption></figure><p>As you can see in Figure 01, the <em>business service logic</em> is the core of the Hexagonal architecture.</p><h4>Domain Model Pattern</h4><p>The traditional procedural <a href="https://martinfowler.com/eaaCatalog/transactionScript.html">Transactional Script Pattern</a> is often a good way to implement “simple business logic”.</p><blockquote>Transaction Script Pattern: Organize the business logic as a collection of procedural transaction scripts, one for each type of request.</blockquote><p>But when implementing a “complex business logic”, you should consider using the <a href="https://martinfowler.com/eaaCatalog/domainModel.html"><em>Domain Model Pattern</em></a><em>, </em>which is basically using the Object Oriented Design (OOD).</p><blockquote>Domain Model Pattern: Organize the business logic as an object model consisting of classes that have state and behavior [1].</blockquote><h4>Domain Driven Design (DDD)</h4><p>However, the Domain Model Pattern works well with a typical monolithic application back-end, it has certain limitations in Microservice applications, which is basically covered by the <a href="https://martinfowler.com/bliki/DomainDrivenDesign.html">Domain Driven Design </a>(DDD).</p><blockquote>DDD is a refinement of OOD and it is an approach for developing the complex back-end business logic.</blockquote><p>When using DDD, each service has its own domain model, which avoids the problem of having a single unified domain model for the whole application.</p><h4>Strategic Patterns vs Tactical Patterns</h4><p>DDD comes up with multiple <em>strategic</em> and <em>tactical</em> patterns.</p><p>Two of the key strategic patterns are <em>Subdomains</em> and <em>Bounded</em> <em>Contexts</em>. These patterns are basically useful to decompose the business logic in an application.</p><blockquote>According to Vaughn Vernon’s book of “Implementing Domain Driven Design” [5], The subdomains live in problem space and the bounded contexts in the solution space.</blockquote><p>In other words, while bounded contexts help you to manage the complexity within an application, Subdomains help to structure and organize the different aspects of the business domain.</p><p>In practice, bounded contexts often align with a subdomain, but it is also possible to have multiple bounded contexts within a single subdomain or to have a bounded context that spans multiple subdomains.</p><p>In each <em>bounded context,</em> we can build teams who are specialized in the respective domains to govern. These teams are there to build artifacts, requirements, specifications and services for the given domain.</p><p>The tactical patterns are basically building blocks of the domain model that you define in your service. Some of these tactical design patterns are <em>Entity, Value Object, Factory, Repository, Service </em>and<em> Aggregate</em>.</p><p>In this blog, let&#39;s dive a bit deeper into the Aggregate Pattern and its usefulness in a typical microservices design.</p><h4>Aggregate Pattern</h4><blockquote>Aggregator Pattern: Organizing a domain model as a collection of aggregates, each of which is a graph of objects that can be treated as a unit [1]</blockquote><p>A “traditional domain model”, is a collection of classes and relationships among them. In this model, all of classes and relationships are interconnected and it is relatively hard to find boundaries of each business object, which is the key requirement in a complex microservices design. The Aggregate Pattern in DDD can help you to solve this problem.</p><p>In the Aggregate Pattern, as per the definition, structuring a domain model as a set of aggregations makes their boundaries explicit and easier to understand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XPAfiGqGti_BaF7gTHI5GA.png" /><figcaption>Figure 02 — A Domain Model with Aggregates</figcaption></figure><p>Each Aggregate has a <strong>Root Entity</strong> (Aggregate Root) and possibly <strong>one or more value objects</strong>.</p><blockquote>However, that does not mean one Aggregate can have only one entity. You can have multiple entities within an aggregate. But the best practice is to have fine-grained services with a minimum number of entities within an Aggregate to improve the scalability of each transaction.</blockquote><p>The <strong>Aggregate Root</strong> is the main entity that holds references to the other aggregates in the domain model and it is the only entity in the aggregate that can be used for a direct lookup. Components such as value objects within the aggregate will have object references with each other. In figure 02, you will see this and each referred aggregate primary key ids are stored in the main aggregate, which is Aggregate 01. This allows to have a more loosely coupled architecture within the domain model.</p><p>An aggregate is often loaded entirely from the database (to avoid any lazy loading). Even while it is being deleted, the aggregate removes all of its objects from the database within its boundary. In addition to that, it is easier to store them in NoSQL databases such as MongoDB.</p><p>In a nutshell, applying the DDD Aggregate Pattern will,</p><ol><li>Modularize the domain model in the service</li><li>Eliminate the possibility of object references between services (In DDD, references between classes in different aggregates are in terms of primary key value rather than object references)</li><li>A transaction can only create or update a single aggregate. This allows the application to use the Saga Pattern to update multiple aggregates (I discussed Saga Pattern in detail in one of my previous <a href="https://crishantha.medium.com/microservices-patterns-part-04-saga-pattern-a7f85d8d4aa3">blog</a> articles)</li></ol><h4>Aggregate and Saga Patterns</h4><p>A Saga orchestrates a sequence of local transactions within a sequence of (micro)services in order to maintain data consistency. Each local transaction is having an Aggregate mapped to it. (See Figure 03).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ALRNCLhmfQUqBCUcAoYCfQ.png" /><figcaption>Figure 03 — Connecting the Aggregate Pattern and the Saga Pattern</figcaption></figure><h4>Aggregate vs Bounded Context</h4><p>In technical theory, there is a bit of a misconception about the difference between a <em>bounded context</em> and an <em>aggregate. </em>Therefore, it is imperative to understand the difference between them and their association with microservices.</p><p>As explained earlier, a microservice could be explained by a <em>bounded context</em> or a <em>domain</em>. Each <em>bounded context</em> will have one or more <em>aggregates.</em></p><blockquote>Therefore, in practice, a microservice should not be smaller than an Aggregate and should not be bigger than a bounded context.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7DFs8u6tCCRVCMqfiiLxdw.png" /><figcaption>Figure 04 — Bounded Context vs Aggregates</figcaption></figure><h4>Domain Event Pattern</h4><p>Conceptually domain events are published by aggregates whenever they get created and updated. An aggregate knows when its state changes and hence what event to publish.</p><p>These domain events are ultimately published as messages to a message broker such as <a href="https://kafka.apache.org/">Kafka</a>.</p><p><em>Domain Event Pattern: An aggregate publishes a domain event when it is created and undergoes some other significant change [1].</em></p><h4>Event Storming</h4><p>There are a few strategies for identifying domain events. One popular strategy is <em>Event Storming, </em>which can be performed via a workshop sort of an arrangement to understand a complex domain with many events. The end result of such a workshop is an event-centric domain model with aggregates and events.</p><h3>Conclusion</h3><p>That’s it! Hope you got some good insight into Microservice Business Service Patterns and other related concepts by reading this blog. You can find my other blogs related to Microservices Patterns and Concepts using the following links:</p><ol><li><a href="https://crishantha.medium.com/microservices-patterns-part-01-inter-process-communication-ipc-76010b07452e">Microservices Patterns: Inter Process Communication</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-part-02-the-circuit-breaker-pattern-ddba14adbbd4">Microservices Patterns: The Circuit Breaker Pattern</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-part-03-service-discovery-patterns-97d603b9a510">Microservices Patterns: The Service Discovery Patterns</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-part-04-saga-pattern-a7f85d8d4aa3">Microservices Patterns: The Saga Pattern</a></li></ol><p>Thank You!</p><h4>References</h4><ol><li>Microservices Patterns: with Examples in Java [Book] — By Chris Richardson, Manning Publications, 2018</li><li>Domain Model Pattern: <a href="https://martinfowler.com/eaaCatalog/domainModel.html">https://martinfowler.com/eaaCatalog/domainModel.html</a></li><li>Aggregate Pattern: <a href="https://microservices.io/patterns/data/aggregate.html">https://microservices.io/patterns/data/aggregate.html</a></li><li>Domain Event Pattern: <a href="https://microservices.io/patterns/data/domain-event.html">https://microservices.io/patterns/data/domain-event.html</a></li><li>Implementing Domain Driven Design [Book] — By Vaughn Vernon, Pearson, 2016</li></ol><h4>Further Reading:</h4><ul><li><a href="https://gethelios.dev/blog/5-microservices-challenges-and-blindspots-for-developers?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">5 Microservices Challenges and Blindspots for Developers</a></li><li><a href="https://gethelios.dev/blog/cut-engineering-costs-save-time-helios?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Microservices Monitoring: Cutting Engineering Costs and Saving Time</a></li><li><a href="https://gethelios.dev/blog/testing-microservices-with-helios/?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Testing Microservices - Trace Based Integration Testing Example</a></li><li><a href="https://gethelios.dev/blog/microservices-or-a-monolith-which-one-are-you/?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Microservices or a monolith - which one are you?</a></li><li><a href="https://medium.com/cloud-native-daily/7-best-tracing-tools-for-microservices-27a5e3bc4b9c">7 Best Tracing Tools for Microservices</a></li><li><a href="https://python.plainenglish.io/top-11-tools-for-microservices-backend-development-in-2023-3d9cdd61ef10">Top 11 Tools for Microservices Backend Development in 2023</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48c12ecbe686" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloud-native-daily/microservices-patterns-business-service-patterns-48c12ecbe686">Microservices Patterns: Business Service Patterns</a> was originally published in <a href="https://medium.com/cloud-native-daily">Cloud Native Daily</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices Patterns: The Saga Pattern]]></title>
            <link>https://medium.com/cloud-native-daily/microservices-patterns-part-04-saga-pattern-a7f85d8d4aa3?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7f85d8d4aa3</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Thu, 15 Jun 2023 09:13:43 GMT</pubDate>
            <atom:updated>2024-06-24T14:00:32.823Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Microservices Pattern Series — Part 04: </em>Some insights into ACID Transactions, 2PC Pattern and finally the Saga Pattern in detail.</h4><h4>Objective</h4><p>Transaction handling is an integral part of any enterprise architecture. This allows for building proper data consistency across enterprise applications. As an Enterprise Architect, knowing how to handle transactions in a distributed architecture is essential.</p><p>This blog will give the reader some insights into ACID Transactions, 2PC Pattern and finally the Saga Pattern in detail. The primary objective is to give an overview of how Saga Pattern operates and how it can solve real-world distributed transactional issues.</p><p>The blog was written with some background reading on multiple literature resources mentioned under the <em>References. </em>If you really want to go into detail about this subject I highly recommend reading the book <a href="https://www.amazon.com/Microservices-Patterns-examples-Chris-Richardson/dp/1617294543"><em>Microservices Patterns: with Examples in Java — By Chris Richardson</em></a> [3].</p><h4>ACID Transactions</h4><p>In a traditional monolithic application, the system will create a local database transaction that works over multiple database tables. If any error occurs in between a transaction, it will be rolled back to its initial state. These transactions are known as ACID transactions (Atomicity, Consistency, Isolation, Durability). ACID transactions greatly ease the developer&#39;s task by having exclusive access to a particular database. You could see this in many monolithic application architectures, which have a simple way of handling transactions with a single database. In Figure 01, you could see how both the Customer and the Order tables are updated via a single atomic ACID transaction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*Dr1cn4jDepMPSR4IzEFOxQ.png" /><figcaption>Figure 01 — An ACID Transaction in a Monolithic Application</figcaption></figure><h4>Distributed Transactions</h4><p>Unlike single monolithic applications, distributed applications are dealing with multiple services. In such architectures, handling transactions could be a challenge.</p><p>Two-Phase Commit (2PC) is one of the distributed transaction strategies that we could apply.</p><h4>Two-Phase Commit (2PC) Pattern</h4><blockquote>The 2PC Pattern is all about updating resources on multiple nodes in a single atomic operation.</blockquote><p>In 2PC, it carries out an update in two phases (Figure 02).</p><ol><li><strong>Prepare</strong>: Each node, participating in the transaction, whether it is able to carry out an update in the second phase. Once each node is able to ensure it, the coordinator will be notified. If any of the nodes are unable to make it, the coordinator is notified to roll back releasing any locks they have with nodes.</li><li><strong>Commit</strong>: Carrying out the update and completing the transaction.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*kap06STHbYfxhxe1rfrmQg.png" /><figcaption>Figure 02–2PC Transaction on a Microservices Setup</figcaption></figure><p>2PC represents a synchronous strong consistency approach within a distributed transaction.</p><p>However, 2PC is not fully recommended for microservices-based applications due to its <strong>synchronous blocking</strong>. This protocol will need to block the required object that will be changed before the transaction completes. This prevents the relevant object from being used by a different transaction (deadlock situation) until the ongoing transaction is fully completed. This is not a good situation, especially in a modern-day application.</p><p>Furthermore, it is not supported by many modern-day message brokers such as RabitMQ and Apache Kafka. In addition to that, some of the popular databases such as MongoDB and Cassandra are also not supported.</p><h4>The Saga Pattern</h4><p>As explained above, <strong>your business logic can use ACID transactions within services. However, it must use Saga Pattern in order to maintain data consistency across services.</strong></p><blockquote>Pattern: Maintain data consistency across services using a sequence of local transactions that are coordinated using asynchronous messaging [2].</blockquote><p>A Saga is a sequence of local transactions. Each local transaction updates the local database using the familiar ACID transaction frameworks and publishes an event to trigger the next local transaction in the Saga. If a local transaction fails, then the Saga executes a series of <strong><em>compensating</em></strong><em> transactions</em> that undo the changes, which were completed by the preceding local transactions (Figure 03).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b2XptfLk2d2DkMjgxavzVQ.png" /><figcaption>Figure 03 — Saga</figcaption></figure><p>It is an <em>asynchronous</em> and <em>eventually consistent</em> transactional approach, which is quite analogous to a typical microservices application architecture, where a distributed transaction is fulfilled by a set of asynchronous transactions on related microservices.</p><p>An important benefit of the asynchronous nature of a Saga message is that it ensures that all steps of a Saga are executed, even if one or more of the Saga’s participants is temporarily unavailable. In addition to that, it can support long-lived transactions without blocking any other microservice or an object in the process.</p><p>However, Saga has its own weaknesses as well. Some of the key challenges are,</p><ul><li>The lack of isolation between Sagas.</li><li>Rolling back changes when an error happens within a Saga.</li></ul><h4>Saga Coordination</h4><p>Sagas can be implemented in “two ways” primarily based on the logic that coordinates the steps of the Saga.</p><ol><li><strong>Choreography based sagas</strong></li><li><strong>Orchestration based sagas</strong></li></ol><blockquote>In a choreography based saga, a local transaction publishes events that trigger other participants to execute local transactions. In an orchestrated-based saga, a centralized saga orchestrator sends command messages to saga participants telling them to execute local transactions. [3].</blockquote><blockquote>Generally in practice, simple sagas can leverage the concept of choreography and for complex sagas, it is recommended to use the orchestrated version. [3]</blockquote><h4>Choreography based Sagas</h4><p>In this approach, unlike orchestrator-based saga, there is no central coordinator to tell saga participants what to do. Saga participants subscribe to each other’s events and respond accordingly (Figure 04).</p><ul><li>Interaction Type: Publisher / Subscriber Messaging</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iDNYtlgga307_eJnFpg4vw.png" /><figcaption>Figure 04 — Choreography based saga</figcaption></figure><p>In Figure 04, each saga participant (here it is the microservice) communicates with each other by exchanging events. In this diagram,</p><ul><li>Event Topic 1 is subscribed to Microservice 2 and 3</li><li>Event Topic 2 is subscribed to Microservice 3</li><li>Event Topic 3 is subscribed to Microservice 1 and 2</li></ul><p>Each saga participant updates its local database and publishes an event that triggers the next participant. For example, in Figure 04, once microsrevice 01 completes its local database update, it publishes an event to <em>Event Topic 1</em> in the message broker. Saga participants, which are subscribed to <em>Event Topic 1</em> are now triggered to execute any other actions within the microservice and publish further events (if any) to connect with other saga participants. Likewise, the flow can continue until the saga is fully completed.</p><p><em>Simplicity</em> and <em>Loose coupling</em> are some of the key benefits of using choreography-based sagas. However, choreography-based sagas are coupled with some significant drawbacks as well. Some of the key drawbacks are:</p><ul><li><em>Difficulty in understanding the flow</em> — Generally this type distributes the implementation of the saga among the services. In this method, there is no centralized place for it to define the flow of the saga.</li><li><em>Cyclic dependencies between services</em> — Saga participants have the possibility of having cyclic dependencies such as <em>microservice 01 -&gt; microservice 02 -&gt; microservice 01</em></li><li><em>Risk of tight coupling </em>-&gt; Having to subscribe to all events that affect them could end up having a tight coupling among services.</li></ul><p>Due to the above drawbacks and how it is being architect, it is advisable to say that choreography-based sagas are fine with a few services but with complex services setup.</p><h4>Orchestration based sagas</h4><p>In this type, a central Saga orchestration class is responsible to tell saga participants what to do.</p><ul><li>Interaction Type: Asynchronous Request/ Response</li></ul><p>Here, an Asynchronous Request also names a “Command Message”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b4fFk1k5PfPdI3mAC1Rskg.png" /><figcaption>Figure 05 — Orchestrator based saga</figcaption></figure><p>As you see in Figure 05, the “Saga Orchestrator” which is implemented within Microservice 01, initiates the saga transaction (e.g. Create Order Saga). As shown above, the interaction style is asynchronous request/ response and the request is forwarded as a “command message”. Since it uses the asynchronous request/ response style, it does use separate request and response channels within the message broker.</p><p>In Figure 05, when microservice 01 receives the create() request, it creates the saga orchestrator. This will set the service request to the PENDING state until the saga orchestrator gets the complete approval. Meantime, the saga orchestrator sends request commands to both microservice 2 and 3 and gets the responses back via the saga orchestrator response channel in the message broker. Based on the outcome of both responses, the orchestrator will approve or reject the request.</p><p><strong>Some of the key benefits are,</strong></p><ul><li><em>Simpler dependencies</em> — Always orchestrator invokes saga participants not vice versa. Therefore, there are no cyclic dependencies.</li><li><em>Less coupling </em>— Unlike a choreography-based saga, it does not need to know about events published or the business logic implemented by other saga participants. This improves coupling and greatly simplifies the business logic.</li></ul><p><strong>There are a few drawbacks as well.</strong></p><ul><li><em>Less business logic at the saga orchestrator level</em> — Try not to have business logic within the saga orchestrator allowing to have more decoupled architecture. Having business logic outside of the relevant service is “not” recommended as it is advised not to load business logic in the saga orchestrator.</li><li><em>Less Isolation</em> — As a whole, microservices architecture lacks <em>Isolation </em>compared to traditional ACID transactions. This is due to the fact that saga participants do commit changes as local transactions before completing the whole transaction. As a result at the database level <em>anomalies</em> could happen.</li></ul><h4>Anomalies</h4><p>There are three types of anomalies found in a typical saga.</p><ol><li><strong>Lost Updates </strong>— One saga overwrites an update made by another saga.</li><li><strong>Dirty Reads</strong> — One saga reads data that is in the middle of being updated by another saga.</li><li><strong>Fuzzy / Non-repeatable Reads</strong> — Two different sets of a saga read the same data and get different results because another saga has made updates.</li></ol><p>Out of these three, <em>lost update</em> and <em>dirty read</em> scenarios are the most common.</p><p>In order to rectify the anomalies, it is required to implement countermeasures in your designs. There are multiple countermeasure approaches in the literature and some of the important ones are as follows.</p><ol><li><strong>Semantic Lock</strong> — This is an application-level lock, in which saga’s compensable transactions set a flag (e.g. Creating an Order can have flag status such as APPROVAL_PENDING, REVISION_PENDING, etc.) in any record that it creates or updates. This flag indicates that the record is not committed and that it has the potential to change. This could be cleared by a retriable transaction or a compensating transaction.</li><li><strong>Commutative Updates</strong> — Designing the system to have more its update operations to be commutative (updates in an orderly manner). This can basically eliminate <em>lost updates.</em></li><li><strong>Pessimistic View </strong>— Reordering saga participants/services to minimize the effect of dirty reads.</li><li><strong>Reread Values</strong> — This countermeasure reread values before updating it to further to re-verify the values are unchanged during the process. This will minimize <em>lost updates.</em></li><li><strong>By Value</strong> — This strategy will select concurrency mechanisms based on the business risk. This can help to execute low-risk requests using sagas and execute high-risk requests using distributed transactions.</li></ol><h4>Conclusion</h4><p>That’s it! Hope you got some good insight into the Saga Pattern and other related concepts by reading this blog. You can find my other blogs related to Microservices Patterns and Concepts using the following links.</p><ol><li><a href="https://crishantha.medium.com/microservices-patterns-part-01-inter-process-communication-ipc-76010b07452e">Microservices Patterns: Inter Process Communication</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-part-02-the-circuit-breaker-pattern-ddba14adbbd4">Microservices Patterns: The Circuit Breaker Pattern</a></li><li><a href="https://crishantha.medium.com/microservices-patterns-part-03-service-discovery-patterns-97d603b9a510">Microservice Patterns: The Service Discovery Patterns</a></li></ol><p>Happy coding!</p><h4>References</h4><ol><li>Two-Phase Commit: <a href="https://martinfowler.com/articles/patterns-of-distributed-systems/two-phase-commit.html">https://martinfowler.com/articles/patterns-of-distributed-systems/two-phase-commit.html</a></li><li>The Saga Pattern: <a href="https://microservices.io/patterns/data/saga.html">https://microservices.io/patterns/data/saga.html</a></li><li>Microservices Patterns: with Examples in Java [Book] — By Chris Richardson, Manning Publications, 2018</li></ol><h4>Further Reading:</h4><ul><li><a href="https://gethelios.dev/blog/cut-engineering-costs-save-time-helios?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Microservices Monitoring: Cutting Engineering Costs and Saving Time</a></li><li><a href="https://gethelios.dev/blog/testing-microservices-with-helios/?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Testing Microservices - Trace Based Integration Testing Example</a></li><li><a href="https://gethelios.dev/blog/opentelemetry-tracing/?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">OpenTelemetry Tracing: Everything you need to know</a></li><li><a href="https://python.plainenglish.io/top-11-tools-for-microservices-backend-development-in-2023-3d9cdd61ef10">Top 11 Tools for Microservices Backend Development in 2023</a></li><li><a href="https://gethelios.dev/opentelemetry-a-full-guide/?utm_source=medium&amp;utm_medium=cloud+native+daily">OpenTelemetry: A full guide</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7f85d8d4aa3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloud-native-daily/microservices-patterns-part-04-saga-pattern-a7f85d8d4aa3">Microservices Patterns: The Saga Pattern</a> was originally published in <a href="https://medium.com/cloud-native-daily">Cloud Native Daily</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices Patterns: Service Discovery Patterns]]></title>
            <link>https://medium.com/cloud-native-daily/microservices-patterns-part-03-service-discovery-patterns-97d603b9a510?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/97d603b9a510</guid>
            <category><![CDATA[spring-cloud]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Sat, 10 Jun 2023 10:17:37 GMT</pubDate>
            <atom:updated>2023-07-12T03:50:10.350Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Microservices Pattern Series — Part 03: A deep dive into the world of Service Discovery Patterns — Client Side and Server Side Service Discovery.</em></h4><p>Due to the dynamic nature of a microservices application, it is difficult to invoke a service with a fixed IP address of the service. This is where the concept Service Discovery comes into the picture. This will help the client to know where the service instance is located. In such a situation the Service Discovery component will act as a Service Registry.</p><p>A <strong>Service Registry</strong> is a centralized server / a database containing the location of service instances. In a microservices setup, services update their locations in the service registry at regular intervals. Then service consumers can connect to the service registry and fetch the locations of those services. <a href="https://github.com/spring-cloud/spring-cloud-netflix">Netflix Eureka</a> is a popular “Application-Level” open-source Service Registry.</p><p>In service discovery, there are two types of patterns.</p><ol><li>Client Side Service Discovery</li><li>Server Side Service Discovery</li></ol><h4>Client Side Service Discovery</h4><p>Multiple steps are involved in this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_7_ns9H86D6SP86GPcUApQ.png" /><figcaption>Figure 01 — Client Side Service Discovery</figcaption></figure><ol><li>The locations of the Service Provider instances are added to the Service Registry (Self Registration Pattern).</li><li>When the Service Consumer wants to request for a particular service, the service discovery mechanism will query the Service Registry to obtain a list of available Service Provider instance locations.</li><li>The location list will be sent back to the Service Consumer after fetching it from the Service Registry.</li><li>Finally, the Service Consumer will route the service request to one of the Service Provider instances.</li></ol><h4>Self Registration Pattern</h4><p>In the Self Registration Pattern, a service instance registers itself with the Service Registry (See Figure 01 — Step 01). This happens with the Service Registry on startup and unregistered on shutdown.</p><p>The service instance may invoke Service Registry’s registration API to register its network location. In addition to that, Service Registry invokes an API to the Service Provider instance for a health check.</p><h4>Pattern 2: Server-Side Discovery Pattern</h4><p>Instead of a Service Client querying the Service Registry, it makes a request to a DNS name, which resolves to a Platform Request Router that queries the Service Registry and Load Balances requests.</p><p>The key benefit of this pattern is, unlike the client-side discovery pattern, all aspects of service discovery are entirely handled by the deployment platform. This is a major advantage and a hassle-free approach for any development party. Deployment platforms such as Kubernetes (K8) has a built-in Service Registry and a service discovery mechanism to cover the Server Side Discovery Pattern.</p><p>The only limitation of this approach is, that you are a bit coupled to the deployment platform that you are using for the Service Registry. For example, if you using Kubernetes as the deployment platform, you are basically coupled to Kubernetes for the Service Discovery. If your other service components are also running on Kubernetes this will not be an issue. However, nowadays most of the deployments happen on cloud native Kubernetes containerized environments, this is not a major limitation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bRfUrKiB2LCauxdal7Zl_Q.png" /><figcaption>Figure 02 — Server Side Discovery Pattern</figcaption></figure><p>Multiple steps are involved in this.</p><ol><li>Service instances are registered with the Service Registry by the <em>registrar </em>(Third Party Registration Pattern).</li><li>The Service Client makes a request to the service network location from the <em>Router</em> and the <em>Router</em> queries the request from the Service Registry</li><li>Then the <em>Router</em> load balances the request from the available Service Provider instances.</li></ol><h4>Third-Party Registration Pattern</h4><p>In the third-party registration (See Figure 02 — Step 02), instead of a self-registration with the service registry, a third party called the <em>registrar </em>handles the registration.</p><h4>References</h4><ol><li>Client Side Discovery Pattern: <a href="https://microservices.io/patterns/client-side-discovery.html">https://microservices.io/patterns/client-side-discovery.html</a></li><li>Server Side Discovery Pattern: <a href="https://microservices.io/patterns/server-side-discovery.html">https://microservices.io/patterns/server-side-discovery.html</a></li><li>Spring Cloud Hystrix: <a href="https://howtodoinjava.com/spring-cloud/spring-hystrix-circuit-breaker-tutorial/">https://howtodoinjava.com/spring-cloud/spring-hystrix-circuit-breaker-tutorial/</a></li><li>Kubernetes Service Discovery: <a href="https://www.densify.com/kubernetes-autoscaling/kubernetes-service-discovery/">https://www.densify.com/kubernetes-autoscaling/kubernetes-service-discovery/</a></li><li><a href="https://www.baeldung.com/cs/service-discovery-microservices">https://www.baeldung.com/cs/service-discovery-microservices</a></li></ol><h4>Further Reading:</h4><ul><li><a href="https://gethelios.dev/blog/cut-engineering-costs-save-time-helios?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Microservices Monitoring: Cutting Engineering Costs and Saving Time</a></li><li><a href="https://gethelios.dev/blog/testing-microservices-with-helios/?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Testing Microservices - Trace Based Integration Testing Example</a></li><li><a href="https://medium.com/cloud-native-daily/7-best-tracing-tools-for-microservices-27a5e3bc4b9c">7 Best Tracing Tools for Microservices</a></li><li><a href="https://gethelios.dev/opentelemetry-a-full-guide/?utm_source=medium&amp;utm_medium=cloud+native+daily">OpenTelemetry: A full guide</a></li><li><a href="https://python.plainenglish.io/top-11-tools-for-microservices-backend-development-in-2023-3d9cdd61ef10">Top 11 Tools for Microservices Backend Development in 2023</a></li><li><a href="https://gethelios.dev/blog/kubernetes-monitoring-opentelemetry/?utm_source=medium&amp;utm_medium=cloud+native+daily">Kubernetes Monitoring with OpenTelemetry</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97d603b9a510" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloud-native-daily/microservices-patterns-part-03-service-discovery-patterns-97d603b9a510">Microservices Patterns: Service Discovery Patterns</a> was originally published in <a href="https://medium.com/cloud-native-daily">Cloud Native Daily</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices Patterns Part 02: The Circuit Breaker Pattern]]></title>
            <link>https://crishantha.medium.com/microservices-patterns-part-02-the-circuit-breaker-pattern-ddba14adbbd4?source=rss-716e01d34c33------2</link>
            <guid isPermaLink="false">https://medium.com/p/ddba14adbbd4</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Crishantha Nanayakkara]]></dc:creator>
            <pubDate>Sat, 10 Jun 2023 10:17:08 GMT</pubDate>
            <atom:updated>2023-06-21T08:32:35.425Z</atom:updated>
            <content:encoded><![CDATA[<h3>Microservices Patterns: The Circuit Breaker Pattern</h3><p><em>Microservices Patterns Series — Part 02</em></p><p>The Circuit Breaker pattern is a basic pattern used especially in microservices based architectures.</p><h4>The Problem</h4><p>When one service synchronously invokes another, there is always a possibility that the other service is unavailable or unusable. This scenario could exhaust precious resources such on the caller side while waiting for the service to respond. This failure could cascade to other services throughout the application.</p><h4><strong>The Solution</strong></h4><p>The service client should invoke the remote service via a proxy that functions similar to to an electronic circuit breaker. It maintains a time out to minimize the client request load and handle service level errors efficiently.</p><ol><li>When the number of consecutive failures crosses a threshold, the circuit breaker trips and stops responding client requests.</li><li>For the duration of a timeout period all attempts to invoke the remote service will fail immediately.</li><li>After the timeout expires the circuit breaker allows a limited number of test requests to pass through. If those requests succeed the circuit breaker will resume its normal operation.</li><li>Otherwise, if there is a failure the timeout period begins again.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*ivcEoBXTOLv5CvTTqn1KKA.png" /><figcaption>Figure 04 — Circuit Breaker Pattern</figcaption></figure><p>When there is a service composition connected to the client proxy, it calls all services and aggregates their responses and sends the response to the client. The code that implement the client proxy endpoint should have a logic to handle the failure of each service it calls (Figure 05).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ZHtBvXI_E7XOKtV10FZGg.png" /><figcaption>Figure 05 — Circuit Breaker Pattern with Proxy level service orchestration</figcaption></figure><p>In a typical microservices deployment, containers are dynamic and has the tendency to go down at any given point. Therefore, this pattern implementation helps to prevent from sending unnecessary loads of requests to a failed service, by providing a delay to recover from errors.</p><h4>References</h4><ol><li>Microservices Patterns: with Examples in Java [Book] — By Chris Richardson, Manning Publications, 2018</li><li>Circuit Breaker Pattern: <a href="https://microservices.io/patterns/reliability/circuit-breaker.html">https://microservices.io/patterns/reliability/circuit-breaker.html</a></li><li>Spring Cloud Hystrix: <a href="https://cloud.spring.io/spring-cloud-netflix/multi/multi__circuit_breaker_hystrix_clients.html">https://cloud.spring.io/spring-cloud-netflix/multi/multi__circuit_breaker_hystrix_clients.html</a></li><li>Hystrix Circuit Breaker Pattern: <a href="https://howtodoinjava.com/spring-cloud/spring-hystrix-circuit-breaker-tutorial/">https://howtodoinjava.com/spring-cloud/spring-hystrix-circuit-breaker-tutorial/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ddba14adbbd4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>