Microservices Communication and Distributed System Patterns
How Microservices Communicate
Section titled “How Microservices Communicate”Microservices communicate using two primary styles.
Synchronous Communication
Section titled “Synchronous Communication”One service calls another directly and waits for a response.
Common technologies:
- REST APIs (HTTP/HTTPS)
- gRPC
- Spring Cloud OpenFeign
Example flow
flowchart LR A[Order Service] -->|HTTP/Feign| B[Payment Service] B --> A
Feign client example
@FeignClient(name = "payment-service")public interface PaymentClient {
@PostMapping("/payments") PaymentResponse processPayment(PaymentRequest request);}Advantages
- Simple
- Immediate response
Disadvantages
- Tight coupling
- Dependent on downstream availability
Asynchronous Communication
Section titled “Asynchronous Communication”Services communicate through a message broker.
Common technologies:
- Kafka
- RabbitMQ
- ActiveMQ
- AWS SQS
flowchart LR A[Order Service] -->|Publish Event| K[(Kafka)] K --> I[Inventory Service] K --> P[Payment Service] K --> N[Notification Service]
Advantages
- Loose coupling
- High scalability
- Better fault tolerance
Disadvantages
- Eventual consistency
- More difficult debugging
Synchronous vs Asynchronous Communication
Section titled “Synchronous vs Asynchronous Communication”| Feature | Synchronous | Asynchronous |
|---|---|---|
| Response | Immediate | Later |
| Dependency | High | Low |
| Coupling | Tight | Loose |
| Scalability | Lower | Higher |
| Example | REST API | Kafka Event |
Event-Driven Microservices
Section titled “Event-Driven Microservices”In an event-driven architecture, services communicate by publishing and consuming events.
flowchart TD A[Order Created] --> B[(Kafka Topic)] B --> C[Inventory Service] B --> D[Payment Service] B --> E[Notification Service]
Kafka Producer
Section titled “Kafka Producer”@Autowiredprivate KafkaTemplate<String, String> kafkaTemplate;
public void publishOrderEvent(String order) { kafkaTemplate.send("order-topic", order);}Kafka Consumer
Section titled “Kafka Consumer”@KafkaListener(topics = "order-topic", groupId = "inventory-group")public void consume(String message) { System.out.println("Received event: " + message);}Benefits:
- Decoupled services
- Scalable
- Fault tolerant
API Composition Pattern
Section titled “API Composition Pattern”API Composition aggregates data from multiple services into a single response.
flowchart TD Client --> Composer[API Composer] Composer --> Order Composer --> Payment Composer --> Product Order --> Composer Payment --> Composer Product --> Composer Composer --> Client
Example response:
{ "orderId": 123, "product": "Laptop", "paymentStatus": "Success"}Often implemented using an API Gateway or Backend-for-Frontend (BFF).
Distributed Configuration
Section titled “Distributed Configuration”Spring Cloud Config centralizes configuration management.
flowchart TD Git[(Git Repository)] --> Config[Config Server] Config --> Order Config --> Payment Config --> Inventory
Configuration example:
spring.cloud.config.uri=http://config-server:8888Benefits:
- Centralized configuration
- Environment-specific configuration
- Dynamic refresh
Centralized Logging
Section titled “Centralized Logging”A common implementation uses the ELK stack.
- Elasticsearch
- Logstash or Filebeat
- Kibana
flowchart TD Services[Microservices] --> Logstash Logstash --> Elasticsearch Elasticsearch --> Kibana
With Filebeat shipping logs from each service instance instead of services writing to Logstash directly:
flowchart TD
S1[Service 1]
S2[Service 2]
S3[Service 3]
S1 --> F[Filebeat]
S2 --> F
S3 --> F
F --> L[Logstash]
L --> E[Elasticsearch]
E --> K[Kibana]
Application logging code:
private static final Logger log = LoggerFactory.getLogger(ProductService.class);
log.info("Product created {}", productId);Example JSON log:
{ "timestamp": "2026-03-16", "service": "order-service", "traceId": "abc123", "message": "Order created"}Distributed tracing tools: OpenTelemetry, Zipkin, Jaeger — see the Spring Boot production monitoring and distributed tracing guide for TraceId/SpanId details.
Related Topics Covered Elsewhere
Section titled “Related Topics Covered Elsewhere”This site already covers several related patterns in more depth — see:
- Resilience patterns (Circuit Breaker, Retry, Timeout, Bulkhead)
- Idempotency and duplicate payment handling
- Saga pattern, choreography vs orchestration
- Kafka duplicate message handling
Interview Focus Areas
Section titled “Interview Focus Areas”For senior Spring Boot and microservices interviews, be comfortable discussing:
- Resilience4j
- Kafka
- Saga Pattern
- ELK Stack
- Spring Cloud Config
- API Gateway
- Event-driven architecture
- Idempotency
- Distributed transactions
Key Takeaways
Section titled “Key Takeaways”- Choose synchronous communication for immediate request/response scenarios and asynchronous communication for scalable, loosely coupled systems.
- Event-driven architecture improves scalability and resilience through messaging platforms such as Kafka.
- API Composition aggregates multiple service calls behind a single API Gateway or BFF response.
- Centralized configuration (Spring Cloud Config) and centralized logging (ELK) are fundamental operational capabilities for production microservices.