Compute
D2 · Resilient
D3 · Performance
D4 · Cost
~1 phút đọc

Lambda & kiến trúc serverless

Function as a Service — billing theo ms, tự scale. Hiểu concurrency, cold start, integration pattern và giới hạn.

lambda
serverless
api-gateway
event-driven

Sơ đồ tổng quan

Đang tải sơ đồ…

Giới hạn cần nhớ

  • Timeout tối đa 15 phút.
  • Memory 128 MB – 10 GB (CPU tỉ lệ thuận).
  • Deployment package: 50 MB zipped (direct), 250 MB unzipped; 10 GB qua container image.
  • `/tmp` 512 MB mặc định, có thể cấu hình tới 10 GB.
  • Concurrent executions mặc định 1000/region (soft limit).

Concurrency control

  • Reserved concurrency: giành slot riêng, tránh function khác ăn hết quota.
  • Provisioned concurrency: keep warm N instance → triệt tiêu cold start (trả tiền).
  • SnapStart (Java): snapshot runtime → khởi động nhanh mà không cần provisioned concurrency.

Pattern thường gặp

  • API Gateway + Lambda + DynamoDB = REST API serverless.
  • S3 event → Lambda → process file (thumbnail, virus scan).
  • Kinesis / DynamoDB Streams → Lambda cho CDC.
  • EventBridge Scheduler → Lambda thay cho cron.
  • SQS → Lambda (batch window, batch size).
Lưu ý
Lambda truy cập RDS dễ gây cạn connection. Dùng RDS Proxy để pool & warm connection.

Error handling

  • Async invoke (S3, SNS): retry 2 lần, sau đó gửi DLQ (SQS/SNS) hoặc On-Failure destination.
  • Stream (Kinesis/DDB Streams): retry tới khi hết retention hoặc tới on-failure destination; có Bisect Batch On Function Error.
  • Sync (API GW, ALB): caller xử lý retry.