Showing posts with label microservices. Show all posts
Showing posts with label microservices. Show all posts

Sunday, 8 January 2023

Microservice Architecture in .NET

In my previous blog we already discussed Microservice Architecture using Java but today we are going to develop and understand how we can leverage the Microservice architecture using .NET core API, and what all tools we can use to make it Resilience, observability, Fault-tolerance, Monitoring, and Rate-limiting and all other components which are necessary for any Microservice driven architecture.   

Before we move forward let's review some PROS and CONS of having Microservice and Monolithic architecture.




To start with let's build an example, where we are going to create a Catalog and Inventory APIs and build our Microservice architecture around it. So the big question comes when to use Microservices?

  • It's fine to start with a monolith and then move to Microservices 
  • And We should start looking at Microservice when:
    • The code base size is more than what a small team can maintain
    • A team can't move fast anymore
    • Build because too slow due to large code base
    • Time to market is compromised due to infrequent deployments and long testing times.
  • It's all about team autonomy.

In our previous Microservice architecture in JAVA, we build Microservice which communicates synchronously but today we are going to build an asynchronous Microservice, and will also understand its benefits.



Synchronous Communication 

  • The client sends a request and waits for a response from the service which is exactly happening in our Mircorservice example in JAVA
  • The client cannot process without the response.
  • The client thread may use blocking or non-blocking callbacks.
  • REST+HTTP protocol is the traditional approach
  • Partial failures will happen.
  • In a distributed system whenever a service makes a synchronous request to another service, there is a risk of partial failure.
  • So, We must design our services to be resilient 
  • Timeouts, for more response experience and to ensure resources are never tied up 
  • Implement a Circuit breaker pattern to prevent our service from reaching resource exhaustion 

Asynchronous Communication

  • The client does not wait for a response in a timely manner
  • There might be no response at all
  • Usually involves the use of a lightweight message broker
  • Message broker has high availability 
  • Messages are sent to the broker and could be received by a single receiver or multiple receivers 

In the Asynchronous Microservice, which I build. 

  • For resilience and transient-fault handling capabilities, Retry, and Circuit Breaker.  I have used Polly.NET libraries.
  • For API Gateway, Caching, and Rate Limiting, I have used Ocelot .NET libraries. 
  • For Monitoring, I have used Prometheus and Grafana.
  • RabbitMQ is used for messaging.
  • MongoDB is used to store the data.

The Code is available on GitHub

Happy coding and keep sharing!!

Tuesday, 18 October 2022

Microservice Architecture in Java

Microservice Architecture enables large teams to build scalable applications that are composed of multiple small loosely coupled services. In Microservice each service handles a dedicated function inside a large-scale application.

Challenges that we all see when designing Microservice Architecture are "Right-Sizing and Identifying the limitations and Boundaries of the Services".

Some of the most commonly used approaches in the industry:-

  • Domain Driven:- In this approach, we would need good Domain Knowledge and it takes a lot of time to close alignment with all the Business stakeholders to identify the need and requirements to develop Microservices for business capabilities.  
  • Event Storming Sizing:-  We conduct a session with all business Stakeholders and identify various events in the system and based on that we can group them in Domain Driven.

In the below Microservice Architecture for a Bank, where we have (Loan, Card, Account, and Customer) Microservices, along with other required services for the successful implementation of Microservice Architecture. 


Let's look at the most critical components that are required for Microservice Architecture Implementation. 

The API Gateway handles all incoming requests and routes to the relevant microservices.  The API gateway depends on the Identity Provider service to handle the authentication.

To locate the service to route an incoming request to, API Gateway consults a service registry and discovery service. ALL Microservice register with Service Registry and Discover the location of other Microservices using Discovery services. 

Let's take a look at the components in detail for a Successful Microservice Architecture and why they are required.
  1. Handle Routing Requirements API Gateway:- Spring Cloud Gateway is a library for building an API gateway. Spring cloud gateway sits between a requester and a resource, where it intercepts analysis of the request.  It is also a preferred API gateway from the spring cloud team.  It also has the following advantages:- 
    1. Built on Spring 5, reactor, and Spring WebFlux.
    2. It also includes circuit breaking and discovery service with Eureka.  
  2. Configuration Service:-  We can't Hard code the config details inside the service and in a DTAP it would be a nightmare to manage all config in the application properties plus manage them when a new service joins. So for that In a Microservice architecture, we have a config service that then can load and inject the configuration from (Git Repo, File system, or Database) to Microsrevies while they're starting up, and since we are talking about Java, I have used Spring Cloud Config for Configuration Management.
  3. Service Registry and Discovery:- In a Microservice Arihcture how do services locate each other inside a network and how do we tell our application architecture when a new service is onboarded or a new node is added for existing services and how load balancer will work. This all looks very complicated but, We have Spring Cloud Discovery Service using the Eureka agent. Some Advantages of using Service discovery. 
    1. No Limitation on Availability 
    2. Peer to Peer communication between service Discovery agent
    3. Dynamically Managed IPs, Configurations, and Load Balance.
    4. Fault-tolerance and Resilience 
  4. Resilience Inside Microservices:- In this, We make sure that we handle the service failure gracefully, avoid cascading effects if one of the services is failed, and have self-healing capabilities. For Resilience Spring Framework Support Resilience4J  which is a lightweight and easy-to-use fault tolerance library inspired by NetFlix Hystrix. Before Resilience4J NetFlix Hystrix.is most commonly used for resiliency but it is now in maintenance mode.  Resilience4J offers the following patterns for increasing fault tolerance. 
    1. Circuit Breaking:- Used to stop making a request when a service is failing.
    2. Fallback:- Alternative path to failing service.
    3. Retry:- Retry when a service is failing temporarily failed.
    4. Rate Limit:- Limit the number of calls a service gets at a time.
    5. Bulkhead:- To avoid overloading.
  5. Distributed Tracing and logging:- For debugging the problem in a microservice architecture we would need to aggregate all the logs traces and monitor the chain of service calls for that we have Spring Cloud Sleuth and Zipkin.
    1. Sleuth provides auto-configuration for disturbing logs it also adds the SPAN ID to all the logs by filtering and interacting with other spring components and generating the Correlation Id passes through to all the system calls.
    2. Zipkin:- Is used for Data-Visualisations 
  6.  Monitoring:- Is used to monitor service metrics health checks and create alerts based on Monitoring and we have different approaches to do that. Let's see the most commonly used approaches.
    1.  Actuator:- is mainly used to expose operational information like health, dump, info, and memory.
    2. Micrometer:- Expose Actuator data in a format that can be understood by the Monitoring system all we need to add vendor-specific Micrometer dependency in the service.
    3. Prometheus:- It is a time-series database to store metric data and also has the data-visualization capability.
    4. Grafana:-  Pulled the data from various data sources like Prometheus and offers rich UI to create custom Dashboard and also allows to set rule-based alerts and notifications. 

We have covered all the relevant components for a successful Microservice Architecture, I build  Microservices using  Spring Framework and all the above Components Code Repo

Happy Coding and Keep Sharing!!