Skip to content

Caching layers

A cache keeps a copy of data closer to where it is needed so that later requests skip slower work: a network hop, a disk read, a query or a page render. Caching is the cheapest way to cut latency and protect a database, and it shows up at every layer of a system. It is also where many outages and stale-data bugs come from, so a designer must know what each layer buys and costs.

Two properties of real workloads make caches effective:

  • Temporal locality. Data that was just read is likely to be read again soon (a viral post, a logged-in user’s profile).
  • Skew. Popularity follows a long-tailed, roughly Zipf-like distribution. With a Zipf exponent of 1 over one million items, the most popular 1% of items receive about 68% of requests and the top 10% about 84%. Caching a small hot set therefore absorbs most of the traffic.

Skew also explains why caches protect databases. A database spreads load over partitions by key, and a popular item concentrates traffic on one partition. A cache in front absorbs those hot reads and smooths spikes.

The costs are just as real:

  • Staleness. A cached copy can disagree with the source of truth. Every cache needs an answer to “how stale may this be, and who fixes it?”
  • Modal behaviour. With a 95% hit ratio, the database only sees 5% of reads. If the cache is flushed, restarted or partitioned, the database suddenly receives 20 times its usual load. The cache has become load-bearing.
  • Cold starts. New cache nodes and fresh deploys start empty and lean on the origin until warm.
  • Memory and complexity. RAM is far more expensive than disk, and every cache adds code paths for misses, failures and invalidation.
flowchart TB
  browser["Browser or app cache"] --> cdn["CDN edge cache"]
  cdn --> proxy["Reverse proxy cache<br/>(Varnish, NGINX)"]
  proxy --> app["App server<br/>in-process cache"]
  app --> dist["Distributed cache<br/>(Redis, Valkey, Memcached)"]
  dist --> db[("Database<br/>buffer pool")]
  db --> disk["OS page cache and disk"]

Each layer is closer to the user (and cheaper per hit) than the one below it, but harder to control and to invalidate.

LayerWhat it cachesControlled byTypical hit latencyWatch out for
Browser or mobile appStatic assets, API responsesCache-Control, ETag, service workers~0 ms (local)You cannot purge it; version asset URLs
CDNStatic files, media, cacheable API and HTMLHTTP headers, CDN rules, purge APISingle-digit to tens of msPersonalised content leaking into shared caches
Reverse proxyFull HTTP responses, fragmentsProxy config (for example Varnish’s VCL)Under a millisecond plus networkCache keys missing a header such as Accept-Language
In-process (L1)Hot objects, config, compiled templatesApplication code (for example Caffeine)MicrosecondsEach server holds its own copy; invalidation fan-out
Distributed cacheObjects, sessions, counters, rendered fragmentsApplication codeSub-millisecond in the same zoneNetwork hop, hot keys, cluster failures
Database buffer cacheData and index pagesDB settingsMicroseconds inside the DBToo small a buffer pool turns reads into disk I/O

Some notes per layer:

  • Client caching is free capacity: a response the browser reuses never reaches you. Long-lived, fingerprinted asset URLs (app.3f9c.js with Cache-Control: max-age=31536000, immutable) plus short-lived HTML is the standard recipe. See HTTP for the headers.
  • CDN caching is covered in Content delivery networks. CDNs increasingly cache API responses too, using short TTLs and stale-while-revalidate.
  • Web server / reverse proxy caching: Varnish has long been the classic HTTP accelerator. Its open-source project has been renamed Vinyl Cache (announced in 2025; Vinyl Cache 9.0 shipped in March 2026), while Varnish Software continues a separate project under the Varnish Cache name. NGINX and most API gateways offer similar response caching. See Reverse proxy and API gateway.
  • Database caching: every database caches pages in memory. PostgreSQL’s shared_buffers (a common starting point is 25% of RAM on a dedicated server) works alongside the OS page cache; MySQL’s InnoDB buffer pool is often given up to 80% of RAM. Tuning these for your working set is often the cheapest win. MySQL removed its built-in query result cache in 8.0 because it scaled poorly and was invalidated per table.
  • Application caching: an in-memory key-value store between the application and its database. In-process caches are fastest but duplicated per server; a distributed cache is shared, survives deploys, and can be sized independently. Many systems use both: a small L1 with a TTL of seconds in front of a shared L2.

There are two broad ways to decide what a cache entry is.

Query-level caching hashes the query text and parameters into a key and stores the result set. It is easy to bolt on, but invalidation is the problem: when one row changes, you cannot cheaply tell which cached queries included it. You either flush whole tables’ worth of entries or live with staleness until TTLs expire. This is exactly why MySQL’s query cache was retired.

Object-level caching stores what the application thinks in: a User with its settings, a product page model, a rendered comment thread. The key is derived from the object’s identity (user:v1:42), so when the underlying data changes the application knows which key to delete. Objects can also be assembled asynchronously: workers listen for changes and rebuild the cached object, so readers almost never miss.

Query-levelObject-level
KeyHash of SQL and parametersDomain identity (user:42, feed:42:page:1)
InvalidationHard: a row change maps to unknown queriesClear: an entity change maps to known keys
Reuse across code pathsLow (only identical queries hit)High (every reader of the object hits)
EffortLow to addRequires modelling the cached objects

Granularity is a spectrum: row, query result, assembled object, rendered fragment, full page. Higher levels save more work per hit but are invalidated by more kinds of change.

Good candidates are read often, expensive to produce and tolerant of brief staleness:

  • Rendered HTML fragments or full pages for anonymous users.
  • Assembled objects: user profiles, product details, permissions.
  • Aggregates and derived data: counters, leaderboards, timelines and activity feeds, social graph adjacency lists.
  • Results of slow external calls: currency rates, geolocation, third-party API responses.
  • Configuration, feature flags and public keys used to verify tokens.
  • Negative results (“no such user”) with a short TTL.

