Caching Patterns
Strategies for storing frequently accessed data closer to the consumer, reducing latency and backend load.
// concept
Caching stores copies of data in fast-access storage layers. The challenge lies in choosing what to cache, when to evict, and how to keep cached data consistent with the source of truth.
LRU Cache
Evict the least recently used item when capacity is reached. O(1) get/put with a hash map + doubly linked list.
LFU Cache
Evict the least frequently used item. Tracks access counts to keep the hottest data in memory.
TTL Cache
Entries expire after a time-to-live. Ensures stale data is automatically purged without manual invalidation.
Cache Aside
Application checks cache first, loads from DB on miss, then populates cache. Most common caching strategy.
Write-Through/Back
Write-through writes to cache and DB synchronously. Write-back buffers writes for eventual persistence.
Cache Stampede
Prevent thundering herd when a popular key expires. Use locking or probabilistic early expiration.
Bloom Filter
Space-efficient probabilistic structure to test set membership. May have false positives but never false negatives.