1.3.8 Data Caching Services

Data Caching Services

So sánh Caching Services

ElastiCache RedisElastiCache MemcachedDynamoDB DAX
LatencySub-millisecondSub-millisecondMicrosecond
Persistence✅ (AOF, RDB)N/A (cache layer)
Replication✅ (Multi-AZ)✅ (Multi-AZ)
Data typesComplex (sorted sets, lists, hashes)Simple key-valueDynamoDB compatible
Clustering
Use caseSession store, leaderboards, pub/subSimple caching, multi-threadedDynamoDB 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 CaseFeature
Session storeKey-value + TTL
LeaderboardsSorted Sets
Rate limitingINCR + EXPIRE
Pub/Sub messagingPUBLISH/SUBSCRIBE
GeospatialGEO 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.