Skip to main content

Caching

Caching stores frequently accessed data in faster storage layers to reduce latency and database load. This section covers caching layers, strategies, invalidation approaches, and common problems.

Benefits of Caching

BenefitDescription
SpeedMemory access is approximately 100x faster than disk. A Redis lookup takes microseconds; a database query takes milliseconds.
ProtectionCaching absorbs read load, reducing database query volume.
CostReduced compute requirements lower infrastructure costs.

Caching Layers

Loading diagram...

1. Client-Side Cache

Browsers cache content based on HTTP headers. Use Cache-Control to specify caching duration (max-age in seconds) and ETag for conditional requests to validate cached content.

2. CDN (Content Delivery Network)

CDNs cache content at edge locations geographically closer to users.

Use CaseExamples
Static assetsImages, CSS, JavaScript
MediaVideo streaming
Pre-rendered contentStatic HTML pages

3. Application Cache

In-memory caches like Redis and Memcached store frequently accessed data between the application and database.

Use CaseExamples
Session dataUser sessions, authentication tokens
Query resultsDatabase query responses
Computed valuesAggregations, calculations
Rate limitingRequest counters

4. Database Cache

Most databases maintain internal query result caches. These are typically managed automatically by the database engine.

Caching Strategies

Cache-Aside (Lazy Loading)

The application checks the cache first, and only queries the database on a cache miss.

Implementation approach:

  1. Check cache using a consistent key format (such as "user:123")
  2. If found, return cached value immediately
  3. If not found (cache miss), query the database
  4. Store the result in cache with an appropriate TTL before returning
AspectDescription
AdvantagesOnly caches requested data, handles cache failures gracefully
DisadvantagesCache miss penalty on first access, potential for stale data

Write-Through

Data is written to both the cache and database simultaneously. On every update, write to the database first, then update the cache with the same data. This ensures cache consistency with the database.

AspectDescription
AdvantagesCache remains consistent with database
DisadvantagesIncreased write latency, caches data that may never be read

Write-Behind (Write-Back)

Data is written to the cache immediately and persisted to the database asynchronously.

AspectDescription
AdvantagesFast write operations
DisadvantagesRisk of data loss if cache fails before persistence, increased complexity

Cache Invalidation

Cache invalidation determines when cached data should be removed or updated.

Strategies

StrategyDescriptionUse Case
TTL (Time To Live)Data expires after a specified durationMost common approach
Event-basedCache is invalidated when underlying data changesReal-time consistency requirements
Version-basedVersion identifier included in cache keyAtomic updates

TTL Configuration

Set TTL (Time To Live) when storing cache entries. For example, a 3600 second TTL expires the entry after 1 hour.

TTL selection factors:

FactorConsideration
Staleness toleranceHow outdated can data be before it impacts users?
Read/write ratioHigher read ratios benefit more from longer TTLs
Cache miss costExpensive operations warrant longer TTLs

Cache Problems

Cache Stampede

Multiple requests simultaneously miss the cache and query the database, causing load spikes.

SolutionDescription
Staggered TTLsAdd random variation to expiration times
Lock/semaphoreOnly one request populates the cache; others wait
Background refreshRefresh cache before expiration

Hot Keys

A single cache key receives disproportionately high traffic.

SolutionDescription
Key replicationDistribute across multiple cache keys
Local cachingCache in application memory
Rate limitingLimit requests to hot keys

Cache Penetration

Requests for non-existent data bypass the cache and query the database repeatedly.

SolutionDescription
Cache negative resultsStore "not found" responses
Bloom filterPre-filter requests for non-existent keys
Rate limitingLimit requests for unknown keys

Redis vs Memcached

FeatureRedisMemcached
Data structuresRich (lists, sets, sorted sets)Key-value only
PersistenceYesNo
ReplicationYesNo
ClusteringYesClient-side
Memory efficiencyLowerHigher

Selection criteria:

Use CaseRecommendation
Complex data structures (sorted sets, pub/sub)Redis
Data persistence requiredRedis
Simple key-value cache with maximum memory efficiencyMemcached

Design Considerations

ConsiderationDescription
Read/write ratioCaching provides greatest benefit for read-heavy workloads
Staleness toleranceDetermines cache strategy and TTL settings
Failure handlingSystem should degrade gracefully when cache is unavailable
Cache warmingPlan for populating cache on cold starts
Capacity planningCache must accommodate working set of frequently accessed data