Tuần 10 - Ngày 5: Serverless Patterns
Mục tiêu học tập
- Hiểu serverless ecosystem trên AWS
- Apply common serverless patterns
- Compare serverless vs container vs EC2
1. Serverless Definition
Đặc điểm
- No server management (you don't see EC2)
- Auto-scaling (0 → millions)
- Pay per use (per request/invocation)
- High availability built-in
AWS Serverless Services
- Compute: Lambda, Fargate
- Storage: S3
- Database: DynamoDB, Aurora Serverless v2
- Messaging: SQS, SNS, EventBridge
- API: API Gateway, AppSync
- Orchestration: Step Functions
- Streaming: Kinesis (somewhat)
- Other: SES (email), Cognito, AWS Glue
2. Serverless vs Other
Serverless (Lambda + DynamoDB)
- Pros: No infra, pay per use, auto-scale, fast deploy
- Cons: Cold start, 15-min limit, less control
Containers (Fargate + ECS)
- Pros: Containerized, more control, longer-running
- Cons: More config, cost may be higher
EC2
- Pros: Full control, all OS access, longest run
- Cons: Manage infra, scaling, patches
Decision
- Event-driven, short tasks: Lambda
- Long-running web apps: Fargate
- Specific OS, GPU, high control: EC2
3. Pattern 1: Simple REST API
Architecture
Client → API Gateway → Lambda → DynamoDB
Components
- API Gateway: HTTP routing, auth
- Lambda: business logic
- DynamoDB: data persistence
Cost (low traffic)
- 1M requests/month:
- Lambda: ~$0 (free tier)
- API Gateway: $1 (HTTP API)
- DynamoDB On-Demand: ~$1
- Total: ~$2/month for 1M requests
Scale to millions
- Auto-scale Lambda + DynamoDB
- Linear cost (no overhead)
4. Pattern 2: File Processing Pipeline
Architecture
User uploads file → S3
↓ (S3 event)
Lambda
↓
Process (transcode, OCR, etc.)
↓
S3 (output)
↓ (event)
SNS → Notify user
Use case
- Image thumbnail generation
- PDF text extraction
- Video transcoding (with AWS Elemental MediaConvert)
- File virus scanning
Lambda limitations
- 15-min execution → use Step Functions or ECS for longer
- 10 GB memory max → use ECS/Batch for heavy compute
5. Pattern 3: Real-time Analytics
Architecture
Use case
- Click stream analytics
- IoT sensor data
- Real-time fraud detection
6. Pattern 4: GraphQL Backend
Architecture
Use case
- Mobile app with complex data needs
- Real-time features (chat, dashboards)
- Offline support (Amplify)
7. Pattern 5: Workflow Orchestration
Architecture
Use case
- ETL pipelines
- Saga (distributed transactions)
- ML training pipelines
- Multi-step business processes
8. Pattern 6: Event-Driven Microservices
Architecture
Communication
- Sync: API calls between services (avoid as much as possible)
- Async: events via EventBridge
9. Pattern 7: Static Website + CDN
Architecture
Developer → CI/CD → S3 (static files)
↓
CloudFront (CDN + cache)
↓
Route 53 (DNS)
↓
User
Use case
- Marketing sites
- Single-page apps (React, Vue, Angular)
- Documentation sites
Cost
- S3: cents
- CloudFront: $0.085/GB after free tier
- Route 53: $0.50/zone
- Total: < $5/month for moderate traffic
10. Pattern 8: Cron / Scheduled Jobs
Architecture
Use case
- Daily reports
- Database cleanup
- Backup automation
- Periodic health checks
vs Cron on EC2
- No EC2 instance idle 99% of time
- Pay per execution
- Auto-scale (multiple schedules → multiple Lambdas)
11. Patterns 9: Async HTTP API
Vấn đề
- HTTP request timeout (29 sec on API Gateway, 30 sec ALB)
- Long-running tasks need async pattern
Architecture
1. Client → POST /jobs → API Gateway → Lambda
2. Lambda creates job_id in DynamoDB, sends to SQS
3. API returns 202 Accepted + job_id
4. Worker (Lambda/ECS) processes from SQS
5. Worker updates job status in DynamoDB
6. Client polls GET /jobs/{id} OR
Client uses WebSocket for push notification
12. Cost Optimization
Lambda
- Right-size memory (1769 MB = 1 vCPU)
- Provisioned Concurrency only when needed
- ARM (Graviton2) Lambda: 20% cheaper
- Lambda Power Tuning tool: find optimal memory
DynamoDB
- On-Demand for unpredictable (no minimum)
- Provisioned + Auto Scaling for known load (cheaper at scale)
- Reserved capacity for steady workload
API Gateway
- HTTP API ($1/M) instead of REST API ($3.50/M) when features OK
S3 + CloudFront
- CloudFront caching reduces S3 requests
- Use S3 Transfer Acceleration only when needed
13. Common Mistakes
Anti-patterns
- Long-running Lambda (close to 15 min) — use Step Functions/ECS
- Lambda + RDS without RDS Proxy — connection storms
- Synchronous chain of Lambdas — use Step Functions
- No DLQ for async invocations — losing failed events
- Lambda in VPC unnecessarily — adds cold start, NAT cost
- Cold start critical paths — use Provisioned Concurrency
Câu hỏi ôn tập
-
Khi nào dùng Step Functions thay vì Lambda chain?
Xem đáp án
Step Functions khi: (1) Workflow có retry/error handling phức tạp per step, (2) Cần audit trail và visibility vào execution state, (3) Cần parallel hoặc conditional branching dễ manage, (4) Workflow dài > 15 phút (vượt Lambda timeout), (5) Cần human approval step. Lambda chain (gọi Lambda từ Lambda) phù hợp cho simple linear 2-3 step flows — rẻ hơn và đơn giản hơn nhưng khó debug và không có built-in retry per step.
-
API Gateway HTTP vs REST: cái nào rẻ hơn?
Xem đáp án
HTTP API rẻ hơn ~70%: $1/million requests vs ~$3.5/million cho REST API. HTTP API cũng có lower latency. REST API cần khi muốn: API Keys/Usage Plans, request transformation (mapping templates), WAF integration, access logging chi tiết, edge-optimized endpoint, caching tích hợp. HTTP API đủ cho hầu hết modern serverless APIs với JWT authorization.
-
Lambda + RDS có vấn đề gì? Cách giải quyết?
Xem đáp án
Lambda scale horizontally (nhiều concurrent executions) — mỗi execution mở DB connection → connection exhaustion khi hàng nghìn Lambda concurrent (RDS max connections: 1,000-16,000 tùy instance). Cũng slow cold start khi mở new connection. Giải pháp: RDS Proxy — pool và reuse DB connections, giảm từ N Lambda connections xuống còn ~connections cần thiết thực sự. Hoặc dùng Aurora Serverless v2 với connection pooling tích hợp.
-
Async HTTP API pattern dùng services nào?
Xem đáp án
Pattern: API Gateway → SQS → Lambda (hoặc Step Functions). Flow: (1) API GW nhận request → ghi vào SQS, trả
202 Accepted+requestId, (2) Lambda consume từ SQS xử lý async, (3) Client poll/status/{requestId}hoặc nhận webhook. SQS buffer giúp: retry khi Lambda fail (DLQ), scale Lambda theo queue depth, không mất requests khi Lambda overloaded. EventBridge cũng có thể thay SQS cho event-driven scenarios. -
Static website serverless dùng services gì?
Xem đáp án
Standard pattern: S3 (lưu HTML/CSS/JS, enable static website hosting) + CloudFront (CDN + HTTPS với ACM cert + custom domain + cache) + Route 53 (DNS ALIAS record trỏ vào CloudFront). Optional: Lambda@Edge hoặc CloudFront Functions cho URL rewrite, auth headers. Cost gần như zero cho low-traffic sites (S3 storage rẻ, CloudFront free tier 1 TB/month 12 tháng đầu). Không cần server, không cần EC2.
Bài tập thực hành
- Build REST API serverless: API Gateway + Lambda + DynamoDB
- Build file processing: S3 → Lambda → S3
- Build scheduled cron job: EventBridge → Lambda
- Build async API: submit job → poll status
- Build static site: S3 + CloudFront + Route 53
Tài liệu tham khảo chính thức
Tiếp theo: Quiz Tuần 10