Skip to content

Designing A Distributed Cache System (Redis / Memcached / Aerospace)

1) Problem Clarification / Làm rõ bài toán

EN

We need a distributed caching layer that accelerates read-heavy workloads and reduces database load.

Questions:

  • What workloads are cached?
  • Is cache consistency required?
  • How large is cache storage?
  • Do we need eviction policies?

VI

Thiết kế lớp cache phân tán tăng tốc truy vấn, giảm tải database.

Câu hỏi cần làm rõ:

  • Cache dữ liệu nào?
  • Có cần consistency cao không?
  • Dung lượng cache bao nhiêu?
  • Chính sách eviction nào?

2) Requirements / Yêu cầu hệ thống

EN – Functional

✔ Key-value lookup
✔ TTL expiration
✔ Eviction strategy
✔ Write policy (write-through or cache-aside)
✔ Cache invalidation

VI – Chức năng

✔ lưu key-value
✔ TTL
✔ eviction
✔ write policy
✔ cache invalidation

EN – Non-functional

✔ Low latency (~1–2ms)
✔ Highly available
✔ Horizontally scalable
✔ Fault tolerant

VI – Phi chức năng

✔ latency thấp (1–2ms)
✔ HA
✔ scale ngang
✔ fault tolerant

3) Architecture Overview / Kiến trúc tổng quan

Client → Application → Cache Layer → Database
                     ↘ Eviction + Replication

VI

App sẽ đọc cache trước, nếu miss mới query DB.

4) Cache Strategies / Chiến lược cache

EN

Cache-aside (lazy loading)

  • Most common
  • App reads cache first; miss → fetch DB → fill cache

Write-through

  • Update cache + DB synchronously

Write-behind

  • Update cache only, flush to DB asynchronously

VI

Cache-aside (phổ biến nhất)

  • đọc cache trước, miss → DB → fill cache

Write-through

  • ghi đồng bộ cache + DB

Write-behind

  • ghi cache trước, flush DB sau

5) Eviction Strategies / Chiến lược loại bỏ key

EN

  • LRU (most common)
  • LFU (frequency-based)
  • TTL expiration
  • Random eviction

VI

  • LRU (thường dùng)
  • LFU
  • TTL
  • random

6) Cache Key Design / Thiết kế key

EN

A good key design avoids:

  • collisions
  • hot keys

Pattern:

<tenant>:<entity>:<id>

VI

Thiết kế key tốt tránh:

  • trùng key
  • hot key

Pattern:

<tenant>:<entity>:<id>

7) Sharding & Partitioning / Phân mảnh

EN

Distributed cache must shard data across nodes:

Techniques:

  • consistent hashing
  • mod hashing
  • node hash ring

VI

Cache phân tán cần shard dat across nodes:

  • consistent hashing
  • mod hashing
  • hash ring

8) Replication / Sao lưu dữ liệu cache

EN

Redis Cluster copies data to slaves, enabling:

✔ HA
✔ read scaling
✔ failover

VI

Redis Cluster có replica hỗ trợ:

✔ HA
✔ read scaling
✔ failover

9) Handling Failures / Xử lý lỗi

EN

Cache failure should not crash the system.

Fallback strategies:

  • degrade to DB reads
  • circuit breaker if DB overload risk
  • partial availability fallback

VI

Cache chết thì hệ thống vẫn phải chạy được.

Fallback:

  • fallback DB
  • circuit breaker nếu DB quá tải
  • degrade mode

10) Cache Stampede / Hiện tượng stampede

EN

Problem: many requests simultaneously fill the cache after TTL expiry.

Solutions:

  • mutex key
  • dog-pile prevention
  • probabilistic TTL refresh
  • request coalescing

VI

Vấn đề: nhiều request cùng lúc refill cache khi TTL hết hạn.

Giải pháp:

  • mutex key
  • tránh dog-pile
  • probabilistic TTL
  • gom request

11) Cache Invalidation / Đồng bộ hoá cache

EN

Options:

  1. TTL expiry
  2. Explicit invalidation on application write
  3. Pub/sub invalidation bus

VI

Cách invalidate:

  1. TTL
  2. invalidation khi ghi DB
  3. publish invalidation qua pub/sub

12) Hot-Keys Handling / Xử lý hot key

EN

Problem: celebrity/viral data causes overload

Solutions:

  • local memory cache layer
  • key replication across shards
  • caching substructures (e.g., embedding lists)

VI

Hot key làm quá tải:

Giải pháp:

  • local cache
  • nhân bản key qua shard
  • cache substructure

13) Comparing Redis, Memcached, Aerospike

EN

FeatureRedisMemcachedAerospike
PersistenceOptionalNoYes
Data TypesRichSimpleRich
PerfHighVery HighVery High
ConsistencyGoodWeakStrong HA
Use Caseapp cache, queueephemeral cacheultra low latency DB/cache

VI

Tính năngRedisMemcachedAerospike
PersistenceCó tùy chọnKhông
Data typephong phúđơn giảnphong phú
Hiệu năngcaorất caorất cao
Consistencytốtyếumạnh
Use casecache app, queuecache ephemeralcache DB tốc độ cực cao

14) Observability / Giám sát cache

EN

Metrics:

  • hit ratio
  • eviction count
  • latency
  • replication delays
  • memory fragmentation

VI

Metric:

  • cache hit
  • số eviction
  • latency
  • delay replicate
  • memory fragmentation

15) Future Enhancements / Mở rộng

EN

  • auto-warm cache
  • adaptive TTL
  • ML-driven eviction
  • distributed lock service

VI

  • auto-warm cache
  • TTL thích nghi
  • ML eviction thông minh
  • dịch vụ lock phân tán
Published inAll

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *