Skip to content

Microservices Communication and Distributed System Patterns

Microservices communicate using two primary styles.

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

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
Feature Synchronous Asynchronous
Response Immediate Later
Dependency High Low
Coupling Tight Loose
Scalability Lower Higher
Example REST API Kafka Event

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]
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void publishOrderEvent(String order) {
kafkaTemplate.send("order-topic", order);
}
@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 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).

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:8888

Benefits:

  • Centralized configuration
  • Environment-specific configuration
  • Dynamic refresh

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.

This site already covers several related patterns in more depth — see:

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
  • 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.