Redis and Distributed Caching Patterns

Mental model

Caching là time-space trade-off: hệ thống trả thêm memory, invalidation và vận hành để mua latency thấp hơn. Redis là cache phổ biến vì nó còn cung cấp data structures và primitive real-time, nhưng khi cache scale ra nhiều node, vấn đề trở thành distributed systems: sharding, replication, consistency, failure và observability.

Các lớp thiết kế

LớpConceptCâu hỏi thiết kế
EngineRedis, Redis Event Loop, Redis Persistence, Redis Data StructuresVì sao Redis nhanh, bền tới mức nào, lệnh nào an toàn trên hot path?
Cache topologyDistributed Cache, Consistent Hashing, Data ReplicationCache nằm local, dedicated node, hay managed service? Key được chia và replicate thế nào?
Read/write policyCache-Aside, Read-Through Cache, Write-Through Cache, Write-Behind CacheApp hay cache layer chịu trách nhiệm miss/write? Đổi latency lấy consistency ra sao?
FreshnessCache Invalidation, Cache Eviction Policy, Cache Warmup, Cache StampedeKhi nào cache entry hết tin cậy, bị loại, hoặc cần preload?
Redis primitivesRedis Streams, Redis Sorted Set, HyperLogLog, Distributed LockCó nên dùng Redis cho queue, leaderboard, approximate analytics hoặc lock không?
ML hot pathFeature Store Cache, AI Model ServingFeature lookup có đang làm inference chậm hoặc tốn Redis quá mức không?

Bài học

  • Redis không thay database chính; trong cache pattern thông thường, database vẫn là source of truth.
  • Cache hit rate cao chỉ có ý nghĩa khi freshness, memory cost và failure mode được kiểm soát.
  • Local cache giảm network hop nhưng làm consistency và rollout khó hơn.
  • Distributed cache cần observability riêng: hit rate, miss rate, latency, memory, eviction, stale read và backend fallback.
  • Redis Streams, Sorted Set, HyperLogLog và lock rất tiện, nhưng mỗi primitive có giới hạn về reliability, scale hoặc correctness.

Liên kết