Skip to main content

Command Palette

Search for a command to run...

Understanding Cache

Published
2 min readView as Markdown

Cache is simply faster storage located closer to the computation.

Where Cache Lives ?

Cache Read Patterns

1. Cache-Aside (Lazy Loading)

This is the most common pattern in production. The application controls the cache:

2. Read-Through Cache

The cache automatically loads data from the database when there's a miss. The application doesn't know about the database — it only talks to the cache.

Read-through works well when you use a dedicated caching layer like AWS ElastiCache with auto-population), but adds complexity.

Cache Write Strategies

1. Write-Through

Every write goes to both cache and database synchronously.

When to use: Financial systems, pricing engines, inventory counts — anywhere consistency matters more than speed.

2. Write-Around

Writes go only to the database. Cache is populated on next read.

When to use: When writes are frequent but reads are rare.
Example: logging systems that write constantly but users rarely query old logs.

3. Write-Back (Write-Behind)

Writes go to cache only. Database is updated asynchronously later.

Data loss risk: If cache crashes before syncing to the database, writes are lost forever. Use only for non-critical data (like page view counts, analytics events).

Cache Invalidation

Cache invalidation means removing data when it changes.

Common Invalidation Strategies

Cache Eviction Policies

Cache eviction means removing old data when cache is full.

1. Least Recently Used (LRU)

2. Least Frequently Used (LFU)

3. First In, First Out (FIFO)

4. TTL-Based Eviction

Cache Stampede (Thundering Herd)

Picture this: A popular product cached with 5-minute TTL. After 5 minutes, cache expires. Next second, 10,000 concurrent requests all get cache misses and simultaneously query the database. Your database CPU spikes to 100%, queries time out, site goes down. This is cache stampede.

Prevention Techniques

Real-World Caching Examples

E-Commerce Platform

Social Media Platform

Authentication & Authorisation

Content Delivery

Financial Systems

Caching

Part 1 of 1