Tuần 5 - Ngày 3: Lambda & Serverless
Mục tiêu học tập
- Hiểu Lambda architecture và limits
- Nắm vững event-driven patterns
- Biết cách optimize Lambda performance
1. Lambda Architecture
2. Lambda Invocation
Invocation Types
1. SYNCHRONOUS (RequestResponse)
- Wait for function to complete
- Get result immediately
- Examples: API Gateway, ALB
2. ASYNCHRONOUS (Event)
- Fire and forget
- Automatic retry (2 times)
- Examples: S3, SNS, EventBridge
3. POLL-BASED (Event Source Mapping)
- Lambda polls source
- Batch processing
- Examples: SQS, Kinesis, DynamoDB Streams
Event Source Mapping
3. Lambda Performance
Cold Start vs Warm Start
Provisioned Concurrency
Memory & CPU Optimization
4. Lambda@Edge & CloudFront Functions
Comparison
5. Serverless Application Model (SAM)
SAM Template
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Timeout: 30
Runtime: python3.9
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
CodeUri: ./src
Events:
Api:
Type: Api
Properties:
Path: /items
Method: GET
Policies:
- DynamoDBReadPolicy:
TableName: !Ref MyTable
6. Câu hỏi ôn tập
-
3 loại Lambda invocation là gì?
Xem đáp án
(1) Synchronous (RequestResponse) — caller đợi response, xử lý lỗi phía caller (API GW, ALB, Cognito). (2) Asynchronous (Event) — Lambda queue event, retry 2 lần nếu fail, DLQ cho unprocessed events (S3, SNS, EventBridge). (3) Event Source Mapping — Lambda poll source service (SQS, DynamoDB Streams, Kinesis, Kafka), batch processing. Error handling khác nhau per invocation type — quan trọng cho architect design.
-
Cold start xảy ra khi nào?
Xem đáp án
Cold start xảy ra khi không có warm execution environment sẵn sàng: (1) First invocation sau deploy, (2) Scale-out khi cần thêm concurrent executions, (3) Sau period of inactivity (environment bị reclaim). Cold start = download code + init runtime + run init code (
importstatements, DB connections, SDK clients) = 100ms đến vài giây. Giảm cold start: Provisioned Concurrency, tối ưu init code, dùng SnapStart (Java), lazy-load resources. -
Provisioned Concurrency giải quyết vấn đề gì?
Xem đásp án
Giải quyết cold start latency bằng cách giữ sẵn N execution environments warm và initialized — function respond ngay lập tức không có cold start delay. Phù hợp cho: latency-sensitive APIs (P99 requirements), functions với heavy init (ML models, connection pools), traffic spikes predictable (8 AM business hours). Chi phí: tính phí theo số provisioned concurrency × thời gian — kể cả khi idle. Trade-off: cost vs latency.
-
Lambda@Edge khác CloudFront Functions như thế nào?
Xem đáp án
Lambda@Edge: runs Node.js/Python, max 5-30 giây execution, có thể gọi network (DynamoDB, Secrets Manager), max 10 MB deployment package, 4 event triggers (viewer/origin request/response). CloudFront Functions: JavaScript only, max 1 ms, NO network calls, 10 KB code limit, chỉ 2 triggers (viewer request/response). CloudFront Functions ~6× rẻ hơn, cho lightweight operations (header manipulation, URL rewrite, simple redirects). Lambda@Edge cho complex logic cần external data.
-
Event Source Mapping hoạt động như thế nào?
Xem đáp án
Event Source Mapping cho phép Lambda poll từ event sources: SQS, DynamoDB Streams, Kinesis Data Streams, Apache Kafka (MSK), self-managed Kafka. Lambda service (không phải function code) poll source và batch records → invoke Lambda function với batch. Config: BatchSize (1-10,000 records), BisectOnFunctionError (split batch khi fail), DestinationConfig (DLQ/DLS for failed records), Filter patterns. SQS: Lambda delete messages sau khi process thành công; Kinesis/DDB Streams: checkpointing tự động.
Tài liệu tham khảo chính thức
Ngày tiếp theo: S3 Advanced & Storage