</>Học Dev
Bài học

Tuần 5 - Ngày 1: RDS Fundamentals

Tuần 5 – Ngày 1

Tuần 5 - Ngày 1: RDS Fundamentals

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

  • Hiểu RDS architecture và supported engines
  • Phân biệt Multi-AZ vs Read Replicas
  • Nắm backup, snapshot, encryption
  • Biết khi nào dùng RDS vs DynamoDB vs Aurora

1. Tổng quan RDS

Amazon RDS (Relational Database Service) = managed relational database service.

Supported Engines

  • MySQL
  • PostgreSQL
  • MariaDB
  • Oracle
  • SQL Server
  • Amazon Aurora (MySQL/PostgreSQL-compatible, AWS proprietary — chi tiết ngày mai)
  • IBM Db2 (added 2023)

RDS managed features

  • Automated provisioning, patching, backup
  • Monitoring (CloudWatch)
  • Multi-AZ high availability
  • Read replicas
  • Encryption at rest (KMS), in transit (TLS)
  • Maintenance windows
  • Không SSH vào underlying EC2 (managed)

2. RDS Architecture

Single-AZ deployment

VPCAZ-aRDS(Primary)Storage(EBS)
  • NOT HA — nếu AZ fail, DB down
  • Dev/test environments

Multi-AZ deployment

VPCAZ-aAZ-bPrimaryStandby(Active)(Syncrepl)EBSEBS
  • Synchronous replication từ primary sang standby
  • Automatic failover khi primary fail (~1-2 phút)
  • DNS endpoint không đổi, app tự reconnect
  • Standby không serve read traffic (chỉ HA backup)
  • Cost gấp đôi (2 instances chạy)

Multi-AZ DB Cluster (mới 2022, MySQL & PostgreSQL)

  • 1 writer + 2 reader instances ở 3 AZs
  • Readers có thể serve read traffic (khác Multi-AZ thường)
  • Failover nhanh hơn (~35s vs 1-2 phút Multi-AZ thường)
  • Higher cost (3 instances)

3. Read Replicas

Định nghĩa

Read Replica = bản copy read-only của primary DB, scale read workload.

Đặc điểm

  • Asynchronous replication từ primary
  • lag (vài giây thường, có thể tăng nếu primary busy)
  • Tối đa 5 read replicas per primary (15 cho Aurora)
  • Có thể promote thành standalone DB (break replication)
  • Có thể cross-region (CRR cho RDS)
  • Có thể cross-account (qua snapshot copy)

Use cases

  • Scale read workload (reporting, analytics)
  • DR scenario (cross-region read replica → promote)
  • Migration (test new region với replica before cutover)

Multi-AZ vs Read Replica

Multi-AZRead Replica
PurposeHAScale reads
ReplicationSynchronousAsynchronous
Serve readsNo (standby idle)Yes
FailoverAutoManual promote
AZDifferent AZSame/different AZ or Region
Cost impact2x+1x per replica

Cross-Region Read Replica

  • Replicate sang Region khác
  • Cross-region data transfer charges apply
  • Use case: DR, regional read scaling

4. RDS Storage

Volume types

  • gp3 (mặc định mới): General SSD
  • gp2 (legacy): General SSD tied to size
  • io1: Provisioned IOPS SSD
  • Magnetic (legacy, deprecated)

Storage scaling

  • Storage Autoscaling: tự động grow khi cần (max threshold do bạn set)
  • Modify storage: increase manually anytime (no downtime)
  • Không thể decrease (cần dump + restore vào DB mới)

Maximum storage

  • MySQL, MariaDB, PostgreSQL: up to 64 TB
  • Oracle, SQL Server: up to 16 TB

5. RDS Backup

Automated Backups

  • Daily full backup + continuous transaction logs
  • Retention 0-35 days (default 7 days)
  • Stored in S3 (managed by AWS)
  • Point-in-Time Recovery (PITR): restore to any second within retention
  • Tự động delete khi DB deleted (trừ khi bạn snapshot manual trước)

Manual Snapshots

  • User-initiated, retain indefinitely until delete
  • Cross-region copy support
  • Cross-account share (qua KMS key sharing nếu encrypted)
  • Use for backup before major change, retain beyond 35 days

Restore process

  • Restore from backup/snapshot = tạo DB instance mới (không restore in-place)
  • Endpoint mới → cần update app
  • Có thể restore vào point-in-time (5-minute granularity)

Database Cloning (Aurora only)

  • Fast clone Aurora cluster (copy-on-write)
  • Use case: test environment với production data

6. RDS Encryption

At Rest

  • KMS encryption (CMK)
  • Apply ở instance creation (KHÔNG enable sau)
  • Bao gồm: storage, backups, snapshots, read replicas

Encrypt unencrypted RDS

  1. Create snapshot
  2. Copy snapshot với encryption enabled
  3. Restore DB from encrypted snapshot
  4. Migrate apps to new endpoint
  5. Delete old DB

In Transit

  • TLS/SSL connection
  • Engines support: MySQL, PostgreSQL, MariaDB, Oracle, SQL Server
  • Download CA cert từ AWS, configure client

Transparent Data Encryption (TDE)

  • Oracle, SQL Server only
  • Database-level encryption (additional layer)

7. RDS Security

Network Isolation

  • Deploy trong VPC, private subnet
  • Security Group control access port (3306, 5432, ...)
  • Không expose public unless absolutely needed

IAM Database Authentication

  • MySQL & PostgreSQL only
  • Use IAM tokens (15-min lifetime) thay password
  • No password management

Database Activity Streams (Oracle, SQL Server, PostgreSQL, MySQL)

  • Real-time stream activity logs to Kinesis
  • Audit trail cho compliance

