Caching Strategies
Overview
A cache is a high-speed storage layer that holds a subset of data so future reads are served from fast memory (often sub-millisecond) instead of a slower origin. Caching reduces latency and offloads databases and services, but introduces the hard problems of staleness and invalidation. Read this before adding a cache layer or diagnosing stale/inconsistent reads.
Official sources
Core concepts
- What a cache is: a high-speed layer storing a subset of data; reading from an in-memory cache is extremely fast (sub-millisecond) compared with the origin store.
- Cache layers: caching happens at multiple tiers — CDN/edge for static content, application/in-memory caches (e.g. Redis) for data and sessions, and database/query caches.
- Cache-aside (lazy loading): the application checks the cache first; on a miss it loads from the origin, populates the cache, and returns — only requested data is ever cached.
- TTL (time to live): an expiry applied to cached entries so stale data is automatically evicted after a bounded window.
- CDN / edge caching: a CDN serves cached copies of content (pages, images, video) from a global network of edge locations near users.
- Eviction: when memory is full, caches evict entries by policy (e.g. LRU) to make room for new data.
Best practices
- Set a TTL on cached entries so staleness is bounded even when explicit invalidation is missed (AWS recommends TTLs to expire data appropriately).
- Use cache-aside as the default for read-heavy data: load on demand and cache only what is actually requested.
- Cache at the right layer: CDN for static assets, in-memory (Redis) for hot data and sessions, closest to where the read happens.
- Invalidate or update the cache on writes (write-through or explicit delete) to limit how long stale data can be served.
Common pitfalls
- Caching with no TTL and no invalidation → data silently goes stale forever; always bound freshness.
- Cache stampede: many concurrent misses hit the origin at once when a hot key expires → use locking/single-flight or staggered/jittered TTLs.
- Caching user-specific or sensitive data in a shared/edge cache → leaks across users; scope cache keys and mark private responses appropriately.
Examples
Cache-aside read path:
1. value = cache.get(key)
2. if hit: return value
3. value = db.query(...) # miss
4. cache.set(key, value, TTL) # populate with expiry
5. return value
# On write: db.update(...); cache.del(key) (or update in place)
Further reading
Related skills
- ../system-design-fundamentals — latency/throughput trade-offs caching addresses
- ../api-design-rest-graphql — HTTP caching of API responses
- ../scalability-reliability — caching to offload origins under load
1---2name: caching-strategies3description: Cache layers, cache-aside, TTLs, and invalidation to cut latency and load; read before adding a cache or debugging stale data.4domain: engineering5category: architecture6tags: [caching, cache-aside, ttl, invalidation, redis]7official_sources:8 - https://aws.amazon.com/caching/9 - https://redis.io/docs/latest/develop/10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/caching-strategies13---1415# Caching Strategies1617## Overview18A cache is a high-speed storage layer that holds a subset of data so future reads are served from fast memory (often sub-millisecond) instead of a slower origin. Caching reduces latency and offloads databases and services, but introduces the hard problems of staleness and invalidation. Read this before adding a cache layer or diagnosing stale/inconsistent reads.1920## Official sources21- AWS — Caching Overview (layers, TTLs, CDN, in-memory): https://aws.amazon.com/caching/22- Redis — Develop documentation (data types, clients, pub/sub): https://redis.io/docs/latest/develop/2324## Core concepts25- **What a cache is**: a high-speed layer storing a subset of data; reading from an in-memory cache is extremely fast (sub-millisecond) compared with the origin store.26- **Cache layers**: caching happens at multiple tiers — CDN/edge for static content, application/in-memory caches (e.g. Redis) for data and sessions, and database/query caches.27- **Cache-aside (lazy loading)**: the application checks the cache first; on a miss it loads from the origin, populates the cache, and returns — only requested data is ever cached.28- **TTL (time to live)**: an expiry applied to cached entries so stale data is automatically evicted after a bounded window.29- **CDN / edge caching**: a CDN serves cached copies of content (pages, images, video) from a global network of edge locations near users.30- **Eviction**: when memory is full, caches evict entries by policy (e.g. LRU) to make room for new data.3132## Best practices33- Set a TTL on cached entries so staleness is bounded even when explicit invalidation is missed (AWS recommends TTLs to expire data appropriately).34- Use cache-aside as the default for read-heavy data: load on demand and cache only what is actually requested.35- Cache at the right layer: CDN for static assets, in-memory (Redis) for hot data and sessions, closest to where the read happens.36- Invalidate or update the cache on writes (write-through or explicit delete) to limit how long stale data can be served.3738## Common pitfalls39- Caching with no TTL and no invalidation → data silently goes stale forever; always bound freshness.40- Cache stampede: many concurrent misses hit the origin at once when a hot key expires → use locking/single-flight or staggered/jittered TTLs.41- Caching user-specific or sensitive data in a shared/edge cache → leaks across users; scope cache keys and mark private responses appropriately.4243## Examples44```text45Cache-aside read path:46 1. value = cache.get(key)47 2. if hit: return value48 3. value = db.query(...) # miss49 4. cache.set(key, value, TTL) # populate with expiry50 5. return value51# On write: db.update(...); cache.del(key) (or update in place)52```5354## Further reading55- Redis develop docs (clients, data types, pub/sub): https://redis.io/docs/latest/develop/56- AWS caching overview (database, session, API caching): https://aws.amazon.com/caching/5758## Related skills59- ../system-design-fundamentals — latency/throughput trade-offs caching addresses60- ../api-design-rest-graphql — HTTP caching of API responses61- ../scalability-reliability — caching to offload origins under load