Java Microservices Interview Questions and Answers
1️⃣ What is a microservice?
A microservice is a small, independent, and loosely coupled service that performs a single business
function and communicates over network protocols like HTTP or messaging systems.
2️⃣ What are the advantages of microservices architecture?
• Scalability
• Independent deployment
• Fault isolation
• Technology diversity
• Better maintainability
3️⃣ How do microservices communicate?
• REST APIs (HTTP/HTTPS)
• Message brokers (Kafka, RabbitMQ)
• gRPC (for high-performance communication)
4️⃣ What is Spring Boot and why is it preferred?
Spring Boot simplifies Java application development by providing embedded servers, auto-
configuration, and minimal setup. It is well-suited for microservices because it reduces boilerplate code
and integrates well with Spring Cloud.
5️⃣ What is service discovery? Give examples.
Service discovery allows services to register and discover each other dynamically. Examples: Netflix
Eureka, Consul, Kubernetes DNS.
6️⃣ What is an API Gateway? Examples?
An API Gateway is the single entry point that routes requests, handles authentication, logging, rate
limiting, etc. Examples: Spring Cloud Gateway, Zuul, Kong.
7️⃣ How do you handle configuration in microservices?
• Spring Cloud Config Server
• Kubernetes ConfigMaps/Secrets
1
• Environment variables
8️⃣ What is a circuit breaker pattern?
It prevents cascading failures by stopping requests to a service that is consistently failing, allowing it
time to recover. Example libraries: Resilience4j, Hystrix.
9️⃣ How do you secure microservices?
• OAuth2 / OpenID Connect
• JWT tokens
• API Gateway for centralized security
• HTTPS for communication
1️⃣0️⃣ What is the Saga pattern?
A pattern for managing distributed transactions across microservices through local transactions and
compensating actions.
1️⃣1️⃣ What is eventual consistency?
A model where data updates propagate over time, and services become consistent eventually rather
than immediately.
1️⃣2️⃣ What is distributed tracing?
It tracks a request across multiple microservices. Tools: Zipkin, Jaeger, Spring Cloud Sleuth.
1️⃣3️⃣ What is the difference between monolithic and microservices architecture?
Monolith: Single unit, tight coupling, hard to scale. Microservices: Modular, loosely coupled, individually
scalable.
1️⃣4️⃣ How do you test microservices?
• Unit testing
• Integration testing
• Contract testing
• End-to-end testing
2
1️⃣5️⃣ How do you handle inter-service security?
• Mutual TLS
• Token-based authentication (JWT)
• API Gateway enforcement
1️⃣6️⃣ What is load balancing and how is it used?
Distributes traffic evenly across instances. Spring Cloud LoadBalancer, Ribbon, or Kubernetes handles it.
1️⃣7️⃣ How do you version microservices APIs?
• URI versioning (e.g., /api/v1/)
• Header versioning
• Content negotiation
1️⃣8️⃣ What are common pitfalls of microservices?
• Too many services increasing complexity
• Difficult debugging
• Overhead of network latency
1️⃣9️⃣ What is a config server in Spring Cloud?
Centralized external configuration management service where properties are stored (usually in Git).
2️⃣0️⃣ What is the difference between synchronous and asynchronous
communication?
Synchronous: client waits for response (REST). Asynchronous: client sends request and continues
(messaging queues).
2️⃣1️⃣ What tools do you use for monitoring microservices?
Prometheus, Grafana, Spring Boot Actuator, ELK Stack (Elasticsearch, Logstash, Kibana).
2️⃣2️⃣ What is the role of Docker in microservices?
Docker packages microservices as containers, ensuring consistency across environments and
simplifying deployment.
3
2️⃣3️⃣ How do you ensure data isolation in microservices?
Each service owns its database. No direct access to another service's database.
2️⃣4️⃣ What is a shared library in microservices?
A common library used across services (e.g., logging, utilities). Should be used carefully to avoid tight
coupling.
2️⃣5️⃣ How do you handle transactions spanning multiple microservices?
• Saga pattern
• Event sourcing
• Compensation logic
2️⃣6️⃣ What is eventual consistency vs strong consistency?
Eventual: data syncs over time. Strong: data is always consistent across nodes at any point.
2️⃣7️⃣ How do you migrate from monolith to microservices?
• Identify business domains
• Break down into services gradually
• Use the strangler fig pattern
2️⃣8️⃣ What is resilience in microservices?
Ability to handle and recover from failures without impacting the system. Achieved using retries, circuit
breakers, bulkheads.
2️⃣9️⃣ Explain the strangler pattern.
Gradually replace parts of a monolith with microservices, while old and new co-exist until the monolith
is fully replaced.
3️⃣0️⃣ How do you log in a distributed system?
Centralized logging using ELK Stack, Fluentd, or similar tools. Correlation IDs help trace requests.
4
3️⃣1️⃣ What is the bulkhead pattern?
Isolate components/resources so failure in one part does not bring down the entire system.
3️⃣2️⃣ How do you handle rate limiting?
API gateway or service-level configurations using tokens, leaky bucket algorithms.
3️⃣3️⃣ What are design considerations for microservices?
• Properly define service boundaries
• Avoid chatty communication
• Idempotent APIs
• Handle failure gracefully
3️⃣4️⃣ What are compensating transactions?
Undo operations in case part of a distributed transaction fails.
3️⃣5️⃣ How do you scale microservices?
• Horizontally by adding more instances
• Using container orchestration (Kubernetes, Docker Swarm)
• Load balancing