Audit logs

  • Engine-specific audit logs gửi đến CloudWatch Logs

8. RDS Performance

Performance Insights

  • Visualize DB load và top queries
  • Free (with retention 7 days)
  • Paid (long-term retention)

Enhanced Monitoring

  • OS-level metrics (CPU per process, memory)
  • Granularity 1-60 sec
  • Costs $$ (CloudWatch logs)

Slow Query Logs

  • Engine-specific
  • Identify slow queries
  • Export to CloudWatch Logs

Instance class selection

  • db.t3/t4g: burstable, dev/test
  • db.m5/m6: general-purpose production
  • db.r5/r6: memory-optimized (in-memory cache, complex queries)
  • db.x2: high-memory (SAP HANA, in-memory analytics)

9. RDS Maintenance

Maintenance Window

  • Weekly time slot (30 min) cho AWS apply patch
  • Configurable (e.g., Sunday 3-3:30 AM)
  • Minor version auto-applied; Major version manual

Engine Version Upgrade

  • Minor upgrade: auto during maintenance window (configurable)
  • Major upgrade: manual (e.g., MySQL 5.7 → 8.0)
  • Test in staging first

10. RDS Proxy

Định nghĩa

RDS Proxy = managed connection pooler giữa app và RDS.

Lợi ích

  • Connection pooling: nhiều app connections share fewer DB connections
  • Failover faster: 60-70% giảm failover time
  • Serverless-friendly: Lambda (mỗi cold start mở connection mới) gây load lên DB
  • IAM auth integration

Use case

  • Lambda → RDS (avoid connection storms)
  • App với connection spikes
  • Faster failover for HA

Cost

  • $/vCPU-hour của proxy capacity

11. RDS vs Aurora vs DynamoDB (quick comparison)

RDSAuroraDynamoDB
TypeRelationalRelational (MySQL/PG compat)NoSQL (key-value, document)
ManagedYesYes (more so)Yes
Multi-AZStandby (idle)6 copies across 3 AZsAuto multi-AZ
Read scalingRead replicas (5)Read replicas (15)Auto
PerformanceStandard3-5x MySQLSingle-digit ms
CostLowerHigherPay-per-use or provisioned
Use caseTraditional RDBHigh-perf RDBNoSQL, high-scale

12. RDS Connection Patterns

Pattern 1: Single-AZ with Snapshots

  • Dev/test
  • Daily snapshots
  • Cheap

Pattern 2: Multi-AZ Production

  • HA primary + standby
  • Automated failover
  • Standard production

Pattern 3: Multi-AZ + Read Replicas

  • Write to primary
  • Read traffic to replicas
  • Reporting workload separate

Pattern 4: Cross-Region DR

  • Primary in Region A
  • Cross-region read replica in Region B
  • Promote replica in DR scenario

Câu hỏi ôn tập

  1. Multi-AZ standby có serve read traffic không?

    Xem đáp án

    Không — RDS Multi-AZ standby là pure failover target, không serve read traffic. Standby sync synchronously từ primary — đảm bảo zero data loss khi failover. Để scale reads, cần Read Replicas (replicate asynchronously, có thể cross-Region, và có thể promote thành standalone). Failover thường hoàn tất trong 60-120 giây — Route 53 CNAME cập nhật.

  2. Tối đa bao nhiêu read replicas per RDS primary?

    Xem đáp án

    5 read replicas per RDS primary instance (cho MySQL, MariaDB, PostgreSQL, Oracle). Mỗi read replica có thể có thêm read replicas của nó (cascading). Aurora hỗ trợ đến 15 read replicas per cluster. Read Replicas có thể ở cross-Region — hỗ trợ geographic distribution và DR.

  3. Khác biệt giữa automated backup và manual snapshot?

    Xem đáp án

    Automated backup: retention 1-35 ngày, tự động xóa khi hết retention period hoặc khi xóa DB instance, enable point-in-time recovery (PITR). Manual snapshot: retention vĩnh viễn cho đến khi bạn xóa thủ công, ngay cả sau khi xóa DB. Dùng manual snapshot trước khi major upgrade, xóa DB, hoặc khi cần backup lâu dài hơn 35 ngày.

  4. Để encrypt RDS đã chạy unencrypted, cần qua bước gì?

    Xem đáp án

    Không thể encrypt trực tiếp. Phải: (1) Tạo snapshot của instance unencrypted, (2) Copy snapshot với encryption (chọn KMS key), (3) Restore từ encrypted snapshot thành new DB instance, (4) Update connection string của ứng dụng. Optional: xóa DB cũ sau khi verify. Downtime = thời gian restore (có thể dùng read replica để giảm downtime).

  5. RDS Proxy giải quyết vấn đề gì cho Lambda?

    Xem đáp án

    Connection pooling. Lambda functions stateless và scale rapidly — mỗi Lambda invocation có thể mở DB connection mới, gây connection exhaustion khi hàng nghìn Lambda chạy đồng thời (RDS max connections giới hạn). RDS Proxy duy trì connection pool đến DB, Lambda kết nối đến Proxy — Proxy multiplexes nhiều Lambda connections qua ít DB connections thực sự. Cũng cải thiện failover time (failover trong Proxy, Lambda không thấy).

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

  • Tạo RDS MySQL db.t3.micro Single-AZ, enable backup 7 days
  • Modify → enable Multi-AZ, observe failover behavior (force reboot with failover)
  • Tạo Read Replica trong cùng AZ và cross-region
  • Test PITR: restore DB to 1 hour ago
  • Enable Performance Insights, observe top queries
  • Setup RDS Proxy for Lambda integration

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


Tiếp theo: Aurora Deep Dive