Microservices Synchronous Vs Asynchronous Patterns - Overview.
scope:
- Intro,
- Synchronous Patterns (Tightly Coupled, Real-Time Response),
- Asynchronous Patterns (Loosely Coupled, Event-Driven),
- Synchronous vs Asynchronous Trade-offs,
- Synchronous vs Asynchronous table,
- Rule of the Thumb.
Intro:
- Microservices communication can be categorized into two primary patterns:
- synchronous
- asynchronous.
- Choosing between them depends entirely on the specific needs of the task, with a hybrid approach often being the most practical solution.
- When designing a distributed systems (especially in cloud architectures like AWS), it is proper to choose between Synchronous and Asynchronous Communication Patterns depending on latency, reliability, scaling, and decoupling needs.
Synchronous Patterns (Tightly
Coupled, Real-Time Response)
Samples: API Gateway, Load
Balancers, ALB/NLB, Direct Service-to-Service Calls(Lambda)
- Definition:
The client sends a request and waits for an immediate response from the service. - Characteristics:
- Request/response model (like REST APIs, gRPC).
- Low latency is crucial.
- Clients are blocked until they
receive a reply.
- Systems are more tightly coupled
(failure in downstream = failure upstream).
- AWS Examples:
- API Gateway calling Lambda/ECS.
- Application Load Balancer routing HTTP(S) traffic to EC2/ECS.
- Network Load Balancer forwarding TCP connections.
- Use Cases:
- Real-time data fetch (e.g., a mobile app
querying product availability).
- Authentication & authorization
services.
- Payment gateways (where immediate response matters).
Asynchronous Patterns (Loosely
Coupled, Event-Driven)
Samples: SQS, SNS, Kinesis, Lambda
Triggers, EventBridge, S3 Event Notifications
- Definition:
The client produces an event/message and doesn’t wait for a direct reply. Processing happens later. - Characteristics:
- Decoupled: producer doesn’t need to know who
consumes.
- Durable messaging (queues, streams).
- Supports retries and error
handling more gracefully.
- Scalable (consumers can scale horizontally).
- Event-driven instead of request-driven.
- AWS Examples:
- SQS: Reliable queue for decoupling producers/consumers.
- SNS: Pub/Sub broadcasting to multiple subscribers.
- Kinesis: Real-time event streaming.
- S3 Events: Trigger Lambda when a file is
uploaded.
- EventBridge: Routing events across services.
- Use Cases:
- Order processing pipelines.
- Log/event ingestion at scale (e.g.,
analytics).
- Image/video processing jobs (background
tasks).
- IoT telemetry data streaming.
Synchronous vs Asynchronous Trade-offs
Side-by-side diagram showing:
- Synchronous Pattern: API Gateway,Load Balancer, directly invoking Lambda.
- Asynchronous Pattern: S3 triggering an event, routed through SNS/SQS, then processed by Lambda.
|
Aspect |
Synchronous
(API Gateway, LB) |
Asynchronous
(SQS, SNS, Kinesis) |
|
Coupling |
Tight (caller depends on callee
availability). |
Loose (decoupled
producers/consumers) |
|
Response Time |
Immediate (blocking). |
Eventual (non-blocking) |
|
Resilience |
Lower (failures cascade upstream). |
Higher (messages can retry, store,
replay) |
|
Scalability |
Limited by real-time service
capacity. |
Can handle massive spikes
(buffering, fan-out) |
|
Complexity |
Simple to implement & debug. |
More complex (need DLQs,
idempotency, retries) |
|
Best For |
Real-time APIs, interactive apps. |
Background processing, streaming,
decoupled workflows |
Rule of the Thumb:
- Use synchronous for low-latency, user-facing APIs.
- Use asynchronous for background tasks, event processing, high-throughput data pipelines, and when reliability/decoupling is key.
No comments:
Post a Comment