Skip to content

Caching strategies

Where a cache sits and how it is kept: who fills it on a miss, when a write reaches the real store, and how long a copy may be served after the data changes.

On this page

What it is

A cache keeps a copy of data, or of a computed result, somewhere faster or nearer than where it really lives, the system of record. The four patterns below answer two questions in different ways: who puts data into the cache when it is missing, and when does a change reach the system of record. A third question follows from both: how stale may a copy be before it does harm.

Cache-aside, also called lazy loading: the application asks the cache first. On a miss it reads the data store, adds the item to the cache and returns it. On an update it writes the data store and then removes the cached item, so the next read loads the new value. Only data someone asked for is cached, and when a failed cache node is replaced by an empty one, the application keeps working with extra misses.

Read-through: the cache is given a loader, and on a miss the cache itself calls it, stores the value and returns it. On reads, the application talks only to the cache. Write-through: a write goes to the cache and the data store in the same operation while the caller waits, so a reader sees the new value right after a successful write. Write-behind (write-back): writes are queued and applied to the data store later, so the caller does not wait, at the price of a lag before the store has them.

Where it sitsWhat it holdsHow entries leave it

The client (browser)

Responses for one user, a private cache

Expiry set by response headers, its own eviction, or the user

A CDN or reverse proxy

Responses shared by many users

Expiry, or a purge through its tools or API

The service (Redis, Memcached, in-process)

Objects and query results

Expiry, eviction when memory runs out, or a delete from your code

How long a copy may be served. In HTTP, max-age says how many seconds a response stays fresh and s-maxage overrides it for shared caches such as a CDN. stale-while-revalidate lets a cache keep serving a stale response while it fetches a new one in the background, and stale-if-error lets it serve a stale one when the origin returns a 500, 502, 503 or 504. In an application cache the same job is done by a time to live on each key; it does not promise the value is current, but it bounds how long a copy is served before it is fetched again.

When to use it

Cache when reads far outnumber writes, the data changes rarely or tolerates some staleness, and a small part of it gets most of the traffic. Redis suggests the allkeys-lru eviction policy for exactly that shape, where a subset of keys is read much more than the rest.

Pick cache-aside by default: it caches only what is read and survives a lost cache node. Pick write-through when readers must see a write right away, and accept that every write takes two trips and fills the cache with data no one may read; a time to live on each entry limits that waste. Pick write-behind when writes come in bursts the store cannot take and losing the most recent writes in a crash is acceptable. Put a CDN in front when many users fetch the same public response from far away.

When not to

Do not cache when most requests would miss anyway: checking the cache and loading it can then cost more than it saves. Keep sensitive or security data out of a cache many users or applications share, and read it from the source. If the whole data set is static and fits, load it all at startup and never expire it instead of loading it piece by piece. And do not pin session state to one server’s local cache in a web farm.

Be careful with data that must be exact at every read, such as a balance or a seat count: a cache-aside copy can be stale between the database write and the next read, so the deciding read goes to the database. And skip write-behind for anything you cannot lose: unless the queue itself is durable, a write that has not landed can be lost with the cache.

Compared

PatternWho fills it on a missWhen a write reaches the storeMain risk

Cache-aside

Your code

Your code writes the store, then removes the cached copy

Brief stale reads; every miss is three trips

Read-through

The cache, through a loader

Depends on the write pattern paired with it

Less visible code path

Write-through

Not by itself: pair it with read-through or cache-aside

At once, while the caller waits

Slower writes; cache fills with unread data

Write-behind

Not by itself: pair it with read-through, for example

Later, from a queue

A lag before the store has the write

In a picture

missmisscheck firston a missBrowser cacheCDN edgeServiceRedisDatabase
Caches stack up: each layer answers what it can and passes the rest inward.

How to say it in an interview

The common mistake

Quiz

With cache-aside, in which order do you handle an update?
Users must see their own profile edit the moment they save it, and edits are rare. Which pattern fits best?
When does adding a cache make things worse?

Questions that use this

Related

Sources

  1. Ehcache documentation: Cache usage patterns
  2. Azure Architecture Center: Cache-Aside pattern
  3. Amazon ElastiCache documentation: Caching strategies
  4. MDN: HTTP caching
  5. MDN: Cache-Control
  6. Redis documentation: Key eviction
  7. Wikipedia: Cache (computing)
Sign in to report a mistake