1. How do you design distributed transactions in microservices?

Monolithic systems allow ACID transactions, but microservices require new patterns:

Solutions:

  • Saga Pattern (most popular)

    • Choreography: events trigger next steps

    • Orchestration: central coordinator

  • Event Sourcing → store state changes as events

  • Compensating transactions instead of rollback

  • 2PC (Two-Phase Commit) → rare because of tight coupling

Trade-offs:
Consistency vs Availability → CAP theorem.


2. Explain Saga Pattern with a real example.

Example: Order Service → Payment → Inventory

Choreography:

  • OrderCreated ➜ PaymentService

  • PaymentCompleted ➜ InventoryService

  • InventoryFailed ➜ PaymentService issues refund

Orchestration:
A central Order Orchestrator calls each service and manages the flow.

Saga ensures eventual consistency across distributed DBs.


3. What is a service mesh and why do large companies use it?

A service mesh manages microservice-to-microservice communication outside the application code.

Features:

  • mTLS (encryption)

  • Automatic retries

  • Traffic shaping

  • Observability

  • Circuit breakers

  • Sidecar proxies

Tools: Istio, Linkerd, Consul Connect
Why used: Offload networking concerns from developers.


4. Explain the Sidecar Pattern.

A helper container runs alongside the main application container in the same Pod.

Uses:

  • Logging

  • Networking proxy (Envoy)

  • Monitoring agents

  • Security policies

Used heavily in service mesh architectures.


5. Explain CAP theorem in microservices context.

In distributed systems, you can choose only two out of:

  • Consistency

  • Availability

  • Partition tolerance (must tolerate network issues)

Microservices typically choose:

  • AP (eventually consistent)
    or

  • CP (consistent but may reject requests)


6. What is eventual consistency and when do you use it?

Data is not immediately consistent but becomes consistent after some time.

Used in:

  • Event-driven systems

  • Multi-database microservices

  • Distributed caches

  • High-traffic systems like Amazon, Netflix


7. How do you design a highly scalable microservices architecture?

Key components:

  • API Gateway

  • Auto-scaling containers (HPA in Kubernetes)

  • Database-per-service

  • Event-driven communication

  • Distributed cache (Redis, Memcached)

  • Observability stack

  • Service mesh

  • Stateless services


8. How do you secure microservices communication?

Techniques:

  • mTLS for encrypted communication

  • OAuth2.0

  • JWT

  • API keys

  • IAM roles (in cloud)

  • Secret managers (Vault, AWS Secrets Manager)

Zero-trust networks prefer mutual TLS.


9. What is zero-trust architecture?

Every request must be authenticated and authorized, even inside the network.

Principles:

  • No implicit trust

  • Validate identity for every request

  • Strict access control

  • Least privilege

Used with service mesh.


10. What is the Strangler Fig Pattern?

Used for migrating monolithic to microservices.

Steps:

  • Add API Gateway

  • Gradually route some functionality to new microservices

  • Slowly remove the old monolithic parts

  • Replace piece-by-piece


11. What is Backpressure and why is it important?

Backpressure is a mechanism that slows down producers when consumers cannot handle load.

Used in:

  • Kafka Streams

  • Reactive programming

  • High-throughput systems

Prevents system overload.


12. What is Bulkhead Pattern?

Divide system resources into isolated pools so failure in one does not impact others.

Example:
Payment service issues should not crash order service.


13. What is the difference between Orchestration vs Choreography?

OrchestrationChoreography
Central controllerNo central controller
Simpler to manageMore decoupled
Easier troubleshootingCan become chaotic
Saga orchestratorEvents trigger next step

14. How do you handle versioning in microservices?

Methods:

  • URL versioning → /v1/users

  • Header-based versioning

  • Backward-compatible APIs

  • Canary releases

  • Rolling deployments

Avoid breaking changes.


15. How do you avoid microservices becoming a distributed monolith?

  • Do not share DB

  • Avoid excessive synchronous calls

  • Use events instead of REST

  • Decouple services

  • Use API Gateway


16. What is Domain-Driven Design (DDD) and how it fits microservices?

Microservices align with bounded contexts in DDD.

Helps:

  • Define clear service boundaries

  • Avoid over-sharing models

  • Improve understanding of each domain


17. What is a bounded context?

A logical boundary in a domain where a specific model applies.

Example:
"Order" in Order Service ≠ "Order" in Inventory Service.


18. How do you handle data replication across microservices?

Options:

  • Event sourcing

  • Change Data Capture (CDC)

  • Streaming via Kafka

  • Eventual consistency

  • Database replication tools (Debezium)


19. What is CQRS and when is it used?

Command Query Responsibility Segregation.

Use it when:

  • Read workload differs from write workload

  • Event sourcing is used

  • High-performance queries required

Decouples read and write models.


20. What is Event-Driven Architecture (EDA)?

Microservices communicate via events.

Benefits:

  • Loose coupling

  • Async

  • Scalable

  • Reliable

Tools: Kafka, SNS/SQS, EventBridge.


21. What is a Distributed Cache? Why used in microservices?

Cache across multiple nodes.

Examples: Redis, Hazelcast, Memcached.

Used for:

  • Performance

  • Reducing DB load

  • Session sharing

  • Rate limiting


22. How do you handle API throttling and rate limiting?

Techniques:

  • Token bucket algorithm

  • Leaky bucket

  • API Gateway throttling

  • Redis counters

Protects services under heavy load.


23. What is a Dead Letter Queue (DLQ)?

Messages that cannot be processed are sent to DLQ.

Helps:

  • Debugging

  • Retrying

  • Ensuring system stability


24. What is Transactional Outbox Pattern?

Ensures event publishing is reliable when using separate DB and message broker.

Process:

  1. Store event in DB

  2. Another process publishes it

  3. Mark as sent

Guarantees no lost events.


25. Explain Idempotent Consumer.

A consumer that processes the same event multiple times safely.

Used when:

  • Message duplication occurs

  • Retrying events
    Ensures data consistency.


26. What are the common microservice failure patterns?

  • Cascading failure

  • Retry storm

  • Thundering herd

  • Network partition

Use circuit breakers, timeouts, and queues to mitigate.


27. How do you design a resilient microservices system?

  • Circuit breakers

  • Retries

  • Bulkheads

  • Timeouts

  • Load balancing

  • Horizontal scaling

  • Distributed tracing


28. What is API Composition Pattern?

API Gateway aggregates responses from multiple microservices and sends a single response to the client.

Needed when:

  • UI requires data from many services

  • Reduce round trips


29. What is the difference between Monorepo and Polyrepo for microservices?

Monorepo:

  • All microservices in one repo

  • Easier dependency management

  • Harder CI/CD pipelines

Polyrepo:

  • Each service in its own repo

  • More autonomy

  • Harder coordination


30. How do you design microservices for millions of users?

High-level design:

  • API Gateway + Load balancer

  • Distributed cache (Redis)

  • Message brokers (Kafka)

  • Auto-scaling (K8s HPA)

  • Multi-region deployment

  • Service mesh for secure communication

  • Observability stack (Prometheus + Grafana + Jaeger)

  • Event-driven architecture

  • Stateless services

  • CDN for static assets