1.2.3 Event Lifecycle & Error Handling

Handle Event Lifecycle and Errors

Invocation Types & Retry Behavior

Invocation TypeVí dụRetryError Handling
SynchronousAPI Gateway, SDK invokeCaller tự retryCaller nhận error trực tiếp
AsynchronousS3, SNS, EventBridge2 retries (tổng 3 lần)DLQ hoặc Destinations
Event Source Mapping (SQS)SQS queueVisibility timeout → retryBatch item failures report
Event Source Mapping (Stream)Kinesis, DynamoDB StreamsRetry until success/expiryBisect 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

FeatureDestinationsDLQ
On-success
On-failure
TargetsSQS, SNS, Lambda, EventBridgeSQS, SNS only
MetadataFull invocation recordMessage only
Recommendation✅ PreferredLegacy

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

ConfigMô tả
Bisect Batch on ErrorChia batch làm 2, retry từng nửa
Max Retry AttemptsSố lần retry tối đa
Max Record AgeBỏ qua records quá cũ
On-failure DestinationGử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.