Introduction
Cloud has changed the way we design systems. From purchasing a physical server, we can now run code without needing to manage infrastructure. This article explores Cloud-Native principles and Serverless architecture.
1. 12-Factor App
12 nguyên tắc thiết kế ứng dụng Cloud-Native:
1. Codebase: 1 repo, nhiều deploys (staging, prod)
2. Dependencies: Khai báo rõ ràng (package.json, requirements.txt)
3. Config: Lưu trong environment variables (không hardcode)
4. Backing: Database, cache, queue là attached resources
5. Build/Release/Run: Tách biệt 3 stages
6. Processes: Stateless processes (không lưu state in-memory)
7. Port binding: Self-contained, export via port
8. Concurrency: Scale out bằng processes
9. Disposability: Fast startup, graceful shutdown
10. Dev/Prod parity: Giữ dev, staging, prod giống nhau
11. Logs: Treat logs as event streams (stdout)
12. Admin: One-off admin tasks cũng là code
2. Serverless Computing
2.1 FaaS (Function as a Service)
Traditional Server:
┌──────────────────────────┐
│ Server (24/7 running) │
│ OS, Runtime, App │
│ Bạn quản lý TẤT CẢ │
│ Trả tiền 24/7 │
└──────────────────────────┘
Container (CaaS):
┌──────────────────────────┐
│ Container (on demand) │
│ Runtime, App │
│ Cloud quản lý OS, Infra │
│ Trả tiền khi container chạy │
└──────────────────────────┘
Serverless (FaaS):
┌──────────────────────────┐
│ Function (on invocation) │
│ Chỉ viết code │
│ Cloud quản lý EVERYTHING │
│ Trả tiền per invocation │
│ Auto scale 0 → ∞ │
└──────────────────────────┘
2.2 Execution Model
Request 1: → Cold Start (init container) → Execute → Return
Request 2: → Warm (reuse container) → Execute → Return
Request 3: → Warm (reuse container) → Execute → Return
... (idle 15 min) ...
Request 4: → Cold Start (new container) → Execute → Return
Cold Start Timeline:
├── Download code (100-500ms)
├── Init runtime (50-200ms)
├── Init dependencies (100-2000ms)
├── Execute function (varies)
└── Total cold start: 500ms - 5s
Mitigation:
- Provisioned concurrency (keep warm)
- Smaller packages (fewer dependencies)
- Choose fast runtimes (Go, Rust > Java, .NET)
3. Serverless Patterns
3.1 API Gateway + Lambda
Client ──► API Gateway ──► Lambda ──► DynamoDB
│
├──► /users → UserFunction
├──► /orders → OrderFunction
└──► /search → SearchFunction
Ưu điểm: Auto-scale, pay per request
Nhược điểm: Cold start, 15m timeout limit
3.2 Event-Driven Serverless
┌──────────┐ ┌────────┐ ┌──────────┐
│ S3 │────►│ Lambda │────►│ DynamoDB │
│ Upload │ │ Process│ │ Metadata │
└──────────┘ │ image │ └──────────┘
└────┬───┘
│
┌────▼───┐
│ S3 │
│ Thumb │
└────────┘
Events trigger functions:
- S3: File uploaded → Lambda resize
- SQS: Message → Lambda process
- DynamoDB Streams: Record change → Lambda sync
- CloudWatch: Schedule → Lambda cron job
- API Gateway: HTTP request → Lambda handler
3.3 Serverless Web App
┌──────────────────────────────────────────────────┐
│ │
│ CloudFront (CDN) │
│ │ │
│ ┌────▼────┐ │
│ │ S3 │ ← Static files (React/Vue SPA) │
│ │ Bucket │ │
│ └─────────┘ │
│ │
│ API Gateway → Lambda Functions │
│ │ │ │
│ │ ┌────▼─────┐ ┌──────────┐ │
│ │ │ DynamoDB │ │ Cognito │ │
│ │ │ (data) │ │ (auth) │ │
│ │ └──────────┘ └──────────┘ │
│ │ │
│ ┌────▼─────┐ │
│ │ SQS │ → Lambda (background) │
│ └──────────┘ │
└──────────────────────────────────────────────────┘
Chi phí (100K requests/tháng):
Lambda: ~$0.20
API Gateway: ~$3.50
DynamoDB: ~$1.00
S3 + CloudFront: ~$1.00
Total: ~$6/tháng (vs EC2 ~$20+/tháng)
4. Container Orchestration
4.1 Kubernetes Overview
┌─────────────────────────────────────────────────┐
│ Kubernetes Cluster │
│ │
│ Control Plane: │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │API Server│ │Scheduler │ │Controller│ │
│ │ │ │ │ │Manager │ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ │
│ Worker Nodes: │
│ ┌────────────────────────────────────────┐ │
│ │ Node 1 │ │
│ │ ┌──────────┐ ┌──────────┐ ┌────────┐ │ │
│ │ │Pod: App-1│ │Pod: App-2│ │Pod: DB │ │ │
│ │ └──────────┘ └──────────┘ └────────┘ │ │
│ └────────────────────────────────────────┘ │
│ ┌────────────────────────────────────────┐ │
│ │ Node 2 │ │
│ │ ┌──────────┐ ┌──────────┐ │ │
│ │ │Pod: App-3│ │Pod: Cache│ │ │
│ │ └──────────┘ └──────────┘ │ │
│ └────────────────────────────────────────┘ │
└─────────────────────────────────────────────────┘
K8s quản lý: Scheduling, Scaling, Self-healing,
Rolling updates, Service discovery, Load balancing
4.2 Serverless vs Containers vs VMs
| Features | VM | Containers | Serverless |
|---|---|---|---|
| Startups | Minutes | Seconds | Milliseconds* |
| Scaling | Manual/Auto | Auto (K8s) | Instant |
| Management | Full (OS+App) | App + Runtime | Code only |
| Cost model | Per hour | Per hour | Per invocation |
| Max runtime | Unlimited | Unlimited | 15 minutes |
| State | Stateful | Stateful | Stateless |
| Vendor lock | Low | Low | High |
5. When to use Serverless?
✅ Dùng khi:
- Event-driven workloads (file processing, webhooks)
- Sporadic traffic (low/unpredictable)
- Rapid prototyping
- Scheduled tasks (cron jobs)
- Chatbots, IoT backends
- APIs with low-medium traffic
❌ KHÔNG dùng khi:
- Consistent high traffic (vì cost cao hơn containers)
- Long-running processes (> 15 minutes)
- Need WebSocket/persistent connections
- Heavy computation (ML training)
- Latency-sensitive (cold start vấn đề)
- Complex stateful workflows
6. Vendor Lock-in Mitigation
Vấn đề: AWS Lambda code khó chạy trên GCP/Azure
Mitigation:
1. Serverless Framework / SAM / CDK:
Abstract cloud-specific config
2. Hexagonal Architecture:
Business logic tách biệt handlers
// Handler (cloud-specific)
export const handler = async (event) => {
return await orderService.createOrder(
parseRequest(event) // adapter
);
};
// Business logic (portable)
class OrderService {
async createOrder(data) { ... }
}
3. Multi-cloud tools:
Knative (serverless on K8s)
OpenFaaS (self-hosted FaaS)
Summary
| Approach | Best For | Trade-off |
|---|---|---|
| Cloud VMs | Legacy, stateful | Full control, high ops |
| Containers + K8s | Microservices | Flexible, complex ops |
| Serverless FaaS | Event-driven, variable load | Simple, vendor lock |
| Serverless Containers | Mixed workloads | Balance control/simplicity |
Exercises
-
Architecture Choice: Startup SaaS: team 3, API + web dashboard, initial 1K users, growth uncertain. Comparison: Serverless (Lambda + DynamoDB) vs Containers (ECS/K8s + PostgreSQL). Which one to choose?
-
12-Factor Audit: Review your current (or imagined) application. Check 12 factors, list which factors are in violation and how to fix them.
-
Migration Plan: Monolith Spring Boot app (10 endpoints, PostgreSQL, Redis, cron jobs). Write a migration plan to serverless. Which endpoint is suitable for Lambda? Which one should hold the container?