Where to cache: client, CDN, application, database
A response passes several places that can remember it. Which one to use for what, and who is allowed to share a copy.
5 minute lesson
Four places on the way
A request travels from a browser to a database and back. At every stop on that road a copy of the answer can be kept. The closer to the user a copy sits, the more of the road it saves.
- ClientThe browser or the appsaves the whole request
- CDNAn edge near the usersaves the long trip to the origin
- ApplicationMemory or a cache serversaves the queries and the computing
- DatabaseIts own memorysaves the reads from disk
A site's logo is needed on every page. It could be cached in the browser, at a CDN, in the application or in the database's memory.
A hit in which of these saves the most work?
- The browser
- The CDN
- The application
- The database
Show the answer
- The browser
A hit in the browser means that no request is sent at all: no connection, no network, no server. A hit at the CDN still costs a short trip. A hit in the application still costs the whole journey to the origin. Each layer saves everything that lies behind it, so the first layer on the road saves the most.
The nearer to the user a cache hit happens, the more of the journey it saves.
One user or many
Private
one user
- Where
- Browser or app
- May hold
- Personal data
- Can be cleared
- No
Shared
many users
- Where
- CDN, application cache
- May hold
- Public answers
- Can be cleared
- Yes
The page /my-orders shows the orders of the user who is logged in.
Where may that response be cached safely?
- In that user's own browser
- In the application's cache, under a key with the user's id
- At the CDN, under the URL of the page
- In the application's cache, under the URL of the page
Show the answer
- In that user's own browser
- In the application's cache, under a key with the user's id
The response is different for every user, and the URL is the same for all. Any cache that finds entries by URL alone would give one user's orders to the next. The browser's cache belongs to one person, and a key that includes the user's id keeps every user's copy apart. What decides safety is whether the key tells users apart.
Personal data may be cached privately, or shared only under a key that includes the user.
What goes where
- Scripts and imagesClient and CDNthe same for all, and rarely changed
- A public product pageCDN or applicationthe same for all, changed now and then
- A user's own cartApplicationkeyed by the user and under your control
- The result of a costly queryApplicationcomputed once and reused
Name the places where a response can be cached on its way to a user, and say what each one saves.
Show the answer
The client, which saves the whole request. A CDN edge, which saves the long trip to the origin. The application, in memory or in a cache server, which saves queries and computing. And the database's own memory, which saves reads from disk. The closer the copy is to the user, the more it saves, and the harder it is to correct.
Recap
- A response can be cached at the client, the CDN, the application and the database.
- Closer to the user saves more, and is harder to clear.
- Shared caches are for what is the same for everyone. Personal data stays private or keyed by user.