Database
Amazon ElastiCache
In-Memory Caching, Redis, Memcached, Session Store
Tổng quan
Amazon ElastiCache là dịch vụ in-memory caching được quản lý hoàn toàn, giúp tăng tốc ứng dụng bằng cách lưu trữ data trong RAM thay vì disk.
Tại sao cần Cache?
Ví dụ đời thường:
Bạn hay uống cà phê mỗi sáng. Thay vì đi cafe 2km mỗi ngày (= query database), bạn mua máy pha cà phê để ở nhà (= cache). Nhanh hơn và tiện hơn!
Hai loại Engine
ElastiCache hỗ trợ 2 engines:
| Feature | Redis | Memcached |
|---|---|---|
| Data Types | Strings, Lists, Sets, Hashes, Sorted Sets, Streams, Geospatial | Chỉ Key-Value (Strings) |
| Persistence | ✅ Có (snapshots, AOF) | ❌ Không (mất khi restart) |
| High Availability | ✅ Multi-AZ Auto-Failover | ❌ Không có |
| Replication | ✅ Read Replicas | ❌ Không |
| Pub/Sub | ✅ Có | ❌ Không |
| Multi-threading | Single-threaded | ✅ Multi-threaded |
| Scaling | Cluster Mode (up to 500 nodes) | Up to 20 nodes |
| Use case | Production, HA, complex data | Simple caching, large objects |
Chọn cái nào?
[!TIP] Mặc định:, chọn Redis cho hầu hết use cases. Memcached chỉ dùng khi có lý do cụ thể.
ElastiCache for Redis - Chi tiết
Architecture
Cluster Mode
| Mode | Mô tả | Max Data |
|---|---|---|
| Cluster Mode Disabled | 1 primary + up to 5 replicas | ~310 GB |
| Cluster Mode Enabled | Multiple shards, mỗi shard có primary + replicas | Up to 500 nodes, multi-TB |
Cluster Mode Disabled
- Primary Endpoint (1): Kết nối đến Primary node → R/W
- Reader Endpoint (1): Load-balanced đến tất cả Replicas → Read only
- Replicas hỗ trợ READ → Scale reads, giảm load cho Primary
- KHÔNG scale writes (chỉ có 1 Primary)
Cluster Mode Enabled (Sharding)
- Configuration Endpoint (1): Client chỉ cần 1 endpoint, Redis tự route
- Multiple Primaries → Scale WRITES (mỗi shard có 1 Primary)
- Multiple Replicas → Scale reads
- Data được hash và phân chia qua các shards
So sánh Cluster Mode
| Feature | Cluster Mode Disabled | Cluster Mode Enabled |
|---|---|---|
| Shards | 1 | 1 - 500 |
| Write nodes | 1 Primary only | Multiple Primaries |
| Scale writes | ❌ | ✅ |
| Scale reads | ✅ (Replicas) | ✅ (Replicas) |
| Max data | ~310 GB | Multi-TB |
| Endpoints cần dùng | 2 (Primary + Reader) | 1 (Configuration) |
| Replicas per shard | 0-5 | 0-5 |
Data Types trong Redis
ElastiCache for Memcached - Chi tiết
Architecture
Đặc điểm:
- Không có replication (nếu 1 node fail → mất data của node đó)
- Application tự hash key để chọn node
- Scale out bằng cách thêm nodes
Use Cases
1. Database Caching (Read-Through)
2. Session Store
3. Real-time Leaderboard
4. Pub/Sub Messaging
5. Rate Limiting
ElastiCache Serverless (2024)
AWS giờ có Serverless option cho cả Redis và Memcached:
Security
Encryption
| Type | Description |
|---|---|
| At-rest encryption | Data encrypted trên disk (Redis snapshots) |
| In-transit encryption | TLS/SSL giữa app và ElastiCache |
Access Control
Caching Strategies
1. Lazy Loading (Cache-Aside)
| Ưu điểm | Nhược điểm |
|---|---|
| Chỉ cache data thực sự cần | Cache miss = chậm (3 trips) |
| Tiết kiệm memory | Data có thể stale |
| Node fail không ảnh hưởng app | Initial request luôn chậm |
2. Write-Through
| Ưu điểm | Nhược điểm |
|---|---|
| Cache luôn up-to-date | Write chậm hơn (2 writes) |
| Read không bao giờ miss | Cache data không dùng đến |
| Không có stale data | Tốn memory hơn |
3. TTL (Time-To-Live)
| Ưu điểm | Nhược điểm |
|---|---|
| Tự động cleanup stale data | Data có thể stale trước TTL |
| Kết hợp được với Lazy Loading | Cần chọn TTL phù hợp |
| Giảm memory usage | TTL quá ngắn = nhiều cache miss |
4. Write-Around (Cache Invalidation)
| Ưu điểm | Nhược điểm |
|---|---|
| Tránh cache data không dùng | Read sau write sẽ chậm (miss) |
| Đơn giản, dễ implement | Cần kết hợp với Lazy Loading |
| Tiết kiệm cache memory | Có thể có race condition |
5. Write-Behind (Write-Back)
| Ưu điểm | Nhược điểm |
|---|---|
| Write cực nhanh (chỉ write cache) | Có thể mất data nếu cache fail |
| Giảm load cho DB | Phức tạp hơn để implement |
| Có thể batch writes | Data trong DB có thể stale tạm thời |
[!WARNING] Write-Behind có risk mất data nếu cache node fail trước khi sync xuống DB. Chỉ dùng khi chấp nhận được eventual consistency.
6. So sánh Strategies
| Strategy | On Write | On Read | Data Freshness | Best For |
|---|---|---|---|---|
| Lazy Loading | - | Miss → Query DB → Cache | Có thể stale | Read-heavy |
| Write-Through | Write Cache + DB | Always hit | Luôn fresh | Data quan trọng |
| Write-Around | Delete Cache + Write DB | Miss → Lazy load | Fresh khi read | Write-heavy |
| Write-Behind | Write Cache only | Always hit | Eventual | High write throughput |
| TTL | - | Auto-expire | Controlled | Kết hợp với strategies khác |
[!TIP] Common Patterns:
- Read-heavy apps: Lazy Loading + TTL
- Write-heavy apps: Write-Around + Lazy Loading
- Real-time apps: Write-Through
- High throughput: Write-Behind (cẩn thận với data loss)
Pricing
| Component | Redis | Memcached |
|---|---|---|
| Node pricing | Per node-hour | Per node-hour |
| Smallest node | cache.t4g.micro (~$12/month) | cache.t4g.micro (~$12/month) |
| Reserved | Up to 55% discount | Up to 55% discount |
| Serverless | GB-hours + ECPUs | GB-hours + ECPUs |
So sánh với các Caching Options khác
| Option | Managed | Use Case |
|---|---|---|
| ElastiCache Redis | ✅ | Production, HA, complex features |
| ElastiCache Memcached | ✅ | Simple caching, multi-threaded |
| DynamoDB DAX | ✅ | DynamoDB-specific caching |
| CloudFront | ✅ | Edge caching (CDN) |
| Self-managed Redis | ❌ | Full control (EC2) |
Exam Tips cho Cloud Practitioner
[!IMPORTANT] Keywords để nhớ ElastiCache:
- In-memory caching → Microsecond latency
- Redis = HA, persistence, complex data types (chọn mặc định)
- Memcached = Simple, multi-threaded, no persistence
- Reduce database load, improve performance
Câu hỏi thường gặp
| Câu hỏi | Trả lời |
|---|---|
| Service nào cho in-memory caching? | ElastiCache |
| Redis hay Memcached cho HA? | Redis (Multi-AZ Failover) |
| Redis hay Memcached cho persistence? | Redis (snapshots, AOF) |
| Lưu session data cho multiple EC2s? | ElastiCache (Redis) |
| Cần Pub/Sub messaging? | Redis |
| Simple key-value, có thể mất data? | Memcached |