Lambda gọi Lambda → callback hell. Thêm retry, timeout, error handling → code phình gấp 3. Step Functions giải quyết: bạn định nghĩa workflow bằng JSON (ASL), nó lo execution, retry, error handling.
Mình từng thấy một Lambda handler 50 dòng code nghiệp vụ, phình lên 200 dòng vì retry/timeout/error handling. Step Functions loại bỏ hết code boilerplate đó, giao cho state machine.
Một lần mình gặp case Step Functions timeout khi Lambda cold start + retry mất 30 giây, vượt task timeout mặc định. Bài học: luôn set timeout Lambda < task timeout, và để lại buffer cho retry.
flowchart LR
Start["Start"] --> Validate["Validate Order<br/>Lambda"]
Validate -->|"valid"| Charge["Charge Payment<br/>Lambda · Retry 3x"]
Validate -->|"invalid"| Fail["Fail"]
Charge -->|"success"| Parallel
Charge -->|"failed"| Refund["Refund"]
Parallel["Parallel: Email + Push Notify"] --> End["End"]
{
"StartAt": "Validate Order",
"States": {
"Validate Order": {
"Type": "Task",
"Resource": "arn:aws:lambda:...",
"Retry": [
{ "ErrorEquals": ["States.ALL"], "MaxAttempts": 3, "BackoffRate": 2 }
],
"Next": "Charge Payment"
},
"Charge Payment": {
"Type": "Task",
"Resource": "arn:aws:lambda:...",
"Catch": [{ "ErrorEquals": ["PaymentFailed"], "Next": "Refund" }],
"Next": "Parallel Notifications"
},
"Parallel Notifications": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "SendEmail",
"States": {
"SendEmail": { "Type": "Task", "Resource": "...", "End": true }
}
},
{
"StartAt": "PushNotify",
"States": {
"PushNotify": { "Type": "Task", "Resource": "...", "End": true }
}
}
],
"End": true
},
"Refund": {
"Type": "Task",
"Resource": "arn:aws:lambda:...",
"End": true
}
}
}
| State | Use Case |
|---|---|
| Task | Gọi Lambda, ECS, SNS, SQS, HTTP |
| Choice | Branch if/else |
| Parallel | Chạy nhiều nhánh đồng thời |
| Map | For each item xử lý song song — Inline (max 40), Distributed (up to 10,000 concurrent) |
| Wait | Delay N giây hoặc đến timestamp |
| Callback | Pause chờ human approval (task token pattern) |
Express vs Standard
| Standard | Express | |
|---|---|---|
| Duration | Tối đa 1 năm | Tối đa 5 phút |
| Rate | 2,000/s (default service quota, có thể request tăng) | 100,000/s (default service quota, có thể request tăng) |
| Cost | $25/1M transitions | $1/1M transitions |
| History | 90 ngày (mặc định, có thể cấu hình tới 1 năm) | CloudWatch Logs |