Intro

To avoid unnecessary compute and latency, Caching stores data in a location where it can quickly (fast retrieval storage) be retrieved and sent to the user without needing a server to fulfill the request. Caching can be based on TemporalLocality or SpatialLocality. // Just like with RAM in computers...

Challenges

Keeping data in cache consistent and synchronized with the data in the database. // single source of truth…

Cache Invalidation Strategies

Methods used to maintain a single source of truth and data consistency.

Write Through

Writes to the DB and cache whenever changes are made, which solves the consistency issue but increases write-latency.

Write Around

Data goes straight into the DB, skipping the cache. This eliminates CacheFlooding and decreases write-latency but can increase read-latency for new data.

Write Back

New data is written to the cache first and then to the DB. This decreases latency for reading and writing BUT can be risky if system failure occurs potentially leading to data loss.

Cache Eviction Policies

Just like with RAM getting full, caches also need policies and algorithms to clear them when they get full.

Least Recently Used

LRU is a famous eviction policy used in computers. It evicts items that are the least recently accessed by the system. The longer an item goes unused (unread) in cache the more likely it is to be evicted.

First In First Out

FIFO removes the oldest items first, just like a Queue. // Also used in computers and operating systems.

Least Frequently Used

LFU evicts based on how frequently an item is accessed, so if an item isn’t popular and isn’t being accessed frequently, evict it!