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.
- 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:-
- Built on Spring 5, reactor, and Spring WebFlux.
- It also includes circuit breaking and discovery service with Eureka.
- 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.
- 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.
- No Limitation on Availability
- Peer to Peer communication between service Discovery agent
- Dynamically Managed IPs, Configurations, and Load Balance.
- Fault-tolerance and Resilience
- 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.
- Circuit Breaking:- Used to stop making a request when a service is failing.
- Fallback:- Alternative path to failing service.
- Retry:- Retry when a service is failing temporarily failed.
- Rate Limit:- Limit the number of calls a service gets at a time.
- Bulkhead:- To avoid overloading.
- 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.
- 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.
- Zipkin:- Is used for Data-Visualisations
- 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.
- Actuator:- is mainly used to expose operational information like health, dump, info, and memory.
- 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.
- Prometheus:- It is a time-series database to store metric data and also has the data-visualization capability.
- 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.