Strategies for storing frequently accessed data closer to the consumer.
Cache-aside is the most widely used caching pattern. The application checks the cache first; on a miss, it queries the database, writes the result to the cache, and returns the data. Only requested data is cached, and cache failure degrades performance but does not cause errors.
Write-through caching ensures every write goes to both the cache and the database synchronously. The cache is always consistent with the database, eliminating stale reads at the cost of higher write latency.
Write-back caching writes to the cache only and asynchronously flushes to the database in batches. It provides the lowest write latency of any caching pattern but risks data loss if the cache crashes before flushing. Ideal for write-heavy workloads tolerant of small data loss windows.
Eviction policies determine which cache entries to remove when the cache is full. LRU, LFU, ARC, and TinyLFU each optimize for different access patterns, balancing hit rate, memory overhead, and implementation complexity.
Time-to-live (TTL) determines when cached entries expire. Hard TTL removes entries immediately at expiry, soft TTL serves stale data while refreshing in the background, and jittered TTL adds randomness to prevent synchronized expiration stampedes.
A cache stampede occurs when a popular cache entry expires and hundreds of concurrent requests simultaneously miss the cache and hit the database. Solutions include mutex/locking, probabilistic early expiration, stale-while-revalidate, and single-flight patterns.
Tiered caching uses multiple cache layers with different latency, capacity, and cost characteristics. L1 (in-process) provides microsecond access, L2 (distributed) provides millisecond access at scale, and L3 (CDN) provides global reach. Requests flow through tiers until data is found or the origin is reached.
Choosing the right caching layer -- browser, CDN, application server, or database -- determines caching effectiveness. Each layer offers different latency, capacity, invalidation characteristics, and scope. The best architectures cache data close to where it is consumed with an appropriate invalidation strategy.