Cache can go out of sync from the source, hence Cache invalidation is needed
To ensure that only the most recent and accurate information is stored
Cache Invalidation Schemes
Write-Through cache
Write ⇒ cache ⇒ storage
Higher latency for write operations
Examples
Update product info in E-commerce website
Write-Around cache
Write ⇒ storage (cache written on read request)
Write to cache happens when read request is hit
Faster write operations
First read request (Recent data) is always cache miss
Examples
Update user profile
Write-Back cache (aka lazy-write)
Write ⇒ cache (storage written on certain conditions)
Faster write operations
Risk of data loss
Examples
Collaborative text editor where the changes are persisted when certain conditions like number of changes reach threshold, write to storage happens
Write-Behind cache
Write ⇒ cache (storage written on periodic intervals)
Faster write operations
Risk of data loss
Examples
Text editor temporarily where changes are written back to the storage periodically to minimize the number of write operations.
flowchart TD
classDef writeThrough fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px,color:#000
classDef writeAround fill:#fff9c4,stroke:#f57f17,stroke-width:2px,color:#000
classDef writeBack fill:#ffe0b2,stroke:#e65100,stroke-width:2px,color:#000
classDef writeBehind fill:#bbdefb,stroke:#0d47a1,stroke-width:2px,color:#000
A["🏰 Write-Through<br><sub>Always update DB & cache at same time</sub>"] --> B["🏞️ Write-Around<br><sub>Update DB only, let cache load on next read</sub>"]
B --> C["🏠 Write-Back<br><sub>Update cache first; persist to DB later, often batched</sub>"]
C --> D["🏗️ Write-Behind<br><sub>Queue writes in cache, asynchronously push to DB in background</sub>"]
class A writeThrough
class B writeAround
class C writeBack
class D writeBehind
Cache eviction
Cache has a finite size, hence Cache eviction is necessary
Popular Cache Eviction Policies
LRU (Least Recently Used)
When recently used items are more likely to be used again.
Examples:
User Session cache
Recent search queries
web caches and CDNs
LFU (Least Frequently Used)
Popular items are accessed repeatedly over time.
Long lived hot data
Examples
E-commerce site best sellers stay cached longer than rarely viewed items