Poor candidates: data that must be exactly current (account balances during a transfer), data written far more often than read, and huge blobs that belong in object storage behind a CDN. Sessions are a special case: they are often stored in Redis or Valkey, but then the store is the session’s system of record, not a cache, and must be configured for persistence and replication accordingly.

A cache key must change whenever the cached value would change, and must not change otherwise.

  • Namespace and version: user:v2:42. Bumping v2 when the serialised format changes makes old entries unreachable instead of crashing new code.
  • Include every input that changes the output: locale, currency, user tier, A/B variant, API version. Missing one serves one user’s variant to another.
  • Normalise: sort query parameters, lowercase case-insensitive values, drop tracking parameters (utm_*) so equivalent requests share an entry.
  • Bound the length: hash long composite inputs. Memcached limits keys to 250 characters, and long keys waste memory anywhere.
  • Isolate tenants and users: a personalised value must include the user or tenant ID; never let a shared cache serve private data under a shared key.
  • Avoid unbounded attacker-controlled keys: caching every arbitrary search string invites memory exhaustion and pollutes the cache.

With hit ratio h, cache latency t_cache and origin latency t_origin:

effective latency = h · t_cache + (1 − h) · t_origin
origin load = (1 − h) · request rate

(With a look-aside cache a miss actually costs t_cache + t_origin, which adds t_cache to every miss; at sub-millisecond cache latency the difference is negligible.)

Example: Redis at 0.5 ms, a database query at 20 ms, 50,000 reads per second:

Hit ratioEffective latencySpeed-upReads reaching the database
0%20.0 ms1.0×50,000/s
50%10.3 ms2.0×25,000/s
80%4.4 ms4.5×10,000/s
90%2.5 ms8.2×5,000/s
95%1.5 ms13.6×2,500/s
99%0.7 ms28.8×500/s
99.9%0.5 ms38.5×50/s

Two lessons. First, the last few percent matter most: going from 90% to 99% reduces database load tenfold. Second, the origin must still survive the miss traffic, and a cold or failed cache drops h to zero. Capacity-plan the database for a degraded hit ratio, or protect it with request coalescing and rate limits (see Cache strategies).

The licensing story. Redis was BSD-licensed up to version 7.2. In March 2024 Redis Ltd. moved new versions to a dual source-available licence (RSALv2 or SSPLv1), which cloud providers could not offer as a managed service without an agreement. About a week later the Linux Foundation launched Valkey, a fork of Redis 7.2.4 under the BSD licence, backed by AWS, Google Cloud, Oracle, Ericsson and others. In May 2025, Redis 8 added the OSI-approved AGPLv3 as a third licence option and folded the former Redis Stack modules (JSON, time series, probabilistic structures, the query engine) and a new vector set type into the core. Valkey continued independently: Valkey 9.0 (October 2025) added atomic slot migration, per-field hash expiration and numbered databases in cluster mode, and Valkey 9.1 (May 2026) added per-database ACLs for isolating tenants and lower per-key memory overhead. Managed offerings now include ElastiCache and Memorystore for Valkey alongside Redis Cloud.

Redis 8.xValkey 9.xMemcached
LicenceRSALv2, SSPLv1 or AGPLv3 (your choice)BSD-3-ClauseBSD-3-Clause
GovernanceRedis Ltd.Linux FoundationCommunity project
Data modelStrings, hashes, lists, sets, sorted sets, streams, JSON, time series, Bloom filters, vector setsRedis 7.2 types plus later additions; JSON, Bloom and search via official modulesStrings (opaque byte values)
ThreadingSingle-threaded command execution with I/O threadsSingle-threaded execution with improved multi-threaded I/OFully multi-threaded
PersistenceRDB snapshots and AOFRDB snapshots and AOFNone (pure cache)
Replication and clusteringBuilt-in replicas, Sentinel, ClusterBuilt-in replicas, Sentinel, ClusterNone built in; clients shard keys
Value sizeUp to 512 MB per stringUp to 512 MB per string1 MB per item by default (configurable)
Best atRich data structures, broad feature set, vendor supportRedis-compatible open source, cloud-managed defaultsSimple, very fast, memory-efficient object cache

Practical guidance: for a new cache in the cloud, Valkey is a natural choice: AWS and Google Cloud offer it as a managed service, and it works with existing Redis clients. Choose Redis 8 when you want its integrated modules or vendor support and can accept its licences. Choose Memcached when you need a plain, multi-threaded object cache and nothing else. Whatever you pick, remember that eviction defaults to noeviction in both Redis and Valkey: configure maxmemory and an eviction policy for cache workloads.

Avoid file-based caching on stateless servers

Section titled “Avoid file-based caching on stateless servers”

Caching to the local disk (or local memory) of application servers looks free but works against horizontal scaling:

  • Each server warms its own copy, so with N servers the hit ratio is lower and the origin sees up to N times the misses.
  • Instances are no longer interchangeable: autoscaling, redeploys and crashes throw the cache away.
  • Invalidation must reach every server, which needs a broadcast channel.
  • Disk caches fill disks and outlive the code that wrote them.

Keep application servers stateless and put shared cached data in a dedicated tier (a distributed cache, a reverse proxy, a CDN). A small in-process L1 cache is fine when entries have a short TTL or are invalidated through pub/sub or server-assisted client-side caching (Redis has had CLIENT TRACKING since version 6).

Based on The System Design Primer by Donne Martin (CC BY 4.0). Restructured, rewritten and updated; site content is licensed under CC BY 4.0.