Skip to content

What is a cache? Definition, how it works and examples

A cache is temporary storage that keeps the result of an expensive operation so it can be reused instead of recomputed. On the web it avoids asking the server again for an image, a page or a piece of data already fetched, cutting both display time and machine load.

In one sentence

A cache keeps a copy of what has already been computed, so it need not be done again.

Key points

  • A cache trades memory for time: you store in order to avoid recomputing.
  • It exists at several levels: browser, network, server, database.
  • The hard part is not storing, but knowing when the copy is no longer valid.
  • A badly invalidated cache serves stale content, which is worse than no cache at all.

Term at a glance

Cache
Caching
French term
Cache, mise en cache
Domain
Web performance
Category
Architecture and performance
Level
Beginner to intermediate

What exactly does "cache" mean?

The principle is old and reaches well beyond the web: when an operation is slow or expensive, you keep its result to hand. The second request no longer pays the price the first one did. What varies between caches is where the copy is kept and how long it is treated as trustworthy.

On a website several caches sit on top of each other. The browser keeps images and files already downloaded. A delivery network keeps a copy of pages on servers close to the visitor. The application server keeps the result of repeated computations. The database keeps frequent queries in memory.

The real difficulty is not caching: it is invalidation. How long does a copy stay correct? What happens when the underlying data changes? A price updated in your back office but served from a cache for six hours is a commercial problem, not a technical footnote.

How does a cache work?

  1. 01

    A request arrives

    The system first checks whether it already holds a valid copy of what is being asked for.

  2. 02

    Copy found and still valid

    It is returned immediately, with no recomputation and no database call. This is the fast path.

  3. 03

    Copy missing or stale

    The operation runs normally, and its result is stored for subsequent requests.

  4. 04

    The source data changes

    The copy must be invalidated, either when a set delay expires or explicitly at the moment of the change.

A concrete example

Your home page shows your three latest articles. Without a cache, every visitor triggers a database query to find them — a thousand visitors, a thousand identical queries. With a five-minute cache, the first visit pays the cost and the rest receive the copy. The database goes from a thousand queries to twelve an hour, and the page loads faster for everyone.

What is a cache used for?

Speeding up display

Serving a ready copy is almost always faster than rebuilding the response, especially on a mobile connection.

Cutting the server bill

Fewer computations and fewer database queries mean smaller machines for the same traffic.

Absorbing spikes

During a campaign or a traffic peak, the cache takes load the server could not handle.

Staying available

Some caches keep serving a copy even while the origin service is briefly unavailable.

Advantages and limits

  • Immediate speed gain, often the best return of any optimisation
  • Reduced server load and hosting costs
  • Better resistance to traffic spikes
  • Risk of serving stale information if invalidation is poorly designed
  • Hard to debug: behaviour depends on what happens to be cached
  • Unsuited to personalised or strictly real-time data

Why caching matters to a business

It is the performance lever with the best effort-to-result ratio. Correct caching often buys more seconds than a full redesign, for a fraction of the cost. The flip side is that a badly tuned cache shows stale prices, stock levels or opening hours — and that mistake is visible to customers.

Frequently asked

Why do my changes not appear straight away?

Because a cache is still serving the old version. Depending on the level involved, you either wait for the delay to expire or trigger an explicit invalidation after the change.

Should everything be cached?

No. Personalised pages, baskets and sensitive data should not be, or only privately in the visitor's own browser. A shared cache holding personal data is a leak.

How long should a copy be kept?

It depends how often the data changes and what a stale version costs. A few minutes for an article list; a year for an image whose filename changes with every version.

Related terms

Sources and references

Site still slow despite decent hosting? Caching is usually the first lever to pull, and the cheapest.

Have your performance measured
Glossary