Client Caching: Keeping Copies on the User's Side
How browsers and apps reuse stored answers, when they check with the server, and how to balance speed against stale data.
In short
Client caching is when a browser or app reuses stored answers, checking with the server only when needed, to balance speed against stale data.
12 min readFree
What you will learn
Client caching means keeping a copy of a server's answer on the user's own side. The copy can sit in a web browser, a phone app, or any program that asks a server for data. When the same request comes again, the client may answer from its copy. Sometimes it first asks the server a small question, and the server replies that nothing has changed. Either way, the user waits less and the network carries less data.
You will learn how the client decides whether a copy can be reused, where copies can be kept on a device, and why every reuse is a trade between speed and stale data. A worked example then walks through reloading a web mail page. Caches that run on servers or between users and servers are covered in other lessons, so this one stays on the client.
Key words
- Cache
- A store of copies, kept so the next request can be answered faster.
- Client
- The program that asks for data, such as a browser or a phone app.
- Origin server
- The server that holds the original version of the data.
- Response
- The answer a server sends back after a request.
- HTTP header
- A labeled note sent with a request or a response. Cache-Control is the main header for caching rules.
- Fresh
- A stored copy that the client may reuse without asking anyone.
- Stale
- A stored copy that is past its fresh time, so the client must check it before use.
- Revalidate
- Ask the server whether a stored copy still matches the current version.
- Staleness
- How far a stored copy has fallen behind the current data.
- Fingerprint
- A short code calculated from a file's contents and placed in its file name.
How Does the Client Decide to Reuse a Copy?
Every request a browser makes is first checked against its local store to see whether a valid copy exists. The server does most of the steering. It sends response headers that say how long a copy stays fresh, who may keep it, and how to check it later. These rules are written in the Cache-Control header. Its value is a list of directives, which are short instructions such as max-age=3600, separated by commas.
How Does Freshness Work with Max-age?
The max-age directive is a timer measured in seconds. A response with max-age=3600 stays fresh for one hour after the server created it. While the timer runs, the client uses its copy and does not contact the server at all. When the timer ends, the copy becomes stale, and the client must check before using it again. The age of a response counts from when it was made, so a copy that sat somewhere for a while has less time left.
The Cache-Control directives you will meet
max-age=N
The copy is fresh for N seconds. After that, the client checks with the server before reusing it.
no-cache
The client may keep a copy, but it must check with the server before every use. The copy is still stored, which surprises many people, so no-cache does not mean do not store.
no-store
Nothing may be kept. The client must request the full response every time.
private
Only the user's own browser may keep the copy. Shared caches on the way must not.
public
Any cache on the path may keep the copy, including a content delivery network (CDN), which is a group of servers placed near users.
How Do You Check with an ETag and a 304 Reply?
When a copy is stale, the client does not always need the whole body again. It can ask a cheap question with a validator, which is a header that describes the current version. An ETag is an identifier the server makes, often a hash. A hash is a short code calculated from the content, so it changes when the content changes. Last-Modified does a similar job using a date. The http.dev guide describes ETag as the more reliable validator. A strong ETag promises the bytes are identical, so even a tiny change must change the tag.
- 1
Store the first answer
The server sends the body with an ETag. The client keeps both the body and the tag.
- 2
Ask with the tag
When the copy goes stale, the client sends the saved tag in an If-None-Match header.
- 3
Server compares
If the tag still matches the current version, the server replies 304 Not Modified, a status code meaning the copy is still good, and sends no body.
- 4
Client reuses its copy
The client keeps using the body it already has and restarts its freshness timer.
- 5
Handle a change
If the content changed, the server replies 200 OK with the new body and a new tag. The client replaces its copy.
Tip
A 304 reply saves download size, which means the bandwidth used by the body. It still needs a trip to the server, though. A fresh copy saves the trip too, so the max-age timer matters most for speed.
Where Can the Client Store Copies?
A client has several places to keep copies. Each place differs in how much it can hold, how long data lasts, and who decides when to clear it. Some places are managed by the browser, and others are managed by the app's own code.
Browser HTTP cache
Built into the browser. It keeps responses such as images, scripts and styles, and follows the server's Cache-Control and ETag rules. App code usually does not control it directly.
Service Worker and Cache API
A Service Worker is a background script that can intercept a page's network requests. The Cache API stores full responses in Cache Storage, and the app decides what to keep and when to delete it.
localStorage
A small key-value store, meaning it saves values under names. Data stays until the app or user removes it. Its calls are synchronous, so they wait for each read or write to finish and can make the page freeze.
IndexedDB
A database built into the browser. It is asynchronous, so it does not make the page wait, and it suits larger structured data.
In-memory app state
Values held in the running program's memory. It is the fastest option, but it is lost when the page or app closes.
Choosing among these is not only about speed. Each option answers three questions: how much data fits, how long it lasts, and who can clear it.
Quick check
A web app must remember a user's chosen colour theme and also keep its app files available when the network is down. Which pair of storage choices fits best?
Comparing size, lifetime and control
The browser's HTTP cache is controlled mainly by the server's headers and by the browser's own limits. Cache Storage entries generally stay until the app deletes them, but the browser can still evict data, which means remove it to free space, when storage runs low. localStorage survives browser restarts, yet the browser or the user can still clear it. Because scripts can read localStorage, and it is not encrypted, it is a poor place for secrets.
Tip
Keep the files an app needs to start in Cache Storage through a Service Worker. Keep larger user data in IndexedDB. Keep only small, non-sensitive preferences in localStorage.
What Is the Trade-off Between Staleness and Speed?
Every reuse of a copy saves time and bandwidth, but it risks showing the user old data. A long max-age makes the cache very fast, yet a fix may take that long to reach users. So the design question is not whether to cache. It is how long each kind of response can safely be wrong. Invalidation is the word for forcing old copies out before their timer ends.
Invalidation with fingerprinted file names
The best tool for static files is to change the name when the contents change. A build step can place a fingerprint in the name, so a script becomes something like app.3f9a1c.js. When the code changes, the fingerprint changes, and the page's HTML points to the new name. The old name keeps its meaning forever, so the server can send Cache-Control: max-age=31536000, which is one year. Caching a script without a fingerprint is risky, because users may keep running old, broken code.
Short lifetimes for changing data
HTML pages and data often keep the same address while their content changes, so a fingerprint cannot help. For public HTML, a common choice is no-cache, so the browser checks every time and reuses its copy only when the server confirms it still matches. For public data that changes slowly, a short max-age, such as 60 seconds, may be acceptable. Pick the window by asking how wrong a user may be, and for how long.
Quick check
A news site sends Cache-Control: max-age=31536000 for its homepage HTML. An editor fixes a wrong headline, but readers keep seeing the old one for days. What went wrong, and how should the HTML and the fingerprinted JS and CSS files be cached differently?
Responses that must never be cached
Login and one-time codes
Use no-store, so no copy stays on the device. A code that was valid once should not be kept on disk.
Payment and bank details
Use no-store for responses that must not persist. Ask whether someone else using the same device could read a stored copy.
Personal data
Use private, so shared caches such as a CDN skip it. The user's own browser may still keep it, so add no-store if even that is not acceptable.
Secrets in web storage
Do not place tokens or personal details in localStorage. Scripts on the page can read them, and the data is not encrypted.
Worked example: reloading a web mail page
This example shows how the rules fit together in a web mail page. It is an illustration of the pattern, not a description of how any one company builds its product. Assume the page loads an HTML file, a script named app.3f9a1c.js, and a stylesheet whose name also carries a fingerprint.
- 1
Check the HTML
The HTML has a stable name and uses no-cache. The browser sends the ETag it saved. If the page has not changed, the server replies 304 and the browser keeps its stored HTML.
- 2
Reuse the fingerprinted files
The HTML points to app.3f9a1c.js and the stylesheet. Their names carry fingerprints and their headers allow one year of reuse, so the browser uses its stored copies with no network trip.
- 3
Ask about the inbox
The inbox list is data, not a fingerprinted file. The browser sends the ETag it saved for that list in an If-None-Match header.
- 4
Server compares
If no new mail arrived, the server replies 304 Not Modified, and the browser shows the saved list. No body is downloaded.
- 5
New mail arrives
The server replies 200 OK with the new list and a new ETag. The browser shows the fresh mail and saves the new tag.
- 6
Next release
When the team ships a new script, its fingerprint changes to something like app.7b2d80.js. The HTML points to the new name, and the old file is no longer used.
Tip
For each response, ask two questions. Does the name change when the content changes? Who may keep a copy? A changing name allows a long max-age. A fixed name calls for no-cache with an ETag, or a short max-age. Use no-store when a copy would be unsafe.
Try the freshness rules in code
The two short programs below copy the logic of the rules above. They use only Python's standard library, and their output never changes. Run them yourself to see the results.
Checking freshness with age
The first program treats a copy as fresh while its age is less than its max-age. At the boundary, a copy whose age equals max-age is already stale.
def is_fresh(age_seconds, max_age_seconds):
return age_seconds < max_age_seconds
for age in [0, 599, 600, 601]:
print(age, is_fresh(age, 600))Output
0 True 599 True 600 False 601 False
Checking a tag like a server
The second program acts like a server. If the saved tag matches the current tag, it replies 304 with no body. If the tag differs, it replies 200 with the new body.
def respond(saved_etag, current_etag, current_body):
if saved_etag == current_etag:
return (304, None)
return (200, current_body)
print(respond('abc123', 'abc123', 'inbox v1'))
print(respond('abc123', 'def456', 'inbox v2'))Output
(304, None) (200, 'inbox v2')
Key takeaways
Reuse is a trade
Every reused copy is faster, and every reused copy may be out of date. Choose lifetimes by asking how wrong a response may be.
Headers set the rules
Use max-age for timed reuse, no-cache with an ETag for checks, no-store for unsafe data, and private for personal data.
Names fix staleness
Fingerprinted files can be cached for a year because a change creates a new name. Pages with stable names need short lifetimes or checks.
Sources
- MDN Web Docs: HTTP caching guide
- MDN Web Docs: Cache-Control header
- web.dev: Prevent unnecessary network requests with the HTTP Cache
- http.dev: HTTP Caching explained
- http.dev: Cache-Control guide
- Cloudflare: What Is Cache-Control?
- ASISA Tools: HTTP caching headers explained
- Jono Alderson: A complete guide to HTTP caching
- howhttpworks: Cache-Control header directives and examples
- Wadhah Digital: Caching strategies for web performance
- web.dev: Storage for the web
- Cursa: Data storage choices (IndexedDB, Cache Storage, localStorage)
- DEV: Client-side storage types and usage
- DEV: Browser storage deep dive, cache vs IndexedDB for PWAs
Last updated on 17 November 2026
Related lessons
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.