1.3.8 Data Caching Services
Data Caching Services
So sánh Caching Services
| ElastiCache Redis | ElastiCache Memcached | DynamoDB DAX |
|---|
| Latency | Sub-millisecond | Sub-millisecond | Microsecond |
| Persistence | ✅ (AOF, RDB) | ❌ | N/A (cache layer) |
| Replication | ✅ (Multi-AZ) | ❌ | ✅ (Multi-AZ) |
| Data types | Complex (sorted sets, lists, hashes) | Simple key-value | DynamoDB compatible |
| Clustering | ✅ | ✅ | ✅ |
| Use case | Session store, leaderboards, pub/sub | Simple caching, multi-threaded | DynamoDB read acceleration |
Caching Strategies
Lazy Loading (Cache-Aside)
App → Cache (miss) → DB → Cache (write) → App
App → Cache (hit) → App
- ✅ Chỉ cache data được request
- ✅ Node failure không critical
- ❌ Cache miss = 3 round trips (penalty)
- ❌ Stale data possible
Write-Through
App → Cache (write) → DB (write)
App → Cache (read, always hit)
- ✅ Data luôn fresh
- ❌ Write penalty (2 writes mỗi lần)
- ❌ Cache chứa data có thể không bao giờ đọc
TTL (Time to Live)
- Kết hợp với Lazy Loading hoặc Write-Through
- Expire cached data sau time period
- Balance giữa freshness và performance
DynamoDB DAX
App → DAX Cluster → DynamoDB
(microsecond) (millisecond)
- In-memory cache cho DynamoDB
- Read: GetItem, BatchGetItem, Query, Scan
- Write: Write-through (DAX → DynamoDB)
- API compatible với DynamoDB (chỉ đổi endpoint)
- Item cache + Query cache
import amazondax
# Chỉ cần đổi client
dax_client = amazondax.AmazonDaxClient(
endpoints=['dax-cluster.abc123.dax-clusters.us-east-1.amazonaws.com:8111']
)
response = dax_client.get_item(
TableName='Orders',
Key={'id': {'S': 'order-123'}}
)
ElastiCache Redis Use Cases
| Use Case | Feature |
|---|
| Session store | Key-value + TTL |
| Leaderboards | Sorted Sets |
| Rate limiting | INCR + EXPIRE |
| Pub/Sub messaging | PUBLISH/SUBSCRIBE |
| Geospatial | GEO commands |
Exam Tip: DAX = DynamoDB only, microsecond latency, API compatible. Redis = versatile (sessions, leaderboards, pub/sub). Memcached = simple, multi-threaded. Lazy Loading + TTL là combo phổ biến nhất.