Microservices
- Types of Architectural Patterns
- Service Registry
- API Gateway
- Load Balancing
- Rate Limiter Pattern
- Fault Tolerance
- Inbox-Outbox Pattern
- The Twelve Factors
- Security
- Monitering
- Alerting
- Logging
- Distributed Tracing
- Distributed Transaction
- Microservice Internal Communication
- Connection Pool
- CI/CD Pipeline Steps
- How to Implement Microservices
- Interview Questions
Types of Architectural patterns
- Monolithic Architecture - all components are combined into one large single tiered application.
- Microservices Architecture - composed of small, loosely coupled, and independently deployable services.
- Layered (N-Tier) Architecture - Organizes an application into distinct layers (e.g., presentation, business logic, data access).
- Event-Driven Architecture - A design that centers around the production, detection, and reaction to events.
- Service-Oriented Architecture (SOA) - provide reusable business functionalities accessible over a network.
- Serverless Architecture - cloud provider dynamically manages the allocation of machine resources.
Service Registry.
a service maintains a database of other service instances info (name, IPs, ports)
Problem: As instances count goes up and down, we cannot track its ip/url/ports by hardcoding it.
Solution: To track those, we use registration and discovery pattern.
Steps:
1. Service Registration: Service registers its network details and metadata on startup.
2. Service Discovery: other Services query the registry to find the current network location of other services.
3. Health Monitoring: The registry at certain time interval(30sec) checks and removes non-responsive services.
Dependency:
Popular Service Registries:
1.Eurekha, 2.Consul, 3.Zookeeper, 4.Etcd.
Api Gateway
a service, act as a single entry point that manages, optimizes, and secures client requests to a system of microservices.
Functions: Routing, Load Balancing, Authentication and Authorization, Rate Limiting, Caching, Request and Response Transformation, Logging and Monitoring, security
Dependecny: spring-cloud-starter-gateway
Load balancing
distributing incoming network traffic across multiple servers
Dependency: spring-cloud-starter-loadbalancer
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
- Client-Side: client queries a service registry and do internal load-balancing algorithm and send request
Libraries: Spring Cloud LoadBalancer
- Server-side: client sends a request to load balancer server, in which uses load-balancing algorithm and forward request
Libraries: AWS Elastic Load Balancing (ELB)
Rate limiter pattern
its limit the number of request to microservice. (eg 5call at a time).
Fault tolerence
Microservices should be resilient, may have a fallback response.
Library: resilient4j in spring
Circuit Breakers:
Prevents retrying failures to a failing service after a threshold of failures.
States:
- Closed - if failure rates are below threshold
- Open - if failure rate are above threashold.
- Half-Open - happens after the wait duration. then repeats from above two condition.
Ex: Hystrix or Resilience4j.
Retry Mechanism:
Automatically retries failed requests a specified number of times before giving up.
Timeouts:
Sets a maximum wait time for a service response.
Fallbacks:
Provides alternative responses or actions when a service fails.
Service Mesh:
Manages service-to-service communication, including fault tolerance features.
Tools like Istio provide retries, circuit breakers, and observability.
Bulkheads:
Isolates different parts of the system to prevent failure in one part from affecting others.
Inbox-Outbox pattern.
ensure reliable message delivery and consistency between services,
Inbox pattern:
Outbox pattern: ensures that changes in the Outbox table and sending of messages (events) to other services.
The Twelve Factors
- Codebase - One codebase tracked in revision control, many deploys.
- Dependencies - Explicitly declare and isolate dependencies.
- Config - Store config in the environment
- Backing services - Treat backing services as attached resources.
- Build, release, run. - Strictly separate build and run stages
- Processes - Execute the app as one or more stateless processes
- Port binding - Export services via port binding
- Concurrency - Scale out via the process model
- Disposability - Maximize robustness with fast startup and graceful shutdown
- Dev/prod parity - Keep development, staging, and production as similar as possible
- Logs - Treat logs as event streams
- Admin processes - Run admin/management tasks as one-off processes
Security
JWT
Json web token
Format Structure: header.payload.signature
- Header - consists of type:jwt and algo.
- Payload - consists of emailid, createddate, roles, subject.
- Signature - signing header payload with public key.
Note: we need to pass JWT in header as key Authorization and value “Bearer tokenxxxxxxx”.
- Prometheus : For collecting and soring metrics.
- Grafana : for visualizing metrics.
Integretion with spring boot: using micrometer library, which export metrics from actuator to prometheus.
Other APM tools: AppDynamics, Datadog, Dynatrace, New Relic.
Alerting
- Prometheus alerting manager
- Third party APM alerts.
- Log Aggregation : ELK stack(Elastic search, logstash, kibana) to aggregate logs from multiple instances.
- Structed logging: JSON format to make logse easier to search and analyse.
Distributed tracing
- zipkin ui or Jaeger: use to track slower microservices, performance bottlneck and latency issues.
- Spring cloud sleuth: add trace and span id to logs automatically, easier to trace flow of requests.
Distributed transaction
1. Synchronous
- Two phase
client -> coordinator -> participants
a) corodinator query participants to vote yes or NO
b) based on above response, the coorodinator ask to commit or rollback
c) if acknoledgement fails again rollback will be intimated
- Three phase
extra pre commit phase, reduce the coordinator went down issue
2. Ashynchronus
Saga pattern
a) Choreography based - each service involved in the saga is responsible for triggering the next action in the process.
b) Orchestrator based - prefered, central coordinator (orchestrator) controls the transaction flow
Microservice internal communication
- Synchronous: Rest or gRPC
- Asynchronous: message brokers like kafka & RabbitMQ
Connection Pool
technique used to manage database connections efficiently by maintaining a pool of reusable connections
Http Connection Pool
technique used to manage http connections efficiently by maintaining a pool of reusable connections
Library: Hikari connection pool in spring boot
CI/CD pipeline steps
- Code commit
- CI -
a) when code pushed, CI kicks automatically
b) pull latest code and do sonarqube, unittest, integration test
c) if everything pass fine, otherwise notify the team
- Build - mvn clean install, and put jar inside docker image and store in registry(eg.jFrog, dockerhub)
- CD - kubernetest manifest(deployment.yml) define how app will be deployed in EKS. Then apply manifest.
- Monitor - Prometheus, Grafana, AWS CloudWatch
How to implement Microservices
- Define Microservices
- Identify Boundaries of each microservice based on business capabilities and domains.
- Decompose the Monolith into smaller, manageable services.
- Choose Technology Stack - Select frameworks that support microservices (e.g., Spring Boot for Java, Flask for Python).
- Design APIs: REST or gRPC
- Develop each Services:
- Implement Communication Mechanisms: sync or async
- Service Discovery and Registration (e.g., Eureka, Consul, Zookeeper)
- API Gateway (e.g., Netflix Zuul, Spring Cloud Gateway, NGINX).
- Configuration Management: (e.g., Spring Cloud Config, Consul).
- Implement Fault Tolerance
- Circuit Breakers: Resilience4j or Hystrix
- Retries and Timeouts: Implement retries and timeouts to handle transient failures.
- Fallbacks: Provide fallback methods to ensure graceful degradation of services.
- Logging and Monitoring
- Centralized Logging: Use the ELK stack (Elasticsearch, Logstash, Kibana) or Fluentd for aggregating logs.
- Distributed Tracing: ex: Zipkin or Jaeger with Spring Cloud Sleuth.
- Metrics and Monitoring: ex: Prometheus for collecting metrics and Grafana for visualizing them.
- Security: OAuth2 and JWT
- Continuous Integration and Continuous Deployment (CI/CD)
- Testing
- Unit Testing: Write unit tests for individual service components.
- Integration Testing: Ensure that services work together correctly.
- Contract Testing: Use tools like Pact to verify that services can communicate correctly based on predefined contracts.
- Documentation: Swagger/OpenAPI.
- Architecture Documentation ex: service dependencies, data flow, and design decisions.
Interview Questions:
- What if service registry goes down?
- what happen if change a value in config server, does my other services will have that change
- Where you store the docker images? ACR- amazon cloud registry