Microservices & System Design Interview Questions
1. Difference between Monolithic vs. Microservice
- Monolithic: A single, unified unit where all components (UI, Business Logic, Data Access) are packaged and deployed together.
- Microservice: An architectural style where an application is structured as a collection of loosely coupled, independently deployable services.
a. Advantages and disadvantages: * Monolith Pros: Simple to develop/test/deploy initially, easier debugging, no network latency between calls. * Monolith Cons: Hard to scale (must scale whole app), tight coupling, technology lock-in, long build times. * Microservice Pros: Independent scaling, technology diversity (polyglot), fault isolation, faster time-to-market for individual features. * Microservice Cons: Complexity (distributed system), network latency, difficult debugging/tracing, data consistency challenges (CAP).
2. What is cascading failure? How to prevent this failure?
- Cascading Failure: When a failure in one service triggers a chain reaction of failures in other services that depend on it, potentially bringing down the entire system.
- Prevention:
- Circuit Breaker Pattern: Detect failures and temporarily stop sending requests to the failing service (fail fast).
- Timeouts: Set strict timeouts for network calls.
- Bulkhead Pattern: Isolate resources (thread pools) so one failing feature doesn't consume all resources.
3. What is fault tolerance? How to make your microservice fault tolerance?
- Fault Tolerance: The ability of a system to continue operating properly in the event of the failure of some of its components.
- How to achieve:
- Resilience Patterns: Use libraries like Resilience4j or Hystrix (Circuit Breaker, Retry, Rate Limiter).
- Redundancy: Run multiple instances of services.
- Graceful Degradation: Return a fallback response (default value or cached data) instead of an error.
4. How do microservices communicate?
- Synchronous: HTTP/REST (JSON), gRPC (Protobuf). Used when an immediate response is required.
- Asynchronous: Message Queues (RabbitMQ, Kafka). Used for decoupling and when immediate processing isn't critical (fire-and-forget).
5. What is Swagger?
- Swagger (OpenAPI): A toolset for designing, building, documenting, and consuming RESTful web services. It provides a UI (
swagger-ui) to visualize and interact with the API's resources without having any of the implementation logic in place.
6. How do you monitor your application?
- Metrics: Prometheus (collects metrics) + Grafana (visualizes dashboards).
- Health Checks: Spring Boot Actuator (
/actuator/health). - Logs: ELK Stack (Elasticsearch, Logstash, Kibana) or Splunk.
- Tracing: Zipkin or Jaeger (with Spring Cloud Sleuth/Micrometer) to trace requests across microservices.
7. Why do we need a gateway? Is a gateway necessary?
- API Gateway: An entry point for all clients. It acts as a reverse proxy.
- Why need it?
- Routing: Directs requests to the appropriate service.
- Security: Centralized Authentication/Authorization (SSL termination).
- Cross-cutting concerns: Rate limiting, logging, monitoring.
- Protocol Translation: HTTP to gRPC, etc.
- Is it necessary? Not strictly for small apps, but highly recommended for complex microservices to hide internal architecture and provide a unified interface.
8. How many environments can your application have?
Typically 4: 1. DEV (Development): Where developers code and test. 2. QA/TEST: For Quality Assurance team to run manual/automated tests. 3. UAT/STAGING: Pre-production environment that mirrors Prod. For User Acceptance Testing. 4. PROD (Production): Live environment for end users.
9. How did you deploy your application? (CI/CD Pipeline)
Flow: 1. Code: Developer pushes code to GitHub/GitLab. 2. Build (CI): Jenkins/GitHub Actions triggers a build (Maven/Gradle), runs unit tests. 3. Image: Docker image is built and pushed to Docker Hub/ECR. 4. Deploy (CD): Kubernetes (K8s) pulls the image and updates the pods on AWS EKS or EC2.
10. Usage of Jenkins.
- Jenkins is an open-source automation server used for CI/CD.
- It automates parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery.
- Pipelines: Defined in
Jenkinsfile(Declarative or Scripted) to orchestrate the steps.
11. Describe your microservice architecture.
- Client (Web/Mobile) -> Load Balancer -> API Gateway (Zuul/Spring Cloud Gateway).
- Gateway talks to Discovery Service (Eureka) to find service locations.
- Auth Service (OAuth2/JWT) validates identity.
- Business Services (Order, Product, User) handle logic.
- Config Server provides externalized configuration.
- Message Broker (RabbitMQ/Kafka) handles async communication.
- Database: Each service has its own DB (Database per service pattern).
- Monitoring: Prometheus/Grafana/Zipkin.
12. How to debug your microservice? (Troubleshooting)
- Distributed Tracing: Use Trace ID and Span ID (via Spring Cloud Sleuth/Micrometer) to track a request flow across services in Zipkin.
- Centralized Logging: Check aggregated logs in ELK/Splunk using the Trace ID.
- Metrics: Check Grafana for spikes in latency or error rates.
13. How do you implement async in web applications?
- @Async Annotation: In Spring, mark a method as
@Asyncto run it in a separate thread. - CompletableFuture: For composing asynchronous non-blocking logic.
- Message Queues: Send a message to RabbitMQ/Kafka and return immediately to the user. A consumer processes it in the background.
14. What is RabbitMQ and what can it help us to achieve in a web application?
- RabbitMQ: An open-source message broker software (implements AMQP).
- Benefits:
- Decoupling: Producer and Consumer don't need to know about each other.
- Asynchronous Processing: Offload heavy tasks (e.g., sending emails, generating reports).
- Load Smoothing: Handle traffic spikes by buffering messages.
15. What are the components of RabbitMQ? Describe the role of each component.
- Producer: Sends messages.
- Exchange: Receives messages from producers and routes them to queues based on rules.
- Queue: Stores messages until they are processed.
- Binding: Link between Exchange and Queue.
- Consumer: Receives and processes messages from the queue.
16. What are different types of exchanges that exist in RabbitMQ? Explain each.
- Direct: Routes to queue with exact matching Routing Key.
- Fanout: Broadcasts to all bound queues (ignores routing key).
- Topic: Routes based on pattern matching (wildcards
*and#) of the routing key. - Headers: Routes based on message header attributes.
17. What is a dead letter exchange (DLX)?
- An exchange where messages are redirected if they cannot be processed (e.g., queue full, message rejected, TTL expired). It allows handling "bad" messages separately (logging, alerting) without blocking the main queue.
18. How to secure your endpoint? (How to check if a HTTP call is valid?)
- JWT (JSON Web Token):
- Client logs in via Auth Service -> gets JWT.
- Client sends JWT in
Authorization: Bearer <token>header. - API Gateway or Resource Server intercepts request.
- Validates signature and expiration of JWT using the public key/secret.
- Extracts roles/claims to authorize access.
- HTTPS: Encrypt transport layer.
19. Where do you store your configuration file when you use microservices?
- Spring Cloud Config Server: A centralized service that manages configurations for all microservices across all environments.
- Backed by a Git Repository (version control for configs).
- Services fetch their config at startup.
20. How did you do user authorization in microservices?
- Role-Based Access Control (RBAC):
- Roles are embedded in the JWT claims (e.g.,
roles: ["ADMIN", "USER"]). - In the microservice, use Spring Security annotations:
@PreAuthorize("hasRole('ADMIN')").
21. Vertical Scaling and Horizontal scaling in your application
- Vertical Scaling (Scale Up): Adding more power (CPU, RAM) to the existing machine. Limit: Hardware capacity.
- Horizontal Scaling (Scale Out): Adding more instances of the application (nodes) to the pool. Preferred for microservices (cloud-native).
22. Tell me about your experience with Cloud Service. Ex. AWS, GCP, Azure
- AWS (Amazon Web Services):
- EC2: Virtual servers.
- S3: Object storage (files, images).
- RDS: Managed Relational Database.
- EKS: Managed Kubernetes.
- Lambda: Serverless computing.
23. What is Kafka? What is Kafka Stream?
- Kafka: A distributed event streaming platform. High throughput, scalable, durable. Used for building real-time data pipelines.
- Kafka Streams: A client library for building applications and microservices, where the input and output data are stored in Kafka clusters. It allows processing/transforming data in real-time.
a. What are the components of Kafka? * Producer: Publishes events. * Consumer: Subscribes to events. * Broker: A Kafka server. * Topic: Category/feed name to which records are stored. * Partition: Topics are split into partitions for scalability. * ZooKeeper: Manages cluster metadata (being replaced by KRaft).
b. Purpose of partition & multiple consumers? * Partition: Allows a topic to scale beyond one server. Parallel processing. * Multiple Consumers: Yes, via Consumer Groups. Within a group, each partition is consumed by exactly one consumer (parallelism). Multiple groups can consume the same topic independently (pub/sub).
24. What is ELK?
- Elasticsearch: Search and analytics engine (stores logs).
- Logstash: Server-side data processing pipeline (ingests/transforms logs).
- Kibana: Visualization tool (dashboard for logs).
- Used for centralized logging and analysis.
25. Explain distributed database management (2-phase commit, SAGA)
- 2-Phase Commit (2PC): A blocking protocol to ensure strong consistency. (Prepare Phase -> Commit Phase). Slow, not suitable for microservices.
- SAGA: A sequence of local transactions. Each local transaction updates the DB and publishes an event to trigger the next local transaction. If one fails, Compensating Transactions are executed to undo changes.
26. What is Event Driven development?
- An architecture where components communicate by producing and consuming events (state changes).
- Decoupled, asynchronous, scalable.
- Example: "OrderPlaced" event triggers "InventoryService" and "EmailService".
27. How do you use SAGA to achieve transaction management in a distributed system?
- Choreography: Services subscribe to events and decide what to do. Decentralized. (Good for simple flows).
- Orchestration: A central Orchestrator (Service) tells participants what to do. (Good for complex flows).
28. Explain your Microservices from end to end, how does a request go from frontend to database and come back?
- User clicks button -> Frontend sends HTTP Request.
- Request hits Load Balancer -> Forwards to API Gateway.
- Gateway validates JWT (Auth) and routes to Order Service.
- Order Service checks cache (Redis). If miss, calls Inventory Service (via Feign/WebClient).
- Inventory Service queries its Database. Returns result.
- Order Service saves order to its Database.
- Order Service publishes message to RabbitMQ (for Email).
- Response flows back: Service -> Gateway -> LB -> Frontend.
29. Explain the components needed when designing a Microservices application.
- Containerization: Docker.
- Orchestration: Kubernetes.
- API Gateway: Routing/Security.
- Service Discovery: Eureka/Consul.
- Config Server: Centralized config.
- Circuit Breaker: Fault tolerance.
- Distributed Tracing: Zipkin/Sleuth.
- Centralized Logging: ELK.
- Message Broker: RabbitMQ/Kafka.