1.1.4 Synchronous vs Asynchronous
Synchronous vs Asynchronous Patterns
Synchronous
- Caller chờ response trước khi tiếp tục
- API Gateway → Lambda (sync invocation)
- Ưu: Đơn giản, immediate response
- Nhược: Blocking, timeout issues
Asynchronous
- Caller gửi request và tiếp tục, không chờ response
- S3 event → Lambda (async invocation)
- SQS → Lambda (event source mapping)
- Ưu: Decoupled, resilient, scalable
- Nhược: Phức tạp hơn, eventual consistency
Lambda Invocation Types
| Type | Trigger Examples | Retry | Error Handling |
|---|
| Synchronous | API Gateway, ALB, CloudFront | Caller retry | Caller nhận error |
| Asynchronous | S3, SNS, EventBridge | 2 retries (tổng 3) | DLQ / Destinations |
| Event Source Mapping | SQS, Kinesis, DynamoDB Streams | Until success hoặc expiry | Bisect batch, DLQ |
Quan trọng: SQS + Lambda: Visibility timeout phải > 6x function timeout. Nếu không, message sẽ bị xử lý lại trước khi Lambda hoàn thành.