devHọc Dev
Bài học

Tuần 6 - Ngày 2: RDS & Aurora Deep Dive

Tuần 6 – Ngày 2

Mục tiêu học tập

  • Phân biệt RDS Multi-AZ (HA) với Read Replicas (scale/DR) và biết giới hạn từng engine
  • Hiểu khi nào cần RDS Proxy (connection pooling cho Lambda/serverless)
  • Nắm Aurora Serverless v2, Aurora Cloning và các thông số cập nhật 2024–2025

1. RDS Multi-AZ

RDSMULTI-AZAZ-aAZ-bPrimaryStandby(Active)Sync(Passive)ReplFailovertriggers:-Primaryinstancefailure-AZfailure-Instancetypechange-Manualfailover(forpatching)Failovertime:60-120seconds

2. RDS Read Replicas

Số read replicas tối đa:
- Aurora: 15
- RDS MySQL/MariaDB/PostgreSQL: 15 (nâng từ 5 lên 15 năm 2022)
- RDS Oracle/SQL Server: 5

Cross-Region Read Replica:
- Async replication
- Different region for DR
- Can be promoted to standalone

Read Replica Use Cases:
1. Read scaling
2. Reporting/Analytics
3. DR (promote if needed)

3. RDS Proxy

RDSPROXYProblem:ManyLambdafunctions=ManyconnectionsLambda1Lambda2LambdaNRDSProxyConnectionpooling(Managed)AutofailoverLimitedconnectionsRDS/Aurora

4. Aurora Features

Aurora Serverless v2

Scaling:
- Min: 0 ACU (auto-pause, từ 11/2024;
  trước đó min 0.5 ACU)
- Max: 256 ACU (từ 10/2024; trước đó 128)
- Scale in seconds

Pricing:
- Per ACU-hour
- Storage separate

Use cases:
- Variable workloads
- Dev/Test
- Multi-tenant SaaS

Aurora Machine Learning

Integrate with ML services from SQL:
- SageMaker (custom models)
- Comprehend (sentiment)

SELECT
  review,
  aws_comprehend.detect_sentiment(review) as sentiment
FROM reviews;

5. Database Cloning

Aurora Cloning:
- Copy-on-write (instant)
- No storage copied initially
- Independent after creation

Use cases:
- Testing with production data
- Analytics without impacting prod
- Debugging

6. RDS/Aurora IAM Database Authentication

Thay vì lưu password trong ứng dụng, dùng IAM credentials để kết nối database — mỗi lần connect sinh một auth token ngắn hạn từ IAM.

IAMDATABASEAUTHENTICATIONFLOW1.generate-db-auth-tokenApp(EC2/IAMLambdarole)2.Authtoken(TTL15phút,kýbngSigV4)3.Connect:user+tokenthaypassword(BTBUCTLS/SSL)4.RDSverifytokenviIAMRDS/Aurorachophépkếtni

Điểm chính:

Khía cạnhChi tiết
Engine hỗ trợMySQL, PostgreSQL, MariaDB (cả RDS lẫn Aurora) — Oracle/SQL Server KHÔNG hỗ trợ
TokenSinh từ SDK/CLI: aws rds generate-db-auth-token, hết hạn sau 15 phút, chỉ dùng để mở connection (connection đang mở không bị cắt khi token hết hạn)
TransportBắt buộc TLS/SSL in-transit
IAM policyAction rds-db:connect, resource là db-user ARN: arn:aws:rds-db:region:account:dbuser:DbiResourceId/db_username
DB userVẫn phải tạo user trong database map với IAM: MySQL/MariaDB CREATE USER ... IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS'; PostgreSQL GRANT rds_iam TO user

Ví dụ IAM policy:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": "rds-db:connect",
    "Resource": "arn:aws:rds-db:ap-northeast-1:123456789012:dbuser:db-ABCDEFGHIJKL/app_user"
  }]
}

Lợi ích (Professional angle):

  • Không lưu credentials trong code/config — không cần rotate password thủ công
  • Tận dụng IAM: rotation tự động qua role, MFA, centralize access control cho nhiều database qua IAM policy
  • Audit qua CloudTrail

Exam keywords: "avoid storing database passwords", "use IAM credentials to connect to the database" → IAM Database Authentication. Phân biệt: nếu đề nói "rotate credentials automatically" cho engine không hỗ trợ IAM auth (Oracle/SQL Server) hoặc app cần password thật → Secrets Manager rotation.

7. Aurora Custom Endpoints

Aurora có 4 loại endpoint:

EndpointTrỏ tớiUse case
Cluster (writer)Primary instance hiện tạiWrite traffic; tự động chuyển khi failover
ReaderLoad-balance qua TẤT CẢ replicasRead traffic chung
Instance1 instance cụ thểDebug, ít dùng cho app
CustomNhóm CON các instance chỉ địnhTách workload theo nhóm replica

Custom endpoint cho phép chỉ định tập con instance — ví dụ 2 replica cỡ lớn (r6g.8xlarge) dành riêng cho analytics, các replica nhỏ cho app đọc thường — hai workload không ảnh hưởng lẫn nhau:

AURORACUSTOMENDPOINTSClusterWriterendpointCustomendpointCustomendpoint"app-read""analytics"Repl1Repl2Rep3Repl4Repl5smallsmallsmllr6g.8xlr6g.8xlAppqueriesAnalytics/Reporting(OLTPreads)(heavyqueries)

