DESIGN PRINCIPLES FOR MICROSERVICES
Microservices
Microservices ?Architecture provides us the facility to handle different functions of the application individually. In the Monotholic approach, we can’t scale any particular functionality, If we need to scale any particular functionality, then we would have to scale the complete application, Hence Microservices architecture is more useful for us if we have multiple functionalities in our application and we wanted to manage them in the right manner.
??????????????BENEFITS OF MICROSRVICES
?????
?
?
?
?DESIGN PRINCIPLES FOR MICROSERVICES
Cohesion?and?coupling?are two terms often used interchangeably when describing a microservices architecture. The former relates to the degree of intradependence that exists between the modules of an application, and the latter is used for the degree of interdependencies.
You should design microservices so that cohesion is high and coupling is low. This plan creates microservices that are adaptable to changes, scalable and can be extended over time.
You should define the functionality of a microservice, describing what it is intended to do. The scope of a microservice corresponds to the requirement of an independent business module. It's important to set a proper scope for each microservice in order to rationalize its size and define its boundaries.
The?Single Responsibility Principle?states that a class should never have more than one reason for change. This principle is essential to designing a microservices-based application, because there should not be multiple responsibilities in a single microservice.
One of the objectives of microservice architecture is to create fault-tolerant and resilient software systems.?Failure or performance issues?in one service should not affect other services. A memory leak, database connectivity problems or other issues in one microservice should not bring the entire application down.
Each microservice should be designed to?solve a business problem. The developer can use the appropriate technology stack for each business problem in each microservice. Unlike a monolithic application, you are not constrained to use a single best-fit homogenous technology stack for the whole architecture. This microservices design principle means developers should choose what's best and readily available for use in every component of the application.
Unlike in monolithic applications, microservices each maintain their?own copy of the data. In other words, each microservice has its own database. You should not set up multiple services to access or share the same database, since it defeats the purpose of autonomous microservice operation.
Traffic to microservices in an application differs from one to the next. One service might have huge traffic while another is low-demand on the network. In each kind of traffic scenario, performance is an important factor. Take advantage of autoscaling and circuit breaker patterns?to maximize performance.
?
??????????????????????????????Design Patterns for Microservices:-
1. Database per Microservice Pattern
Database design is rapidly evolving, and there are numerous hurdles to overcome while developing a microservices-based solution. Database architecture is one of the most important aspects of microservices.?
What is the best way to store data and where should it be stored?
There should are two main options for organizing the databases when using the microservice architecture.?
2. Event Sourcing Pattern
The event sourcing is responsible for giving a new ordered sequence of events. The application state can be reconstructed using querying the data and in order to do this, we need to reimage every change to the state of the application.?Event Sourcing?is based on the idea that any change in an entity's state should be captured by the system.?
The persistence of a business item is accomplished by storing a series of state-changing events. A new event is added to the sequence of events every time an object's state changes. It's essentially atomic because it's one action. By replaying the occurrences of an entity, its current state can be reconstructed.
An event store is used to keep track of all of your events. The event store serves as a message broker as well as a database of events. It gives services the ability to subscribe to events via an API. The event store sends all interested subscribers information about each event that is saved in the database. In an event-driven microservices architecture, the event store is the foundation.
This pattern can be used in the following scenarios,
So as from the above discussion, it is clearly indicated that the event sourcing addresses a challenge of implementing an event-driven architecture. Microservices with shared databases can't easily scale.?The database will also be a single point of failure. Changes to the database could have an influence on a number of services.
?
3. Command Query Segmentation (CQRS)?Pattern
In the above, we have discussed what is event sourcing. In this topic, we are going to discuss what is CQRS? We can divide the topic into two parts with commands and queries.?Commands - Change the state of the object or entity.
?Queries -?Return the state of the entity and will not change anything.
领英推荐
?
In traditional data management systems, there are some issues,
??1. Risk of data contention
??2. Managing performance and security is complex as objects are exposed to both reading and writing applications.
So in order to solve these problems, the CQRS comes to the big picture. The CQRS is responsible for either change the state of the entity or return the result.?
?
4. Backend For Frontend (BFF)
This pattern is used to identify how the data is fetched between the server and clients. Ideally, the frontend team will be responsible for managing the BFF.A single BFF is responsible for handling the single UI and it will help us to keep the frontend simple and see a unified view data through the backend.
?
5. API Gateway
This microservice architecture pattern is really good for large applications with multiple client apps and it is responsible for giving a single entry point for a certain group of microservices.?API gateway sits between the client apps and the microservices and it serves as a reverse proxy, forwarding client requests to services.?Authentication, SSL termination, and caching are some of the other cross-cutting services it can provide.
Why do we consider the API Gateway architecture instead of using direct client-to-microservice communication? We will discuss this with the following examples,??
1. Security issues?-?All microservices must be exposed to the "external world" without a gateway, increasing the attack surface compared to hiding internal microservices that aren't directly accessed by client apps.
2. Cross-cutting concerns?- Authorization and SSL must be handled by each publicly published microservice. Those problems might be addressed in a single tier in many cases, reducing the number of internal microservices.
3. Coupling?-?Client apps are tied to internal microservices without the API Gateway pattern. Client apps must understand how microservices decompose the application's various sections.
Last but not least, the microservices API gateway must be capable of handling partial failures. The failure of a single unresponsive microservice should not result in the failure of the entire request.
A microservices API gateway can deal with partial failures in a variety of ways, including:
?
6. Strangler
The strangler design pattern is a popular design pattern to incrementally transform your monolithic application to microservices by replacing old functionality with a new service.?Once the new component is ready, the old component is strangled and a new one is put to use.
The facade interface, which serves as the primary interface between the legacy system and the other apps and systems that call it, is one of the most important components of the strangler pattern.
?
7. Circuit Breaker Pattern
The circuit breaker is the solution for the failure of remote calls or the hang without a response until some timeout limit is reached. You can run out of critical resources if you having many callers with an unresponsive supplier and this will lead to failure across the multiple systems in the applications.
So here comes the circuit breaker pattern which is wrapping up a protected function call in a circuit breaker object which monitors for failure. When the number of failures reaches a specific level, the circuit breaker trips, and all subsequent calls to the circuit breaker result in an error or a different service or default message, rather than the protected call being made at all.
Different states in the circuit break pattern
Closed?- When everything works well according to the normal way, the circuit breaker remains in this closed state.
Open?-?When the number of failures in the system exceeds the maximum threshold, this will lead to open up the open state. This will give the error for calls without executing the function.
Open?-Half - After having run the system several times, the circuit breaker will go on to the half-open state in order to check the underlying problems are still exist.?
?
8. Externalized Configuration
Often services need to be run in different environments.?Environment-specific configuration is required, such as secret keys, database credentials, and so on. Changing the service for each environment has a number of drawbacks. So how we can enable a service to run in multiple environments without modification?Here comes the Externalized configuration pattern as this enables the externalization of all application configurations including the database credentials and network location.
?
?
Best Languages for Microservices
Here is a list of the best language for microservices are:
Alongside large corporations, numerous small and medium-sized businesses and enterprises are shifting towards developing complex applications, intending to migrate from monolithic systems to a single independent Microservice architecture. Choosing the right technology and languages for your microservices is difficult. After all, the tool you choose to create distinct app pieces determines the technology you use. Furthermore, the knowledge of your employees is crucial. If you’ve decided to go the microservices route, you’ll need to find a trustworthy partner to help you accomplish your project.
Things we should keep in our Mind Before Adopting Microservices Architecture
THANK YOU FOR READING!!!!!!!!!!!!!!!
World Record Holder | 2x TEDx Speaker | Philanthropist | Sr. Principal Consultant | Entrepreneur | Founder LW Informatics | Founder Hash13 pvt ltd | Founder IIEC
1 年Great
Aspiring DevOps Engineer | Passionate About Cloud, Automation & CI/CD Pipelines | Linux | Shell scripting | Git & Github | Jenkins | Ansible | Terraform | Docker | Kubernetes | Aws | Azure | Python
1 年Informative ??
UGC NET English Qualified 2022|| Author of UPSC Mains Essay 13 Previous Years 2011 - 23 Solved Paper|| Corporate Communication Trainer|| Lecturer English|| Research Scholar|| Writing Specialist|| Speaker
1 年A must-read post.
MBA | Human Resources Management | Onboarding | Training & Development | Social Worker |
1 年Great ??????
Executive in Criminal checks and special checks
1 年Great job Prateek!!