Saturday, May 1, 2021

System design: Microservices design patterns | My 20 minutes reading

May 1, 2021

Here is the article.  

Top Microservices Design Patterns To Build Your Applications

Sahiti Kappagantula
Aug 2, 2019 · 11 min read


In today’s market, Microservices have become the go-to solution, to build an application. They are known to solve various challenges, but yet, skilled professionals often face challenges while using this architecture. So, instead, developers can explore the common patterns in these problems, and can create reusable solutions to improve the performance of the application. Thus, in this article on Microservices Design Patterns, I will discuss the top patterns necessary to build a successful Microservices.

The following topics will be covered in this article:

  • What are Microservices?
  • Principles Used to Design Microservice Architecture
  • Design Patterns of Microservices

Microservices, aka microservice architecture, is an architectural style that structures an application as a collection of small autonomous services, modeled around a business domain. In a Microservice Architecture, each service is self-contained and implements a single business capability. If you want a detailed understanding of Microservices, you can refer to my article on Microservices Architecture.

Principles Used to Design Microservice Architecture

The principles used to design Microservices are as follows:

  1. Independent & Autonomous Services
  2. Scalability
  3. Decentralization
  4. Resilient Services
  5. Real-Time Load Balancing
  6. Availability
  7. Continuous delivery through DevOps Integration
  8. Seamless API Integration and Continuous Monitoring
  9. Isolation from Failures
  10. Auto -Provisioning

Design Patterns of Microservices

  1. Aggregator
  2. API Gateway
  3. Chained or Chain of Responsibility
  4. Asynchronous Messaging
  5. Database or Shared Data
  6. Event Sourcing
  7. Branch
  8. Command Query Responsibility Segregator
  9. Circuit Breaker
  10. Decomposition

Aggregator Pattern

Aggregator in the computing world refers to a website or program that collects related items of data and displays them. So, even in Microservices patterns, Aggregator is a basic web page which invokes various services to get the required information or achieve the required functionality.

Also, since the source of output gets divided on breaking the monolithic architecture to microservices, this pattern proves to be beneficial when you need an output by combining data from multiple services. So, if we have two services each having their own database, then an aggregator having a unique transaction ID, would collect the data from each individual microservice, apply the business logic and finally publish it as a REST endpoint. Later on, the data collected can be consumed by the respective services which require that collected data.

The Aggregate Design Pattern is based on the DRY principle. Based on this principle, you can abstract the logic into a composite microservices and aggregate that particular business logic into one service.

So, for example, if you consider two services: Service A and B, then you can individually scale these services simultaneously by providing the data to the composite microservice.

API Gateway Design Pattern

Microservices are built in such a way that each service has its own functionality. But, when an application is broken down into small autonomous services, then there could be few problems that a developer might face. The problems could be as follows:

  1. How can I request information from multiple microservices?
  2. Different UI require different data to respond to the same backend database service
  3. How to transform data according to the consumer requirement from reusable Microservices
  4. How to handle multiple protocol requests?

Well, the solution to these kinds of problems could be the API Gateway Design Pattern. The API Gateway Design Pattern address not only the concerns mentioned above but it solves many other problems. This microservice design pattern can also be considered as the proxy service to route a request to the concerned microservice. Being a variation of the Aggregator service, it can send the request to multiple services and similarly aggregate the results back to the composite or the consumer service. API Gateway also acts as the entry point for all the microservices and creates fine-grained APIs’ for different types of clients.

With the help of the API Gateway design pattern, the API gateways can convert the protocol request from one type to other. Similarly, it can also offload the authentication/authorization responsibility of the microservice.

So, once the client sends a request, these requests are passed to the API Gateway which acts as an entry point to forward the clients’ requests to the appropriate microservices. Then, with the help of the load balancer, the load of the request is handled and the request is sent to the respective services. Microservices use Service Discovery which acts as a guide to find the route of communication between each of them. Microservices then communicate with each other via a stateless server i.e. either by HTTP Request/Message Bus.

