What is the difference between Synchronous and Asynchronous communication in microservices?
Topic: microservices, Communication. Level: Basic.
Answer
- **Synchronous**: the caller sends a request and blocks until it receives a response (e.g. REST over HTTP, gRPC). Simple to reason about, but the caller's availability is now coupled to the callee's — if the downstream is slow or down, the caller suffers latency or failure. Chains of sync calls multiply latency and create cascading-failure risk.
- **Asynchronous**: the caller emits a message/event and does not wait (e.g. Kafka, RabbitMQ). Services are temporally decoupled — the consumer can process later. Improves resilience and scalability, but adds eventual consistency, ordering, and debugging complexity.
Rule of thumb: use sync for queries needing an immediate answer; use async for commands/events where the result can be processed in the background.
```java
// SYNCHRONOUS — caller blocks for the response
OrderResponse resp = restClient.post()
.uri("http://payment-service/charge")
.body(chargeRequest)
.retrieve()
.body(OrderResponse.class); // blocks here
// ASYNCHRONOUS — fire an event and move on
kafkaTemplate.send("order.placed", new OrderPlacedEvent(orderId, amount));
// payment-service consumes it whenever it can
```
