3. What is a microservice? Or explain microservices architecture?
Microservice architecture is an approach to software
development where an application is composed of several small, independent
services that can communicate over a network.
Each service is responsible for a specific functionality and can be developed,
deployed and scaled independently. Easier to scale individually.
microservices is offering high flexibility and real-time production
deployments.
Multiple loosely coupled services. For example, we can split into separate
services: authentication service, catalog service, order service, payment
service, stock service, transport service.
smaller codebases are easier to understand and modify.
different technologies can be used. one in java and another in .net.
Each microservice may have its own database. In this case,
data integrity is challenging.
saga pattern will be applied to manage distributed transactions, there
compensating transactions will be created instead of rollbacks.
Event-driven architecture can be applied.
Event sourcing : Non-blocking calls can be made. one service publishes
an event to a message broker. subscribing microservices will react
to these events and will provide data consistency and provide asynchronous
processing. Idempotency will be followed.
CQRS and command pattern : commands and queries are treated separately.
It uses API composition : to retrieve data from multiple services and present
it as a single response.
It may generate too many log files. it may require extra
resources to manage multiple systems. Use distributed tracing tools
zipkin, Jaeger, Kibana, , AppDynamics, etc. Use micrometer,
Prometheus and Grafana for real time monitoring. Use a spring boot
actuator to check health endpoints.
Use Logstash or ELK stack to centralize and collate logs.
it may have an API gateway
observer design pattern that is followed when consuming
asynchronous messages from kafka / RabbitMQ
API versioning will be used to manage complexity in coding.
Redis will be used to manage distributed caching.
Docker or Kubernetes will be used for deployment.
Provide fallback responses when services are unavailable. Circuit breakers
will prevent cascading failures. Failure in one service does not
affect others.
In-memory databases will be used to test microservices in isolation
Embedded servers will be used to apply CI/CD
We can do Horizontal, Vertical scaling, dynamic scaling.
You can enable load balancing with the help from kubernetes replica.
If you have plenty of microservices, you may need a service discovery pattern
Microservice follows several design patterns. saga, API composition, service
discovery, API Gateway, circuit breaker, Observability, Deployment