Skip to main content

Caching

Caching stores frequently accessed data in fast storage to reduce latency and database load.

Performance Impact

ProblemWithout CacheWith Cache
Database reads10-100ms1-10ms
Repeated computationsCPU cost per requestComputed once
API rate limitsLimits reached quicklyReduced external calls
User experienceSlow page loadsFast responses

Cache is most effective when data is read more frequently than written.

Cache Placement

Loading diagram...
LayerCached ContentToolsLatency
ClientStatic assets, API responsesBrowser cache, localStorage0ms
CDNImages, CSS, JS, videosCloudFront, Cloudflare, Akamai10-50ms
ApplicationSession data, computed resultsRedis, Memcached1-5ms
DatabaseQuery results, indexesMySQL query cache, pg_bouncer1-10ms

Caching Strategies

Strategy 1: Cache-Aside (Lazy Loading)

The application manages the cache explicitly.

Loading diagram...

Advantages:

  • Only requested data is cached
  • Cache failures do not affect application availability
  • Compatible with any database

Disadvantages:

  • Cache miss requires 3 operations (cache read, DB read, cache write)
  • Data can become stale
  • Cold start requires cache warming

Use case: Read-heavy workloads, expensive computations

Strategy 2: Write-Through

Writes go to both cache and database synchronously.

Loading diagram...

Advantages:

  • Cache is always consistent with database
  • Reads are always fast (data is pre-cached)

Disadvantages:

  • Increased write latency (dual write)
  • Cache may store unread data
  • Complex failure handling required

Use case: Data that is read immediately after writing

Strategy 3: Write-Behind (Write-Back)

Writes go to cache immediately; database writes are asynchronous.

Loading diagram...

Advantages:

  • Fast write performance
  • Batching reduces database load
  • Absorbs traffic spikes

Disadvantages:

  • Data loss risk if cache fails before database write
  • Eventual consistency only
  • Complex failure recovery

Use case: Write-heavy workloads, analytics, logging

Strategy 4: Refresh-Ahead

Cache is proactively refreshed before expiration.

Loading diagram...

Advantages:

  • Eliminates cache miss latency for frequently accessed data
  • Predictable performance

Disadvantages:

  • Wasted refreshes for unused data
  • Implementation complexity

Use case: Predictable access patterns, critical high-traffic data

Strategy Comparison

StrategyWrite SpeedRead SpeedConsistencyComplexityUse Case
Cache-AsideNormalMiss is slowEventualLowGeneral purpose
Write-ThroughSlowerAlways fastStrongMediumRead-after-write
Write-BehindFastestAlways fastEventualHighWrite-heavy
Refresh-AheadNormalAlways fastEventualHighPredictable hot data

Cache Invalidation

Invalidation Strategies

StrategyMechanismAdvantagesDisadvantages
TTL (Time-to-Live)Data expires after fixed timeSimple, automaticMay serve stale data
Event-drivenInvalidate on write/updateFresh dataComplex, tight coupling
Version-basedInclude version in cache keyNo invalidation requiredStorage overhead
Purge on writeDelete cache entry on updateSimpleCache miss after every write

TTL Guidelines

Data TypeSuggested TTLRationale
Static assets1 yearVersioned URLs handle updates
User sessions15-30 minutesSecurity requirements
API responses1-5 minutesBalance freshness and load
Database query cache10-60 secondsDepends on write frequency
Real-time dataNo cache or secondsFreshness critical

Cache Eviction Policies

When cache is full, entries must be removed based on a policy.

Loading diagram...
PolicyMechanismBest ForDrawback
LRUEvict least recently accessedGeneral workloadsOne-time scans pollute cache
LFUEvict least frequently accessedStable hot setSlow to adapt to changes
FIFOEvict oldest entriesSimple use casesIgnores access patterns
TTLEvict expired entriesTime-sensitive dataMay keep stale unused data

LRU is the recommended default for most applications.

Distributed Caching

Single Node vs Distributed

Loading diagram...
AspectLocal CacheDistributed Cache
Latency~1us~1ms
ConsistencyPer-serverGlobal
MemoryLimited to one serverScalable
Failure impactGraceful degradationPotential single point of failure

Distributed Cache Architecture

Loading diagram...

Cache Technologies

TechnologyTypeStrengthsUse Cases
RedisIn-memory, persistentData structures, pub/sub, Lua scriptsSessions, leaderboards, queues
MemcachedIn-memory onlySimple, multi-threadedSimple key-value caching
VarnishHTTP cacheHTTP-specific optimizationsWeb page caching
CDNEdge cacheGlobal distributionStatic assets, media

Common Problems

1. Cache Stampede

Multiple requests hit the database simultaneously when cache expires.

Loading diagram...

Solutions:

  • Locking: First request acquires lock; others wait
  • Probabilistic refresh: Refresh before expiry with randomization
  • Background refresh: Separate process maintains cache

2. Hot Key Problem

Single key receives disproportionate traffic.

Solutions:

  • Replicate hot keys across multiple nodes
  • Add random suffix to distribute load: hot_key_1, hot_key_2
  • Use local cache for extremely hot keys

3. Cache Penetration

Requests for non-existent data always reach the database.

Solutions:

  • Cache null results with short TTL
  • Use Bloom filter to check existence before database query