What is caching? Explained with a restaurant kitchen
Caching means keeping a ready copy of things people ask for often, so your app doesn't make them from scratch every time. Here's how it works, explained with a busy restaurant kitchen.
- Author
Umair Mateen- Published
- Updated
- Reading time
- 5 min
- Category
- Engineering
In one sentence: caching means keeping a ready copy of something people ask for often, so your website or app can hand it over straight away instead of making it from scratch every time.
The everyday picture
Imagine a restaurant where every dish is cooked from raw ingredients the moment it is ordered. The food is fresh, but at lunchtime the queue goes out the door and the cooks are overwhelmed.
Good kitchens don't work like that. Before service, the cooks chop the onions, make the sauces and portion the rice. When an order comes in, most of the work is already done, and the dish reaches the table in minutes.
Your website or app is the kitchen. Every page a visitor opens is an order. The database is the walk-in fridge at the back: everything is in there, but walking back for it every time is slow. A cache is the prep station: ready-to-use copies within arm's reach.
A visitor's request checks each station in turn, starting with the closest. The first one holding a fresh copy serves it, and only if none do does it go all the way to the database.
How it works, step by step
- The browser cache (the bread basket already on the diner's table). Things that rarely change, like your logo, fonts and styles, are kept on the visitor's own device. On their next visit, those files don't need to be fetched again.
- The CDN (a pickup counter in every neighbourhood). A content delivery network is a group of servers spread around the world that keeps copies of your pages close to visitors. Cloudflare describes it as distributed servers that cache content near end users, which speeds up loading and reduces requests to your own server.
- The server cache (the prep station by the stove). Popular data, like a blog post or a product list, is kept ready in fast memory next to your app. Tools such as Redis are often used for this.
- The database (the walk-in fridge). If none of the stations has a copy, the app fetches the data from the database, serves it, and keeps a copy for the next person who asks.
Keeping it fresh
Prepped food doesn't last forever, and neither does a cached copy. Two habits keep it fresh:
- Use-by times. Every copy gets an expiry. A logo can be kept for a long time; a page that changes often might be kept for only a few minutes. MDN's caching guide explains that once that time passes, the copy is treated as stale and checked with the server before it is used again.
- Throwing out what changed. When you edit something, the old copy is deleted straight away. Redis's own guide describes this pattern: update the database, then remove the cached copy so the next request fetches the new version.
One rule never bends: personal information, like someone's account or basket, is never put on the shared prep station. It is cooked for that customer alone, or kept only in their own browser.
Where the picture stops
- Copies cost almost nothing. A kitchen that preps too much wastes food. A cache can hand out the same copy thousands of times, and it never runs out.
- Stale is the risk, not spoiled. Old data won't make anyone ill, but it can show an out-of-date price or a deleted post. That is why expiry and clean-up matter.
- There can be many fridges. Larger systems often have several databases and services behind the cache, not one store room.
Why it matters for your business
- Faster pages. Visitors get what they asked for sooner, which makes them more likely to stay.
- Lower running costs. Every request answered from a cache is one your database and servers don't have to handle.
- Calm under a spike. When a post or campaign suddenly brings a crowd, the cache absorbs most of the load instead of your database.
An example
Imagine an online shop that sends a sale email to 20,000 customers at 9am. Without caching, every visitor's page asks the database for the same product list, and the site slows to a crawl. With caching, the first visitor's request fetches the list and keeps a copy; the next thousands are served from the prep station in a moment. When the shop changes a price, that copy is thrown out and rebuilt.
Frequently asked questions
Is caching the same as saving a file?
Not quite. A saved file is the original. A cache holds a temporary copy kept close by for speed, and it can be deleted and rebuilt at any time without losing anything.
Why do I sometimes see an old version of a website?
Your browser or a CDN may still hold a copy that hasn't expired yet. A hard refresh, or waiting a few minutes, usually brings the new version.
Is caching safe for customer data?
Yes, when it is set up properly. Personal pages are marked private so they are never stored in shared caches like a CDN.
Do small websites need caching?
Many hosting platforms already do some caching for you. It matters more as traffic grows or pages become slow to build.
The DevDrift sets up hosting, caching and monitoring as part of Cloud and support.