Fan-out với SNS → SQS → Lambda có lợi gì so với SNS → Lambda trực tiếp?
Xem đáp án
Chèn SQS giữa SNS và Lambda cho: (1) buffer khi consumer chậm/chết — message không mất (SNS invoke Lambda fail chỉ retry giới hạn), (2) rate control qua batch size + reserved concurrency, (3) DLQ per consumer với maxReceiveCount, (4) replay dễ hơn. Trade-off: thêm latency và chi phí SQS. Cần ordering trong fan-out → SNS FIFO + SQS FIFO (message group) — EventBridge không đảm bảo ordering.
Standard vs Express Step Functions — chọn theo tiêu chí nào?
Xem đáp án
Standard: exactly-once, chạy tới 1 năm, tính phí per state transition, full history — cho workflow nghiệp vụ (order saga, human approval, long-running). Express: at-least-once (phải idempotent), tối đa 5 phút, tính phí per execution + duration, rẻ hơn nhiều ở volume cao — cho event processing/IoT ingestion tần suất lớn. Keyword "high-volume short-lived executions, cost-effective" → Express; "long-running with audit trail" → Standard.
Vì sao visibility timeout của SQS phải lớn hơn function timeout của Lambda?
Xem đáp án
Lambda poll message → message ẩn trong visibility timeout; nếu function còn đang xử lý mà visibility timeout hết, message hiện lại và consumer khác xử lý trùng lặp. Khuyến nghị AWS: visibility timeout ≥ 6× function timeout (tính cả retry + batch window). Kèm theo: maxReceiveCount → DLQ để message hỏng không loop vô hạn, và bật partial batch failure reporting (ReportBatchItemFailures) để chỉ retry message fail trong batch.
Xử lý request vượt 29 giây sau API Gateway thế nào cho đúng pattern?
Xem đáp án
Pattern chuẩn async + polling/callback: API Gateway → Lambda nhận job, đẩy SQS/Step Functions, trả 202 Accepted + job ID ngay; client poll endpoint status (hoặc nhận WebSocket/webhook khi xong). Lưu ý cập nhật 06/2024: REST/HTTP API Regional có thể request nâng integration timeout quá 29s (đổi lại giảm throttle quota) — nhưng với đề thi, nâng timeout hiếm khi là "best answer" so với thiết kế async đúng nghĩa.
Queue-based load leveling: dựng API Gateway → Lambda → SQS → Lambda worker (reserved concurrency 2); bơm 500 request nhanh và quan sát queue depth tăng rồi tiêu dần — hệ thống không rớt request.
Saga với Step Functions: tạo state machine 3 bước (reserve inventory → charge payment → confirm) với Catch chuyển sang bước compensation (release/refund); chạy input gây fail ở bước 2 và xem nhánh rollback trong console.
Async API pattern: implement endpoint POST /jobs trả 202 + jobId (ghi DynamoDB), worker xử lý 2 phút, endpoint GET /jobs/{id} trả status — mô phỏng đúng đáp án "long-running task behind API Gateway".