1) Problem Clarification / Làm rõ bài toán
EN
Caching hugely improves performance, but stale data risks push architects to ask:
How do we keep cache & database consistent?
VI
Cache tăng tốc rất lớn, nhưng vấn đề data cũ khiến kiến trúc sư phải hỏi:
Làm sao giữ cache và database đồng bộ?
2) Why Cache Invalidation is Hard / Vì sao cache invalidate khó?
EN
- concurrent writes
- async replication delay
- distributed cache nodes
- multiple readers/writers
- failure recovery complexity
VI
Khó vì:
- ghi đồng thời
- delay trong replication
- cache phân tán
- nhiều writer/reader
- phục hồi lỗi phức tạp
3) Caching Models Overview / Các mô hình cache
EN
✔ Cache-aside (lazy load)
✔ Write-through
✔ Write-behind
✔ Read-through
VI
✔ Cache-aside (lazy load)
✔ Write-through
✔ Write-behind
✔ Read-through
4) Cache Consistency Models / Các mô hình consistency của cache
EN
Strong Consistency
Cache always in sync with DB
→ expensive & rarely necessary
Eventual Consistency
Updates propagate eventually
→ most widely used
VI
Strong consistency
cache luôn đúng với DB
→ tốn kém, ít dùng
Eventual consistency
update lan dần tới cache
→ thực tế phổ biến
5) Cache Invalidation Strategies / Chiến lược thu hồi cache
EN
A) TTL Expiry
Let cache expire after N seconds
→ simple, allows temporary staleness
B) Explicit Invalidation
App removes cache entry on DB write
UPDATE DB
DELETE CACHE
C) Update Cache On Write
Write-through pattern ensures update consistency
D) Pub/Sub Invalidation (Distributed Systems)
Publisher triggers purge message
Cache nodes receive & evict distributed entry
VI
A) Hết hạn TTL
Cache tự hết hạn sau N giây
→ đơn giản, chấp nhận stale tạm thời
B) Invalidate chủ động
Khi ghi DB → xoá cache ngay:
UPDATE DB
DELETE CACHE
C) Update cache khi ghi
Write-through giúp đồng bộ cache ngay khi ghi
D) Pub/Sub invalidation phân tán
Service publish invalidate
Cache nodes subscribe để xoá key
6) Preventing Cache Race / Tránh race condition cache
EN
Cache stampede solutions:
- mutex key
- probabilistic early refresh
- stale-while-revalidate
- soft TTL + hard TTL
VI
Cache stampede:
- khoá mutex
- refresh sớm random
- stale-while-revalidate
- soft TTL + hard TTL
7) Write Ordering Problem / Vấn đề thứ tự ghi
EN
Case: out-of-order updates between DB & cache.
Solution:
✔ versioning
✔ compare timestamps
✔ invalidate older writes
VI
Ghi out-of-order gây lỗi.
Giải pháp:
✔ version/timestamp
✔ bỏ qua update cũ
✔ invalidate logic
8) Consistency Strategy per Use Case / Mô hình phù hợp từng use case
EN
| System | Consistency |
|---|---|
| Payment | strong |
| Feed | eventual |
| Product catalog | TTL based refresh |
| Session/user profile | pub/sub invalidation |
VI
| Hệ thống | consistency |
|---|---|
| Thanh toán | strong |
| Feed | eventual |
| Catalog | TTL |
| Session/profile | pub/sub |
9) Multi-Level Cache / Cache nhiều tầng
EN
Use:
✔ local memory cache
✔ distributed Redis cache
✔ DB
Coherency managed by TTL + pub/sub.
VI
Có thể dùng:
✔ cache local
✔ cache phân tán
✔ database
Sync bằng TTL + pub/sub.
10) Write-on-Read Pattern / Ghi cache khi đọc
EN
Lazy refill works well for read-heavy workloads.
Risk: stampede → apply mitigation.
VI
Lazy refill tốt cho workload read-heavy.
Risk: stampede → cần xử lý.
11) Observability / Giám sát
EN
Metrics:
- hit ratio
- eviction
- invalidation latency
- stale read count
- hot key rate
VI
Metric:
- hit ratio
- số eviction
- latency invalidation
- số stale read
- hot key rate
12) Trade-offs Summary / Tóm tắt trade-off
EN
| Strategy | Pros | Cons |
| TTL | simple | stale |
| write-through | accuracy | write latency |
| invalidate-on-write | good balance | requires sync |
| pub/sub | scalable | message loss risk |
VI
| chiến lược | ưu | nhược |
| TTL | đơn giản | stale |
| write-through | đúng | latency |
| invalidate-on-write | cân bằng | sync complexity |
| pub/sub | scale | risk mất message |
13) Future Enhancements / Nâng cấp
EN
- ML-based TTL tuning
- consistency modelling
- cache autopilot systems
- CRDT-based cache consistency
VI
- TTL auto-tune bằng ML
- consistency modelling
- hệ thống cache autopilot
- CRDT consistency
[…] Cache Invalidation Strategies & Consistency Models […]
[…] Cache Invalidation Strategies & Consistency Models […]