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:
- TTL expiry
- Explicit invalidation on application write
- Pub/sub invalidation bus
VI
Cách invalidate:
- TTL
- invalidation khi ghi DB
- 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
| Feature | Redis | Memcached | Aerospike |
|---|---|---|---|
| Persistence | Optional | No | Yes |
| Data Types | Rich | Simple | Rich |
| Perf | High | Very High | Very High |
| Consistency | Good | Weak | Strong HA |
| Use Case | app cache, queue | ephemeral cache | ultra low latency DB/cache |
VI
| Tính năng | Redis | Memcached | Aerospike |
|---|---|---|---|
| Persistence | Có tùy chọn | Không | Có |
| Data type | phong phú | đơn giản | phong phú |
| Hiệu năng | cao | rất cao | rất cao |
| Consistency | tốt | yếu | mạnh |
| Use case | cache app, queue | cache ephemeral | cache 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
[…] Designing A Distributed Cache System (Redis / Memcached / Aerospace) […]