Database
RDS Read-Write Splitting Patterns
Multi-AZ Cluster Endpoints, Read Replica Load Balancing, Spring Boot Examples
Tổng quan
RDS có nhiều cách để scale reads và đảm bảo high availability, mỗi cách có cách sử dụng endpoints khác nhau:
| Deployment | Endpoints | Load Balance |
|---|---|---|
| Single-AZ | 1 | - |
| Multi-AZ Instance | 1 | AWS (failover) |
| Multi-AZ Cluster | 2 (Writer + Reader) | AWS tự động |
| Read Replicas | 1 + N (mỗi replica 1 EP) | Tự làm |
Sync vs Async Replication
Synchronous Replication (Multi-AZ Standby)
Asynchronous Replication (Read Replica)
So sánh
| Multi-AZ Standby | Multi-AZ Cluster Readers | Read Replica | |
|---|---|---|---|
| Có thể đọc? | ❌ KHÔNG | ✅ CÓ | ✅ CÓ |
| Replication | Sync | Semi-sync (~10-20ms) | Async (seconds-minutes) |
| Auto failover? | ✅ Có | ✅ Có | ❌ Không (manual) |
| Mục đích | HA only | HA + Read scaling | Read scaling |
Multi-AZ Cluster: 2 Endpoints
Traffic Flow
Khi nào dùng endpoint nào?
| Operation | Dùng Endpoint nào? | Tại sao? |
|---|---|---|
INSERT, UPDATE, DELETE | WRITER | Chỉ Writer mới ghi được |
SELECT general (listings, reports) | READER | Phân tải, chấp nhận lag nhỏ |
SELECT ngay sau WRITE | WRITER | Tránh stale read |
Read Replicas: Multiple Endpoints
Giải pháp Load Balancing cho Read Replicas
| Giải pháp | Khi nào dùng? |
|---|---|
| RDS Proxy | Serverless (Lambda), cần connection pooling |
| Route 53 Weighted | Multi-region, DNS-based routing |
| Application-level | Đơn giản, ít replicas |
RDS Proxy (Recommended)
Route 53 Weighted Routing
Spring Boot Implementation
application.yml
DataSource Config
Custom @ReadOnly Annotation
Service Usage
Stale Read Problem
Với Async Replication (Read Replica), có thể xảy ra Stale Read:
Giải quyết
| Read Type | Đọc từ đâu? |
|---|---|
| User vừa update → xem lại | Primary/Writer |
| Dashboard analytics | Replica/Reader (chấp nhận lag) |
| Order vừa đặt → xem status | Primary/Writer |
| Product listing | Replica/Reader |
Best Practices
-
Multi-AZ Cluster > Read Replicas nếu cần cả HA và Read scaling với auto load balance
-
Read-after-write từ Writer để tránh stale read
-
Connection pooling riêng cho mỗi endpoint
-
RDS Proxy cho Serverless (Lambda) workloads
-
Aurora nếu chưa chọn engine - có sẵn Reader endpoint tự động
Exam Tips
- Standby = Sync, Read Replica = Async
- Multi-AZ Cluster có 2 endpoints (Writer + Reader)
- Read Replicas không có chung Reader endpoint
- RDS Proxy hoặc Route 53 để load balance Read Replicas
- Aurora có sẵn Reader endpoint như Multi-AZ Cluster