1.2.3 Event Lifecycle & Error Handling
Handle Event Lifecycle and Errors
Invocation Types & Retry Behavior
| Invocation Type | Ví dụ | Retry | Error Handling |
|---|
| Synchronous | API Gateway, SDK invoke | Caller tự retry | Caller nhận error trực tiếp |
| Asynchronous | S3, SNS, EventBridge | 2 retries (tổng 3 lần) | DLQ hoặc Destinations |
| Event Source Mapping (SQS) | SQS queue | Visibility timeout → retry | Batch item failures report |
| Event Source Mapping (Stream) | Kinesis, DynamoDB Streams | Retry until success/expiry | Bisect batch, max retry age |
Asynchronous Invocation Flow
Event → Internal Queue → Lambda (attempt 1)
→ Lambda (attempt 2, sau 1 min)
→ Lambda (attempt 3, sau 2 min)
→ On-failure Destination / DLQ
- Max event age: 6 hours (configurable, min 60s)
- Max retry attempts: 0-2 (configurable)
Destinations vs DLQ
| Feature | Destinations | DLQ |
|---|
| On-success | ✅ | ❌ |
| On-failure | ✅ | ✅ |
| Targets | SQS, SNS, Lambda, EventBridge | SQS, SNS only |
| Metadata | Full invocation record | Message only |
| Recommendation | ✅ Preferred | Legacy |
SQS Event Source Mapping — Batch Item Failures
def handler(event, context):
batch_item_failures = []
for record in event['Records']:
try:
process(record)
except Exception:
batch_item_failures.append({
"itemIdentifier": record['messageId']
})
return {"batchItemFailures": batch_item_failures}
ReportBatchItemFailures trong event source mapping config- Chỉ retry failed items thay vì toàn bộ batch
Kinesis/DynamoDB Streams Error Handling
| Config | Mô tả |
|---|
| Bisect Batch on Error | Chia batch làm 2, retry từng nửa |
| Max Retry Attempts | Số lần retry tối đa |
| Max Record Age | Bỏ qua records quá cũ |
| On-failure Destination | Gửi failed records đến SQS/SNS |
Exam Tip: Destinations > DLQ (linh hoạt hơn). SQS batch → dùng ReportBatchItemFailures. Kinesis/Streams → BisectBatchOnFunctionError để tìm bad record.