π‘ Sync vs Async Communication
Microservices Communication Patterns β Complete Visual Guide
π§ Mental Model: Phone Call vs Email
π SYNC = Phone Call (wait, immediate, both available)
βοΈ ASYNC = Email (no wait, eventual, independent)
π― Think of calling vs emailing β the same principles apply to microservices!
π SYNC
Request-Response Pattern
ββββββββββ ββββββββββ
β Client ββββββΊβ Server β
ββββββββββ ββββββββββ
β² β
βββββββWaitββββββ
β±οΈ Client blocks until response
ββββββββββ ββββββββββ
β Client ββββββΊβ Server β
ββββββββββ ββββββββββ
β² β
βββββββWaitββββββ
β±οΈ Client blocks until response
REST API / gRPC Example
GET /api/orders/123 β 200 OK { "status": "completed" }
REST
gRPC
GraphQL
WebSocket
βοΈ ASYNC
Event-Driven Pattern
ββββββββββ ββββββββββββ ββββββββββββ
βProducerββββββΊβ Message ββββββΊβ Consumer β
ββββββββββ β Broker β ββββββββββββ
ββββββββββββ
β Producer continues immediately
ββββββββββ ββββββββββββ ββββββββββββ
βProducerββββββΊβ Message ββββββΊβ Consumer β
ββββββββββ β Broker β ββββββββββββ
ββββββββββββ
β Producer continues immediately
Kafka / RabbitMQ Example
topic: "order-events" β partition 0 β consumer group
Kafka
RabbitMQ
SQS
Redis
| Factor | SYNC | ASYNC |
|---|---|---|
| Latency | π’ Low (immediate) | π‘ Higher (event delay) |
| Coupling | π΄ Tight | π’ Loose |
| Scalability | π‘ Limited (thread bound) | π’ High (decoupled) |
| Consistency | π’ Strong | π‘ Eventual |
| Debugging | π’ Easy | π΄ Hard (distributed) |
| Complexity | π’ Simple | π‘ Complex |
| Best For | User-facing, real-time | Background, analytics |
π³ Decision Tree: Which One to Use?
Q1: Immediate response required?
ββ YES β USE SYNC
ββ NO β Continue
Q2: User-facing critical path?
ββ YES β USE SYNC
ββ NO β Continue
Q3: Can processing be delayed?
ββ YES β USE ASYNC
ββ NO β USE SYNC
ββ YES β USE SYNC
ββ NO β Continue
Q2: User-facing critical path?
ββ YES β USE SYNC
ββ NO β Continue
Q3: Can processing be delayed?
ββ YES β USE ASYNC
ββ NO β USE SYNC
π The Hybrid Pattern: Best of Both Worlds
Use SYNC for critical path, ASYNC for side effects
π User Request
β
π¦ Order Service
β
π³ Payment Service
β
π Inventory Check
β
β
Response to User
β¬οΈ β¬οΈ β¬οΈ
π§ Email Service
π¦ Shipping Service
π Analytics Service
π Notification Service
β‘ These run in background β user doesn't wait!
π― 4 Golden Rules
1οΈβ£
User clicks? β SYNC
2οΈβ£
Notification? β ASYNC
3οΈβ£
Immediate result? β SYNC
4οΈβ£
High scale? β ASYNC
"Sync for critical path, Async for side effects"
π― One-Line Summary
"SYNC for immediate user-facing responses, ASYNC for everything that can wait."
"SYNC for immediate user-facing responses, ASYNC for everything that can wait."