We will discuss some critical interview questions related to Microservices.
1. How Do Microservices Communicate With Each Other?
Microservices can communicate with each other using either:
- Synchronous protocols like HTTP/REST
- Asynchronous protocols like RabbitMQ, Kafka
| Communication Type | Description | Example Tools | Protocol |
|---|---|---|---|
| Synchronous | Real-time, blocking | REST, gRPC, Feign | HTTP, gRPC |
| Asynchronous | Event-driven, non-blocking | Kafka, RabbitMQ | AMQP, Kafka |
So what are the differences between them?
| Feature | REST (Synchronous) | Messaging (Asynchronous) |
|---|---|---|
| Timing | Real-time | Delayed, eventually consistent |
| Coupling | Tightly coupled | Loosely coupled |
| Failure handling | Retry logic or circuit breaker | Message retries, dead letter queues |
| Tools | Feign, RestTemplate | Kafka, RabbitMQ |
Rest Template Examples
@Service
public class ProductClient {
private final RestTemplate restTemplate = new RestTemplate();
public String getProductById(Long productId) {
String url = "http://localhost:8081/products/" + productId;
return restTemplate.getForObject(url, String.class);
}
}
Using Feign Client
@FeignClient(name = "product-service")
public interface ProductClient {
@GetMapping("/products/{id}")
String getProduct(@PathVariable("id") Long id);
}
2. How Can You Secure Microservices?
We can use OAuth2, JWT or API Keys to secure microservices.
JWT Example
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated())
.oauth2ResourceServer(oauth2 -> oauth2.jwt());
return http.build();
}
}
3. What Are the Key Differences Between Monolithic and Microservices Architectures?
| Feature | Monolithic Architecture | Microservices Architecture |
|---|---|---|
| Codebase | Single unified codebase | Multiple small, independent codebases |
| Deployment | Entire application deployed at once | Each service can be deployed independently |
| Scalability | Hard to scale specific features | Easy to scale individual services |
| Technology Stack | Typically a single tech stack | Can use different stacks per service |
| Fault Isolation | One failure can crash the entire app | Failures isolated to individual services |
4. What Are the Common Microservices Patterns?
| Pattern | Purpose | Example Library |
|---|---|---|
| API Gateway | Entry point for requests | Spring Cloud Gateway |
| Circuit Breaker | Fail fast on repeated failures | Resilience4j |
| Service Discovery | Dynamic registration and discovery | Eureka, Consul |
| Configuration Server | Central config management | Spring Cloud Config |
| Saga Pattern | Distributed transaction management | Kafka-based Orchestration |
5. What Are the Different Ways to Handle Fault Tolerance?
| Strategy | Description | Tools |
|---|---|---|
| Retry | Re-attempt failed calls | Spring Retry |
| Circuit Breaker | Prevent repeated failures | Resilience4j |
| Bulkhead | Isolate service failures | Resilience4j |
| Fallback | Provide default response | Resilience4j, Hystrix |
Circuit Breaker with Fallback Example:
@CircuitBreaker(name = "productService", fallbackMethod = "fallback")
public String callProductService() {
return restTemplate.getForObject("http://localhost:8081/products", String.class);
}
public String fallback(Throwable t) {
return "Default product response";
}



