Bài này gộp 2 chủ đề: Modernization Patterns, Serverless Patterns và Best Practices.
Mục tiêu học tập
- Hiểu modernization spectrum (VMs → managed services → containers → serverless) và trade-off
- Nắm Strangler Fig pattern để tách monolith an toàn
- Biết kiến trúc microservices/event-driven trên AWS và các pattern Gen AI (Bedrock, RAG)
- Nhận diện 6 pattern serverless kinh điển (API backend, event processing, fan-out, load leveling, saga, strangler fig)
- Nắm Lambda best practices: cold start, memory/timeout, error handling theo invocation type
- Chọn đúng REST API vs HTTP API vs WebSocket và Standard vs Express Step Functions
Phần 1: Modernization Patterns
1. Modernization Paths
2. Strangler Fig Pattern
3. Microservices on AWS
4. Event-Driven Architecture
5. Amazon MQ — Re-platform Messaging
Managed message broker cho Apache ActiveMQ và RabbitMQ. Đây là lựa chọn re-platform cho messaging: app on-prem đang dùng message broker với protocol chuẩn công nghiệp — JMS, AMQP 0-9-1, MQTT, OpenWire, STOMP — migrate lên cloud mà không sửa code ứng dụng, chỉ đổi connection endpoint sang broker của Amazon MQ.
Amazon MQ vs SQS/SNS
| Tiêu chí | Amazon MQ | SQS/SNS |
|---|---|---|
| Protocol | JMS, AMQP 0-9-1, MQTT, OpenWire, STOMP (chuẩn công nghiệp) | API riêng của AWS (HTTPS) |
| Migrate app cũ | Không cần sửa code | Phải rewrite messaging layer |
| Throughput/scale | Giới hạn theo instance size của broker | Cloud-native, scale gần như vô hạn |
| Network | Chạy trong VPC (endpoint riêng) | Public endpoint (hỗ trợ VPC endpoint) |
| Auth | Username/password của broker (+ LDAP với ActiveMQ) | IAM policies |
| Operational overhead | Vẫn phải chọn instance size, quản lý broker config | Thấp nhất (fully serverless) |
| Use case | App cũ dùng ActiveMQ/RabbitMQ/protocol chuẩn | App mới build trên AWS |
Nguyên tắc chọn cho exam: app mới → luôn ưu tiên SQS/SNS (scale tốt hơn, rẻ hơn, ít vận hành hơn); app hiện có đang dùng messaging protocol chuẩn và yêu cầu "migrate messaging without rewriting application code" → Amazon MQ. Nếu đề bài cho phép sửa code và nhấn "unlimited scale" / "least operational overhead" thì SQS/SNS mới là đáp án — đừng chọn MQ chỉ vì thấy chữ "message broker".
6. Generative AI Modernization Patterns
AWS đẩy mạnh dịch vụ Gen AI từ 2023-2024. SAP-C02 (post-2024) bắt đầu xuất hiện scenario về kiến trúc AI-augmented application — cần nắm các lựa chọn chính.
Amazon Bedrock (managed Foundation Models)
Khi nào dùng Bedrock: cần thêm Gen AI vào application mà không muốn quản lý GPU/model serving. Multi-tenant, cross-account access qua IAM Identity Center hoặc resource-based policies.
Bedrock vs SageMaker JumpStart vs Self-managed
| Tiêu chí | Bedrock | SageMaker JumpStart | Self-managed (EC2/EKS) |
|---|---|---|---|
| Quản lý infra | Không (serverless) | One-click deploy lên endpoint | Toàn bộ tự quản |
| Customize model | Fine-tuning có giới hạn | Full fine-tuning, custom training | Toàn quyền |
| Cost model | Per token | Endpoint + storage | EC2/GPU hours |
| Privacy | Data không leak ra base model | Trong VPC, isolated | Full control |
| Operational overhead | Thấp nhất | Trung bình | Cao nhất |
| Use case | Production Gen AI app, RAG, agent | Custom model, fine-tune sâu | Có yêu cầu compliance đặc biệt |
Pattern: RAG (Retrieval-Augmented Generation)
Các dịch vụ AI/ML khác SAP-C02 hay hỏi
- SageMaker: end-to-end ML platform (train, deploy, monitor models)
- SageMaker JumpStart: pre-trained models + solution templates
- Comprehend: NLP (sentiment, entity recognition, language detection)
- Textract: OCR + form/table extraction
- Rekognition: image/video analysis (faces, objects, moderation)
- Transcribe / Polly: speech-to-text / text-to-speech
- Translate: real-time translation
- Lex: build chatbot/voice bot với ASR + NLU (intents/slots — cùng công nghệ Alexa); tích hợp Amazon Connect để tự động hoá contact center
- Kendra: enterprise search bằng ML trên tài liệu nội bộ (connectors: S3, SharePoint, Salesforce...), trả lời câu hỏi ngôn ngữ tự nhiên; hay dùng làm retriever cho RAG với Bedrock
- Personalize: recommendation engine (rất phù hợp với e-commerce)
- Q Developer (formerly CodeWhisperer): AI code assistant cho IDE/CLI
Exam keyword cho Gen AI scenario
| Yêu cầu trong câu hỏi | Service phù hợp |
|---|---|
| "Use foundation models với least operational overhead" | Bedrock |
| "Customize model với enterprise data, không leak data" | Bedrock fine-tuning hoặc Knowledge Bases |
| "Build chatbot với context từ internal docs" | Bedrock + Knowledge Bases (RAG) |
| "Multi-step agent với tool use" | Bedrock Agents |
| "Content moderation, PII redaction" | Bedrock Guardrails hoặc Comprehend PII |
| "Train custom model từ đầu, full control" | SageMaker (training jobs) |
| "Quick deploy pre-trained model" | SageMaker JumpStart |
| "Automated customer service chatbot" (contact center) | Lex (+ Amazon Connect) |
| "Natural language search across company documents" | Kendra |
Phần 2: Serverless Patterns và Best Practices
Tổng quan Serverless Architecture
Common Serverless Patterns
1. API Backend Pattern
GraphQL Variant:
2. Event Processing Pattern
3. Fan-Out Pattern
4. Queue-Based Load Leveling
5. Saga Pattern (Distributed Transactions)
6. Strangler Fig Pattern
Lambda Best Practices
1. Cold Start Optimization
2. Memory and Timeout
3. Error Handling
API Gateway Patterns
1. REST API vs HTTP API
2. Caching Strategy
Step Functions Patterns
1. State Machine Types
2. Common States
Exam Scenarios
Scenario 1: High-Volume Event Processing
Scenario 2: Async Workflow
Scenario 3: Real-time Dashboard
Quick Reference
Câu hỏi ôn tập
-
Strangler Fig pattern hoạt động thế nào và vì sao an toàn hơn big-bang rewrite?
Xem đáp án
Đặt facade (ALB/API Gateway) trước monolith, rồi tách dần từng chức năng thành service mới — facade route path đã migrate sang service mới, phần còn lại vẫn vào monolith, đến khi monolith "bị siết" hết. An toàn hơn big-bang vì: mỗi bước nhỏ có thể rollback (đổi routing), business chạy liên tục, học được từ service đầu trước khi tách tiếp, và giảm rủi ro "rewrite 2 năm không release gì". Keyword "gradually migrate monolith with minimal risk" → Strangler Fig.
-
Chọn sync (API) hay async (event) cho giao tiếp giữa microservices?
Xem đáp án
Sync (API Gateway/ALB): khi caller cần kết quả ngay (query, validate) — nhưng tạo temporal coupling, service B chết kéo A chết theo. Async (SQS/SNS/EventBridge): khi xử lý có thể trễ (order processing, notification) — loose coupling, buffer spike, retry tự nhiên; trade-off là eventual consistency và khó debug hơn (cần tracing). Nguyên tắc Professional: mặc định async giữa bounded contexts, sync chỉ trong critical read path — nhiều câu exam thưởng cho việc chèn SQS giữa 2 tier để "decouple and absorb spikes".
-
Khi nào Bedrock Knowledge Bases là đáp án thay vì fine-tuning?
Xem đáp án
Knowledge Bases (RAG): kiến thức nằm trong tài liệu thay đổi thường xuyên (policy nội bộ, docs sản phẩm) — model lấy context lúc query, cập nhật chỉ cần re-index, không train lại, dữ liệu kiểm soát được (citation). Fine-tuning: cần đổi hành vi/giọng điệu/format của model hoặc domain đặc thù mà prompt + RAG không đủ — tốn kém hơn, cập nhật kiến thức chậm. Keyword "chatbot answers from internal documents, always up to date" → Knowledge Bases; "model must follow company-specific style at scale" → fine-tuning.
-
App2Container phù hợp scenario nào trong modernization?
Xem đáp án
App2Container (A2C): CLI phân tích app Java/.NET đang chạy trên VM/on-prem, tự đóng gói thành container image + sinh ECS/EKS artifacts và pipeline — dùng cho bước "containerize without code changes" (giữa Rehost và Refactor). Phù hợp: legacy app không có Dockerfile, team muốn chuẩn hoá lên ECS/EKS nhanh. Không phù hợp: app cần rewrite logic (Refactor thật sự) hoặc COTS app không hỗ trợ.
-
Công ty migrate app on-prem đang dùng ActiveMQ với JMS lên AWS, yêu cầu không sửa code ứng dụng. Chọn Amazon MQ hay SQS/SNS, và thiết kế HA thế nào?
Xem đáp án
Amazon MQ (ActiveMQ engine) — hỗ trợ nguyên protocol JMS/OpenWire nên app chỉ cần đổi endpoint, đúng keyword "migrate messaging without rewriting application code" (re-platform). SQS/SNS bắt buộc rewrite messaging layer sang AWS API nên loại. HA: deploy active/standby multi-AZ với shared storage trên EFS — broker standby ở AZ khác tự động takeover khi active fail, message không mất. (Với RabbitMQ engine thì HA là cluster 3 nodes trải 3 AZ.) Ngược lại, nếu là app mới hoặc đề cho phép sửa code + nhấn "unlimited scale"/"least operational overhead" → SQS/SNS.
-
Chatbot tự phục vụ khách hàng cần trả lời từ tài liệu nội bộ: khi nào dùng Lex, khi nào dùng Kendra, khi nào kết hợp?
Xem đáp án
Lex = giao diện hội thoại (ASR + NLU, intents/slots): nhận yêu cầu người dùng qua voice/text, phù hợp "automated customer service chatbot", tích hợp Amazon Connect cho contact center. Kendra = ML-powered enterprise search: index tài liệu nội bộ (S3, SharePoint, Salesforce...) và trả lời câu hỏi ngôn ngữ tự nhiên — keyword "natural language search across company documents". Kết hợp: Lex nhận intent, fulfillment (Lambda) query Kendra để lấy câu trả lời từ docs; hoặc pattern hiện đại hơn là Kendra làm retriever cho RAG với Bedrock. Lex không tự tìm trong tài liệu, Kendra không tự quản hội thoại — đề hỏi cả hai vế thì đáp án là kiến trúc ghép.
-
Fan-out với SNS → SQS → Lambda có lợi gì so với SNS → Lambda trực tiếp?
Xem đáp án
Chèn SQS giữa SNS và Lambda cho: (1) buffer khi consumer chậm/chết — message không mất (SNS invoke Lambda fail chỉ retry giới hạn), (2) rate control qua batch size + reserved concurrency, (3) DLQ per consumer với maxReceiveCount, (4) replay dễ hơn. Trade-off: thêm latency và chi phí SQS. Cần ordering trong fan-out → SNS FIFO + SQS FIFO (message group) — EventBridge không đảm bảo ordering.
-
Standard vs Express Step Functions — chọn theo tiêu chí nào?
Xem đáp án
Standard: exactly-once, chạy tới 1 năm, tính phí per state transition, full history — cho workflow nghiệp vụ (order saga, human approval, long-running). Express: at-least-once (phải idempotent), tối đa 5 phút, tính phí per execution + duration, rẻ hơn nhiều ở volume cao — cho event processing/IoT ingestion tần suất lớn. Keyword "high-volume short-lived executions, cost-effective" → Express; "long-running with audit trail" → Standard.
-
Vì sao visibility timeout của SQS phải lớn hơn function timeout của Lambda?
Xem đáp án
Lambda poll message → message ẩn trong visibility timeout; nếu function còn đang xử lý mà visibility timeout hết, message hiện lại và consumer khác xử lý trùng lặp. Khuyến nghị AWS: visibility timeout ≥ 6× function timeout (tính cả retry + batch window). Kèm theo: maxReceiveCount → DLQ để message hỏng không loop vô hạn, và bật partial batch failure reporting (
ReportBatchItemFailures) để chỉ retry message fail trong batch. -
Xử lý request vượt 29 giây sau API Gateway thế nào cho đúng pattern?
Xem đáp án
Pattern chuẩn async + polling/callback: API Gateway → Lambda nhận job, đẩy SQS/Step Functions, trả 202 Accepted + job ID ngay; client poll endpoint status (hoặc nhận WebSocket/webhook khi xong). Lưu ý cập nhật 06/2024: REST API Regional và private có thể request nâng integration timeout quá 29s (đổi lại có thể bị giảm throttle quota của account); REST API edge-optimized (29s) và HTTP API (tối đa 30s) không nâng được — nhưng với đề thi, nâng timeout hiếm khi là "best answer" so với thiết kế async đúng nghĩa.
Bài tập thực hành
Phần 1: Modernization Patterns
-
Strangler Fig mini: dựng "monolith" (1 EC2/container với route
/ordersvà/users), đặt ALB trước; tách/usersthành Lambda + target group mới, đổi listener rule để/usersđi Lambda — monolith không đổi code. -
Event-driven decouple: thay call trực tiếp giữa 2 service demo bằng EventBridge (publisher bắn event
OrderCreated, consumer Lambda xử lý); tắt consumer và verify event không mất (archive + DLQ), bật lại xử lý tiếp. -
RAG với Bedrock Knowledge Bases: tạo Knowledge Base từ vài file PDF trong S3 (OpenSearch Serverless auto), hỏi qua console/API và kiểm tra citation trỏ đúng tài liệu nguồn.
Phần 2: Serverless Patterns và Best Practices
-
Queue-based load leveling: dựng API Gateway → Lambda → SQS → Lambda worker (reserved concurrency 2); bơm 500 request nhanh và quan sát queue depth tăng rồi tiêu dần — hệ thống không rớt request.
-
Saga với Step Functions: tạo state machine 3 bước (reserve inventory → charge payment → confirm) với Catch chuyển sang bước compensation (release/refund); chạy input gây fail ở bước 2 và xem nhánh rollback trong console.
-
Async API pattern: implement endpoint
POST /jobstrả 202 + jobId (ghi DynamoDB), worker xử lý 2 phút, endpointGET /jobs/{id}trả status — mô phỏng đúng đáp án "long-running task behind API Gateway".
Tài liệu tham khảo
- Modern applications on AWS
- Strangler fig pattern (AWS Prescriptive Guidance)
- Microservices on AWS
- Serverless Application Model
- Amazon Bedrock User Guide
- Bedrock Knowledge Bases
- SageMaker JumpStart
- Amazon MQ Developer Guide
- Amazon Lex Developer Guide
- Amazon Kendra Developer Guide
- AWS Lambda Developer Guide
- Amazon API Gateway
- AWS Step Functions
- Serverless Land Patterns