Lưu ý:

  • Sau khi tạo custom endpoint, thường không dùng reader endpoint nữa cho workload đó (reader endpoint vẫn load-balance qua TẤT CẢ replicas, kể cả nhóm analytics)
  • Membership định nghĩa theo static list (danh sách instance) hoặc exclusion list; instance mới thêm vào cluster có thể tự động join tuỳ cấu hình
  • Tối đa 5 custom endpoints/cluster

Exam keyword: "run analytics/reporting queries trên một subset replicas riêng, không ảnh hưởng read traffic của app" → Aurora custom endpoint (không phải reader endpoint, không cần tạo cluster mới).

8. Câu hỏi ôn tập

  1. Multi-AZ khác Read Replica như thế nào?

    Xem đáp án

    Multi-AZ: mục tiêu là HA — standby sync replication, không nhận read traffic (trừ Multi-AZ DB Cluster 2 readable standbys), failover tự động 60–120s, cùng endpoint. Read Replica: mục tiêu là read scaling / DRasync replication, nhận read traffic, có thể cross-region, promote thủ công thành standalone. Câu hỏi exam hay bẫy: "increase availability" → Multi-AZ; "offload reporting queries" → Read Replica; "DR sang region khác với RPO thấp" → cross-region read replica (hoặc Aurora Global Database).

  2. Số read replicas tối đa của từng engine?

    Xem đáp án

    Aurora: 15 replicas (chung storage volume, lag ~ms, có thể làm failover target theo priority tier). RDS MySQL/MariaDB/PostgreSQL: 15 (nâng từ 5 lên 15 năm 2022 — tài liệu cũ ghi 5 là lỗi thời). RDS Oracle/SQL Server: 5. Read replica RDS dùng engine-level async replication nên lag cao hơn Aurora.

  3. Khi nào cần RDS Proxy?

    Xem đáp án

    Khi có nhiều connection ngắn hạn làm cạn max_connections — điển hình là Lambda scale hàng nghìn concurrent executions cùng mở connection. RDS Proxy làm connection pooling managed, multiplexing, giảm failover time tới ~66% (giữ connection khi failover), hỗ trợ IAM auth + Secrets Manager. Keyword: "Lambda + RDS + too many connections" hoặc "reduce database failover time" → RDS Proxy.

  4. Aurora Cloning hoạt động thế nào và khác restore snapshot ra sao?

    Xem đáp án

    Cloning dùng copy-on-write trên shared storage layer — clone tạo gần như tức thì, không copy dữ liệu ban đầu, chỉ ghi page mới khi clone hoặc source thay đổi. Restore từ snapshot phải copy toàn bộ dữ liệu → chậm và tốn storage hơn. Dùng clone cho test trên production data, analytics ad-hoc, debugging. Giới hạn: clone trong cùng region (cross-account được qua RAM sharing).

  5. Ứng dụng trên EC2 cần kết nối Aurora MySQL mà "không lưu database password". Giải pháp và các bước cấu hình?

    Xem đáp án

    IAM Database Authentication. Các bước: (1) bật IAM DB auth trên cluster; (2) tạo DB user map với IAM: CREATE USER 'app_user' IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS' (PostgreSQL: GRANT rds_iam); (3) gắn IAM policy cho EC2 instance role với action rds-db:connect trên db-user ARN; (4) app gọi generate-db-auth-token lấy token 15 phút và connect qua TLS với token thay password. Chỉ hỗ trợ MySQL/PostgreSQL/MariaDB. Keyword: "avoid storing database passwords", "use IAM credentials to connect" → IAM DB auth.

  6. Team analytics chạy query nặng trên Aurora làm chậm read traffic của app. Làm sao tách 2 workload mà không tạo cluster mới?

    Xem đáp án

    Tạo Aurora custom endpoint: thêm 2 replica cỡ lớn dành riêng cho analytics và gom chúng vào 1 custom endpoint; app đọc qua custom endpoint khác (hoặc reader endpoint nếu đã exclude nhóm analytics — nhưng lưu ý reader endpoint mặc định load-balance qua TẤT CẢ replicas, nên thường tạo 2 custom endpoints và bỏ reader endpoint cho các workload này). Kết quả: query analytics chỉ chạy trên subset replicas chỉ định, không ảnh hưởng OLTP reads. Keyword: "reporting/analytics trên subset replicas riêng" → custom endpoint.

9. Bài tập thực hành

  1. Failover Multi-AZ: tạo RDS MySQL Multi-AZ (db.t3.micro), chạy reboot with failover, đo thời gian client mất kết nối và verify endpoint không đổi.

  2. RDS Proxy + Lambda: tạo RDS Proxy trước database trên, trỏ Lambda qua proxy endpoint với IAM auth; chạy 200 invocation đồng thời và so sánh số connection trên DB (SHOW PROCESSLIST) khi đi thẳng vs qua proxy.

  3. Aurora clone: tạo Aurora cluster nhỏ, insert dữ liệu mẫu, tạo clone và đo thời gian tạo; sửa dữ liệu trên clone và verify source không đổi.


Tài liệu tham khảo chính thức


Ngày tiếp theo: DynamoDB Deep Dive