Caching in Web Applications: What It Is and When It Helps
What is caching?
Caching means keeping a copy of a result so you do not have to produce it again. When the same page, image or database answer is requested many times, serving a stored copy is faster for the user and cheaper for your servers than rebuilding it every time.
The trade-off is freshness. A copy can fall out of date. Every caching decision is a decision about how out of date a result is allowed to be.
Where do caches sit in a web application?
A request can pass several caches on its way from a user to your database.
| Layer | What it stores | Shared by |
|---|---|---|
| Browser cache | Pages, images, scripts the user already downloaded | One user |
| Content delivery network (CDN) | Static files and some pages, close to users | Many users |
| Application cache | Results of expensive work, such as a computed report | All requests to your app |
| Database cache | Recently used data, and in some systems query results | All queries |
The browser and the CDN follow instructions your server sends with each response, in a header called Cache-Control. It can say, for example, that a file may be reused for a set time, that it must be checked with the server before reuse, or that it must never be stored. A browser cache belongs to one person, so it can hold private content. A shared cache such as a CDN serves many people, so it must never hold one user's private page.
What is worth caching?
Good candidates share three traits: they are requested often, they are expensive to produce, and they change rarely or can be slightly stale without harm.
- Images, fonts, scripts and style sheets
- Public pages that look the same for everyone, such as a product catalogue
- Expensive calculations, such as dashboard totals that can be a few minutes old
- Responses from slow outside services, where the provider allows it
Poor candidates are anything personal, anything that must be exact right now, such as an account balance or stock level at checkout, and anything that is cheap to produce anyway.
What is the hard part?
Knowing when a cached copy is no longer true. There are two common approaches.
- Expire after a set time. The copy is thrown away after a fixed period. Simple, but users may see old data until it expires.
- Remove on change. When the underlying data changes, the code removes or updates the cached copy. Fresher, but every place that changes the data must remember to do it.
For files such as scripts and images, a common pattern avoids the problem entirely: put a version or fingerprint in the file name. A new release produces a new name, so the old copy can be cached for a long time without ever being served by mistake.
What can go wrong?
- Serving one user's data to another. This is the most serious caching mistake. Pages that depend on who is signed in must not be stored in a shared cache.
- Stale data that matters. A price or availability shown from an old copy can lead to a wrong order.
- Many requests at once when a copy expires. If a popular item expires and every request rebuilds it at the same moment, the database can be overwhelmed. Teams avoid this by letting one request rebuild while others wait or receive the old copy.
- Caching to hide a slow design. A cache can mask a slow query instead of fixing it. Fix the cause first where you can.
When should you add caching?
Add it when you have measured a real slowness or cost and know which requests cause it. Start with the cheapest layers: correct Cache-Control headers for static files and a CDN in front of public content. Add an application cache only for specific expensive results, and decide for each one how stale it may be and how it will be cleared.
For the first layer most sites add, see what a CDN is, explained for founders.
Where can you read more?
Caching is one of the patterns behind systems that grow without breaking, covered in how to build scalable and robust architectures. Your API style affects how easily responses can be cached; see REST vs GraphQL. And the database you choose shapes which queries are expensive in the first place, discussed in PostgreSQL vs MongoDB for new products.
*Is your application slower or costlier than it should be? We can find where the time goes and fix it. Start a conversation →*