Chained or Chain of Responsibility Pattern

Chained or Chain of Responsibility Design Patterns produces a single output which is a combination of multiple chained outputs. So, if you have three services lined up in a chain, then, the request from the client is first received by Service A. Then, this service communicates with the next Service B and collects data. Finally, the second service communicates with the third service to generate the consolidated output. All these services use synchronous HTTP request or response for messaging. Also, until the request passes through all the services and the respective responses are generated, the client doesn’t get any output. So, it is always recommended to not to make a long chain, as the client will wait until the chain is completed

One more important aspect which you need to understand is that the request from Service A to Service B may look different from Service B to Service C. Similarly the response from Service C to Service B may look completely different from Service B to Service A.

Asynchronous Messaging Design Pattern

From the above pattern, it is quite obvious that the client gets blocked or has to wait for a long time in synchronous messaging. But, if you do not want the consumer, to wait for a long time, then you can opt for the Asynchronous Messaging. In this type of microservices design pattern, all the services can communicate with each other, but they do not have to communicate with each other sequentially. So, if you consider 3 services: Service A, Service B, and Service C. The request from the client can be directly sent to the Service C and Service B simultaneously. These requests will be in a queue. Apart from this, the request can also be sent to Service A whose response need not have to be sent to the same service through which request has come.

Database or Shared Data Pattern

For every application, there is a humongous amount of data present. So, when we break down an application from its monolithic architecture to microservices, it is very important to note that each microservice has a sufficient amount of data to process a request. So, either the system can have a database per each service or it can have shared database per service. You can use database per service and shared database per service to solve various problems. The problems could be as follows:

  • Duplication of data and inconsistency
  • Different services have different kinds of storage requirements
  • Few business transactions can query the data, with multiple services
  • De-normalization of data

Well, to solve the first three problems, I think you can go for database per service, as it will be then accessed by the microservice API itself. So, each microservice will have its own database ID, which thereafter prevents the other services in the system to use that particular database. Apart from this, to solve the issue of de-normalization, you can choose shared databases per service, to align more than one database for each microservice. This will help you gather data, for the monolithic applications which are broken down into microservices. But, you have to keep in mind that, you have to limit these databases to 2–3 microservices; else, scaling these services will be a problem.

Event Sourcing Design Pattern

The event sourcing design pattern creates events regarding the changes in the application state. Also, these events are stored as a sequence of events to help the developers track which change was made when. So, with the help of this, you can always adjust the application state to cope up with the past changes. You can also query these events, for any data change and simultaneously publish these events from the event store. Once the events are published, you can see the changes in the application state on the presentation layer.

Branch Pattern

Branch microservice design pattern is a design pattern in which you can simultaneously process the requests and responses from two or more independent microservices. So, unlike the chained design pattern, the request is not passed in a sequence, but the request is passed to two or more mutually exclusive microservices chains. This design pattern extends the Aggregator design pattern and provides the flexibility to produce responses from multiple chains or single chain. For example, if you consider an e-commerce application, then you may need to retrieve data from multiple sources and this data could be a collaborated output of data from various services. So, you can use the branching pattern, to retrieve data from multiple sources.

Command Query Responsibility Segregator (CQRS) Design Pattern

Every microservices design has either the database per service model or the shared database per service. But, in the database per service model, we cannot implement a query as the data access is only limited to one single database. So, in such scenario, you can use the CQRS pattern. According to this pattern, the application will be divided into two parts: Command and Query. The command part will handle all the requests related to CREATE, UPDATE, DELETE while the query part will take care of the materialized views. The materialized views are updated through a sequence of events which are creating using the event source pattern discussed above.

Circuit Breaker Pattern

As the name suggests, the Circuit Breaker design pattern is used to stop the process of request and response if a service is not working. So, for example, let’s say a client is sending a request to retrieve data from multiple services. But, due to some issues, one of the services is down. Now, there are mainly two problems you will face: first, since the client will not have any knowledge about a particular service being down, the request will be continuously sent to that service. The second problem is that the network resources will be exhausted with low performance and bad user experience.

