1. What are the main benefits of microservices architecture?
Answer:
Microservices offer modularity, scalability, and independent deployment. We adopted it to separate concerns by business capability, which allows individual teams to own, scale, and deploy independently.
This improves fault isolation, enables CI/CD pipelines, and aligns with agile delivery.
2. How do microservices communicate?
Answer:
Services communicate synchronously using REST or gRPC for request-response, and asynchronously using message brokers like Kafka or RabbitMQ.
For loosely coupled systems and eventual consistency, async communication is preferred. For real-time needs like authentication or inventory checks, sync calls are ideal.
3. How do you ensure consistency across distributed microservices?
Answer:
We use eventual consistency with patterns like Saga or Outbox. Each service performs local transactions and emits events.
For example, in an order service, we save the order and publish an "OrderCreated" event. Other services (like inventory or payment) react accordingly.
4. What is the Saga pattern?
Answer:
Saga is a pattern to manage distributed transactions without locking resources. It breaks a global transaction into a series of local transactions with compensating actions in case of failure.
In Java, this is often implemented using event choreography (Kafka) or orchestration (Camunda, Temporal).
5. What are the challenges in microservices?
Answer:
To address these, we use tools like Spring Cloud Netflix, OpenTelemetry, OAuth2 with JWT, and Kafka for async communication.
6. How do you handle service discovery?
Answer:
We use Spring Cloud Netflix Eureka or Consul. Each service registers itself and queries others via a discovery client.
In Kubernetes, DNS-based discovery is preferred using services and endpoints.
7. How do you manage configurations in microservices?
Answer:
Centralized config using Spring Cloud Config Server or HashiCorp Vault allows dynamic refresh and secure secret management.
Profiles help us manage environment-specific settings cleanly.
8. What is the role of API Gateway in microservices?
Answer:
API Gateway (e.g., Spring Cloud Gateway, Kong, or Zuul) acts as a single entry point, handling routing, authentication, rate limiting, request transformation, and load balancing.
It also helps hide internal microservices and reduce client complexity.
9. How do you secure microservices?
Answer:
We use OAuth2 and JWT tokens for authentication, with a central identity provider (e.g., Keycloak or Auth0).
Services validate tokens locally using public keys (JWKs). Communication is secured using mutual TLS (mTLS) or HTTPS.
10. How do you monitor and trace microservices?
Answer:
We use the ELK stack for logs, Prometheus + Grafana for metrics, and Jaeger or Zipkin for tracing.
Each service is instrumented using OpenTelemetry and correlation IDs are passed across services for end-to-end visibility.
11. How do you version microservices APIs?
Answer:
Through URI versioning (/v1/products
), or header-based versioning. We ensure backward compatibility and sunset old versions gradually.
12. What database strategies do you use in microservices?
Answer:
Each microservice has its own schema or database (Database per Service).
For queries across services, we use CQRS, event sourcing, or materialized views updated by consuming events.
13. How do you handle deployment of multiple microservices?
Answer:
We use Docker for packaging and Kubernetes for orchestration. Helm charts help manage config.
CI/CD pipelines are built with GitHub Actions or Jenkins for automated test, build, and rollout.
14. What’s the difference between orchestration and choreography in microservices?
Answer:
-
Orchestration: One service (like a workflow engine) controls the flow (e.g., Camunda, Temporal.io).
-
Choreography: Services listen for events and react independently (Kafka, RabbitMQ).
I prefer choreography for loosely coupled, scalable designs; orchestration for long-running transactions needing coordination.
15. How do you deal with latency and timeouts in microservices?
Answer:
By setting proper timeouts, circuit breakers (Resilience4j), bulkheads, and retries.
Caching is introduced at service or gateway level for read-heavy workloads.
✅ 1. CQRS – Command Query Responsibility Segregation
💡 What is it?
CQRS separates the read and write sides of your application into distinct models.
-
Command Model → Handles writes (create, update, delete)
-
Query Model → Handles reads (optimized for fast access)
🧠 Why Use CQRS?
“We chose CQRS because we needed to scale reads and writes differently and optimize for complex queries without compromising transactional integrity.”
✅ When to Use
-
Complex domains with frequent read/write operations
-
High scalability or performance requirements
-
Read side requires denormalized or aggregated data
❌ When Not to Use
🛠️ Spring Implementation
-
Use Spring Data JPA for command model
-
Use native SQL, projections, or NoSQL for query model
-
Optional: Use Axon Framework for full CQRS+Event Sourcing support
✅ 2. Saga Pattern – Distributed Transaction Coordination
💡 What is it?
A Saga is a sequence of local transactions where each step publishes an event that triggers the next. If a failure occurs, compensating transactions are invoked.
🧠 Why Use Saga?
“We implemented the Saga pattern to manage distributed transactions across microservices without requiring a global lock or 2PC. It allowed each service to remain autonomous.”
✅ When to Use
🧱 Two Types
✅ 3. Orchestration vs Choreography
⚖️ Orchestration
-
Central controller (e.g., BPMN engine like Camunda, Temporal)
-
Clear visibility and coordination
-
Tightly coupled to the orchestrator
⚡ Choreography
🛠️ Java/Spring Implementation Patterns
📌 Saga + Orchestration Example (Spring Boot + Camunda):
📌 Saga + Choreography Example (Spring Boot + Kafka):
-
OrderService: emits OrderCreated
-
InventoryService: listens → emits InventoryReserved
-
PaymentService: listens → emits PaymentProcessed
-
Failure handlers: emit OrderCancelled
and trigger compensating logic
⚠️ Trade-Offs
Pattern | Pros | Cons |
---|
CQRS | Read/write separation, optimized models | Complexity, eventual consistency |
Saga | Distributed consistency, resilience | Harder to debug, requires compensations |
Orchestration | Clear control, auditability | Central point of failure, tight coupling |
Choreography | Decoupled services, scalable | Complex flows, hard to monitor |