Sub topic 2
The layers where caching happens
This chapter assumes you know how a client sends a request to a server, how a database answers queries, and what latency means. It maps the places where a cache can sit between a user and the data. Later chapters cover the patterns used inside each place.
One request, many possible caches
A single page load can pass through four caches before it reaches the database's disk. Each layer sits closer to the data than the one before. A hit at an outer layer skips every layer behind it.
The four layers
| Layer | Where it runs | Typical contents | Who controls it |
|---|---|---|---|
| Browser | The user's device | Static files, API responses marked cacheable | Response headers |
| CDN | Servers near users | Images, scripts, public pages | Headers and CDN rules |
| Application | Your servers or a cache service such as Redis | Query results, computed values, sessions | Your code |
| Database | Inside the database engine | Recently read data pages, query plans | Database configuration |
Browser cache
The browser keeps copies of responses on the user's device. A hit costs no network round trip at all, so it is the fastest layer. It is also the hardest to control, because you cannot clear a cache on a device you do not own. You influence it only through HTTP headers, covered in the HTTP caching chapter.
CDN
A content delivery network (CDN) is a set of servers spread across regions. It stores copies of your responses and serves them from a location near the user. This cuts latency and removes load from your origin servers. It is shared by all users, so it should only hold content that is safe to show to everyone.
Application cache
This is the layer you write code for. Your service stores results in process memory or in a shared store such as Redis. The cache-aside, read-through, and write-through patterns in the next chapters all work at this layer. Because you own the code, you can invalidate entries precisely when data changes.
Database cache
Databases keep hot pages in memory (a buffer pool) and may cache query plans. You rarely manage this directly, but it explains why the second run of a query is often faster than the first. Tuning it means sizing memory, not writing cache logic.
Choosing the layer
Move a cache outward for speed and scale, and inward for control and freshness.
- Content identical for all users, such as a logo, belongs in the CDN and browser.
- Content tied to one user, such as an account page, must not go in a shared cache without a key that separates users.
- Expensive results that change on writes belong in the application cache, where your code can invalidate them.
Layers also stack their staleness. A value cached for 60 seconds in the application and 5 minutes in a CDN can be nearly 6 minutes old when a user sees it.
Exercise
A product page shows the same description to every visitor, and the price changes a few times a day. Which layers could cache the description, and what risk appears when you also cache the price in several layers?
Show answer
The description can be cached in the browser, the CDN, and the application cache, because it is the same for everyone. Caching the price in several layers makes staleness add up. A price change may take as long as the longest lifetime in the chain to reach users, so each layer needs a short lifetime or a way to invalidate it.