So, to avoid such problems, you can use the Circuit Breaker Design Pattern. With the help of this pattern, the client will invoke a remote service via a proxy. This proxy will basically behave as a circuit barrier. So, when the number of failures crosses the threshold number, the circuit breaker trips for a particular time period. Then, all the attempts to invoke the remote service will fail in this timeout period. Once that time period is finished, the circuit breaker will allow a limited number of tests to pass through and if those requests succeed, the circuit breaker resumes back to the normal operation. Else, if there is a failure, then the time out period begins again.

Decomposition Design Pattern

Microservices are developed with an idea on developers mind to create small services, with each having their own functionality. But, breaking an application into small autonomous units has to be done logically. So, to decompose a small or big application into small services, you can use the Decomposition patterns.

With the help of this pattern, either you can decompose an application based on business capability or on based on the sub-domains. For example, if you consider an e-commerce application, then you can have separate services for orders, payment, customers, products if you decompose by business capability.

But, in the same scenario, if you design the application by decomposing the sub-domains, then you can have services for each and every class. Here, in this example, if you consider the customer as a class, then this class will be used in customer management, customer support, etc. So, to decompose, you can use the Domain-Driven Design through which the whole domain model is broken down into sub-domains. Then, each of these sub-domains will have their own specific model and scope(bounded context). Now, when a developer designs microservices, he/she will design those services around the scope or bounded context.

Though these patterns may sound feasible to you, they are not feasible for big monolithic applications. This is because of the fact that identifying sub-domains and business capabilities is not an easy task for big applications. So, the only way to decompose big monolithic applications is by following the Vine Pattern or the Strangler Pattern.

Strangler Pattern or Vine Pattern

The Strangler Pattern or the Vine Pattern is based on the analogy to a vine which basically strangles a tree that it is wrapped around. So, when this pattern is applied on the web applications, a call goes back and forth for each URI call and the services are broken down into different domains. These domains will be hosted as separate services.

According to the strangler pattern, two separate applications will live side by side in the same URI space, and one domain will be taken in to account at an instance of time. So, eventually, the new refactored application wraps around or strangles or replaces the original application until you can shut down the monolithic application

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Microservices.

1. What is Microservices?

2. Microservices Architecture

3. Microservices vs SOA

4. Microservices Tutorial

5. Building Microservices Application Using Spring Boot

5. Microservices Security

System design: Microservice aggregator pattern

May 1, 2021

Here is the article. 

Aggregator Pattern Overview


The aggregator design pattern is a service that receives a request, subsequently makes requests of multiple services, combines the results and responds to the initiating request.  The diagram pictorially displays this pattern:


Benefits of the Aggregator Pattern

  • Reduces chattiness and communication overhead between the client and services, especially in the case of mobile clients that may be limited in bandwidth or number of connections.
  • Intellectually easy to understand and implement – allows engineers to develop fast time to market solutions at the expense of customer perceived availability
  • Allows for architecturally easily understood endpoint consolidation of discrete functionality.

Drawbacks of the Aggregator Pattern

Put as simply as possible, your solution should avoid the aggregator pattern for a majority of your critical functionality.  The pattern is an implementation of the AKF Microservice Anti-Pattern Fan—Out

  • Reduces availability as a result of the multiplicative effect of failure (per article above).
  • Likely increases latency of responses as a result of the anti-pattern above.
  • Increases complexity (cost) of troubleshooting which results in additional time to restore service and repair faults for many incidents.

