Cache-aside (Lazy Loading): application tự quản lý cache – trước tiên check cache, nếu miss thì đọc từ DB và populate cache (cache-aside pattern).
- Ưu: chỉ cache dữ liệu thực sự được đọc, cache failure không block read; Nhược: cache miss đầu tiên luôn có extra latency, dữ liệu cache có thể stale nếu DB được update trực tiếp.
- Write-through: mỗi write đồng thời cập nhật cả cache và DB trước khi trả response.
- Ưu: cache luôn fresh, không bao giờ stale; Nhược: write latency cao hơn, cache dữ liệu ít được đọc lãng phí memory.
- Write-back (Write-behind): write chỉ vào cache trước, trả response ngay, sau đó async flush xuống DB.
- Ưu: write latency rất thấp, tốt cho write-heavy workloads; Nhược: data loss risk nếu cache crash trước khi flush, phức tạp hơn.
- Redis thường dùng cho cả 3 pattern; cache-aside là phổ biến nhất trong thực tế vì đơn giản và phù hợp hầu hết use case.
Cache-aside (Lazy Loading): the application manages the cache itself — check the cache first, and on a miss, read from the database and populate the cache.
Pros: only caches data that is actually read; cache failure does not block reads.
Cons: the first cache miss always incurs extra latency; cache data can become stale if the database is updated directly. Write-through: every write synchronously updates both the cache and the database before returning a response.
Pros: cache is always fresh and never stale.
Cons: higher write latency; memory is wasted caching rarely-read data. Write-back (Write-behind): writes go to the cache first, the response is returned immediately, and the cache is asynchronously flushed to the database.
Pros: very low write latency, great for write-heavy workloads.
Cons: risk of data loss if the cache crashes before flushing; more complex to implement. Redis is commonly used for all three patterns; cache-aside is most prevalent in practice due to its simplicity and suitability for most use cases.