β 1. What are the key benefits and challenges of Microservices?Β
β 1. What are the key benefits and challenges of Microservices?Β
Synchronous (Request-Response)
REST APIs over HTTP (common)
gRPC (more efficient)
Asynchronous (Event-driven)
Message brokers like Kafka, RabbitMQ, ActiveMQ
REST: OrderService calls InventoryService via HTTP
Messaging: UserService publishes UserCreated event to Kafka
Automatically locating service instances in a dynamic environment (e.g., containers scaling up/down).
Client-side: Client queries the registry (e.g., Netflix Eureka)
Server-side: API Gateway or Load Balancer handles routing
Distributing traffic across multiple service instances.
Each service should have its own config, ideally externalized from code.
Spring Cloud Config Server β Centralized config management
HashiCorp Consul β KV store for config
Kubernetes ConfigMaps / Secrets
Environment Variables (12-factor app style)
Use profiles (dev, prod) and separate sensitive data (via secrets or vaults).
Used to prevent cascading failures when a dependent service is down or slow.
Monitor failure rate of calls to a service.
If failures exceed a threshold, "open" the circuit.
During open state, return fallback or error immediately.
Retry after a cooldown period.
β 6. How would you secure inter-service communication?Β
Use short-lived JWTs signed by a trusted auth service.
Use mTLS for highly sensitive internal traffic.
Rotate secrets and certs securely (e.g., Vault).
In microservices, a single logical transaction may span multiple services, but XA (2PC) is discouraged (complex, slow).
β 1. Saga Pattern (preferred)
Sequence of local transactions with compensating actions for rollback.
Coordination:
Choreography: Services emit events and listen to each other.
Orchestration: Central coordinator service manages steps.
β 2. Eventual Consistency
Accept temporary inconsistency.
Sync using reliable messaging (Kafka, RabbitMQ).