When to Use the Aggregator Pattern

  • Low Scale Needs:  Low transaction rate/low demand/low expected increased rate of demand activities.  These are candidates only when the cost to fault isolate components is high relative to the availability increase it enables.  Examples may include low return material authorization request services.
  • Low Business Value:  Lower business value functions where the pain of suffering outages as a result of fan out anti-pattern usage is low.  Examples of low business value solutions may be company executive team bio pages, company stock price/investor relations page, etc.
  • Team Velocity:  When a “combined” or monolithic service would require more than one team to implement it, and pain from the increased rate of failure is low (per above).  You may for instance decide to implement a set of new functionalities under the aggregator pattern, knowing that if it gets widely adopted later you will need to rearchitect it.  Examples here may be a new series of recommendations based on your past purchases, the past purchases of others, and recently viewed items.  Because all of them are new, you could call a common recommendation service.  Once the services show value to the checkout process, you will likely want to split them up and make them separate calls.
  • Third Party Calls: This is a special exception to our advice not to use the aggregator pattern (or composite service).  When Services B and C are remote, third party calls, and the calls to Services B and C are asynchronous to offset the high failure rate of distant third-party calls, the Aggregator Pattern can be useful.  Service A is built with circuit breakers and becomes the third-party proxy for all communications.  Ideally, calls to Service A from inside the remainder of the architecture is also asynchronous.

Considerations During Usage:

  • Distance and Latency:  When using an aggregator, the aggregator should be collocated (in the same data center, or availability zone) as the services of which it is comprised.  The one exception to this rule is when it is used to combine third party calls.  In this case, all interactions with the aggregator should be asynchronous.
  • Single Logical Point of Failure:  Because the aggregator has a relatively high probability of failure, it needs to be more redundant than most solutions.  Additional copies, and segmentation of users along the Z axis of the AKF Scale Cube  is appropriate to engender higher levels of availability .
  • Install Bulkheads:  Given the increased probability of failure, the aggregator and services of which it is comprised must not speak to additional services.  Bulkheads https://akfpartners.com/growth-blog/bulkhead-pattern (fault isolation zones or swimlanes) must be put in place between it and other services.  Further segmentation along customer boundaries as described above is also appropriate – similarly bulkheaded from each other.
  •  
  • Install Circuit Breakers:  Because failure and slowness of the services of which the aggregator is comprised will affect the aggregator and as a result both services, the connections to the services should be severable via a circuit breaker.  Further, the aggregator should be able to continue to operate without the presence of any of its “children”.
  • Ideally Includes Asynchronous Communications:  As earlier indicated, all communication with the aggregator service whether from a client or another service ideally is async in nature.
  • Real Time Monitoring:  More than any other atomic service, the aggregator and its children must be monitored in both depth and breadth.  Identifying service interruption and cause of interruption is critically important to restoring normal service operation. 
  • Gateway vs Composite Implementation:  When implemented as a reverse proxy, the Aggregator Pattern is called a Gateway Aggregator Gateway Aggregators inherit all of the concerns of a normal (or “composite tier” aggregator).


When to Avoid the Aggregator Pattern

  • High Scalability Needs:  Aggregator services are difficult to scale because they have multiple dimensions.  When is the scale need at the point of aggregation and when is it within one of the aggregator’s children?  Analyzing this is difficult at best, and most companies with aggregator services find themselves in a constant fight of finding the component that is most critical to scale.  If you need extreme scale, stay away from this pattern.
  • High Availability Needs:  As we’ve indicated, aggregators always mathematically reduce the availability of a system.  As such, if a solution requires incredibly high availability (e.g. checkout, search, file taxes, etc) you should run away from this pattern.
  • Solutions that Comprise Significant Business Value: Do not deploy this pattern for solutions that comprise a majority of value for customers and/or the business, as high failure rates resulting from the fan-out anti-pattern will cause incredibly high levels of pain and will likely result in customer churn.

System design: Microservices Design Patterns | Microservices Architecture Patterns | Edureka

May 1, 2021

Here is the link. 

** Microservices Architecture Training: https://www.edureka.co/microservices-...​ ** This Edureka's video on Microservices Design Patterns talks about the top design patterns you can use to build applications. In this video, you will learn the following: 1:29​ Why do we need Design Patterns? 3:41​ What are Design Patterns? 4:28​ What are Microservices? 6:00​ Principles behind Microservices 10:24​ Microservices Design Patterns #edureka​ #microservicesedureka​ #microservicesdesignpatterns​ #microservices​ Join Edureka’s Meetup community and never miss any event – YouTube Live, Webinars, Workshops, etc. https://bit.ly/2EfTXS1​ Subscribe to our channel to get video updates. Hit the subscribe button above: https://goo.gl/6ohpTV​ SlideShare: https://www.slideshare.net/edurekaIN​ Instagram: https://www.instagram.com/edureka_lea...​ Facebook: https://www.facebook.com/edurekaIN/​ Twitter: https://twitter.com/edurekain​ LinkedIn: https://www.linkedin.com/company/edureka​ How it Works? 1. This is a 4 Week Instructor-led Online Course. 2. We have a 24x7 One-on-One LIVE Technical Support to help you with any problems you might face or any clarifications you may require during the course. 3. At the end of the training, you will be working on a real-time project for which we will provide you a Grade and a Verifiable Certificate! --------------------------------------------------------------------------------- About Microservices Architecture Training

Edureka’s Microservices Architecture training introduces you to the concept of Microservices that are small, lightweight, process-driven components. So, Microservices are the next important thing in designing scalable, easy-to-maintain applications. This not only makes application development easier but also offers great flexibility and lets you utilize various resources optimally. If you want to build an enterprise-ready implementation of the Microservices architecture, then this course is the first step for you! In this Microservices Architecture training, we will start by giving you a deep understanding of the core Microservices concepts and principle with insight in how they have evolved. We will walk you through the complete life cycle - from design to development to testing; including cross-layer concepts such as how to secure Microservices. ------------------------------------------------------------------------ What are the Objectives of our Microservices Architecture Training? After completing, Edureka's Microservice training, you will be able to: 1.Understand and differentiate between various Microservices Architectural styles 2.Apply Microservices Architecture principles 3.Know how to make the appropriate Microservice Architecture decision 4.Develop and test a Microservice 5.Know what technologies can be used to enable Microservices with an example --------------------------------------------------------------- Why should you go for Microservices Architecture Training? Microservices Architecture, or simply Microservices, is a unique method of developing software systems as a suite of independently deployable, small, modular services in which each service runs a unique process and communicates through a well-defined, lightweight mechanism to serve a business goal. Thanks to its scalability, this architectural method is considered ideal when you have to enable support for a range of platforms and devices spanning across the web, mobile, Internet of Things, and so on. Because of its flexibility, you can also use this method when you’re not sure what kind of devices you’ll need to support in an increasingly Cloud-based future.

-------------------------------------------------------------------------------- What are the Pre-Requisites for Microservices Architecture Training ? There are no prerequisites for attending this Microservice course. Understanding of programming languages such as Java, basic understanding and familiarity with Spring Boot framework and building Java applications would be useful to execute Case Study and Project. ---------------------------------------------------------------------------------------------------- Who should go for this Microservices Architecture Training? 1.Application Architects 2.Software Architects 3.Application Developers 4.A developer working on Web, Cloud, Mobile, and other social technologies For more information, please write back to us at sales@edureka.in or call us at IND: 9606058406 / US: 18338555775 (toll-free)

Principles:

  • Independent & autonomous services -
  • Scalability -
  • Decentralization -
  • Resilient service -
  • Real time load balancing -
  • Availability
  • Continuous delivery through DevOps integration
  • Seamless API integration & Continuous monitoring
  • Isolation from failures
  • Auto-provisioning
Take some notes:

  • Independent & autonomous services -
  • Scalability - scale individually, without scaling others
  • Decentralization - not centralized architecture
  • Resilient service -
  • Real time load balancing -
  • Continuous delivery through


Patterns
  • Aggregator - Collects related items of data and displays them based on DRY principle
  • Chained or chain of responsibility

System design: Top 25 Microservice Interview Questions Answered - Java Brains

 May 1, 2021

Here is the link.