CSE Roadmap

Common tier · about 3 hours · 7 parts

Part 3 of 7 · 3 min read

HTTP caching: Cache-Control, Expires and ETag

The headers that tell browsers and proxies how long to keep a response.

Think of a label on a jar of food: "use within 3 days" or "use before 12 March". A third kind of label gives a batch number, so that a shop can check whether its jar is still the current batch without opening it.

The book "Building Microservices" explains how HTTP builds caching in. It is one reason HTTP scales so well.

Cache-Control. A header in the response tells clients whether to cache the resource, and for how many seconds.

Cache-Control

Cache-Control is a response header that tells browsers and proxies whether and for how long they may reuse a response without asking the server.

Example: Cache-Control: max-age=3600 allows reuse for one hour. Cache-Control: no-store forbids storing.

Expires. Instead of a number of seconds, it gives a date and time after which the resource is stale. The book says that standard static content such as CSS or images often fits a simple time to live, while a known update time suits an Expires header. Together these can stop a client making a request at all. The book also warns that ETags, Expires and Cache-Control overlap, and that using all of them can give conflicting information.

ETag. An entity tag identifies the version of a resource. When the resource changes, the tag changes. It supports a conditional GET.

ETag and conditional GET

An ETag is a short label for one version of a resource. In a conditional GET, the client sends the ETag it holds in an If-None-Match header. If the resource is unchanged, the server answers 304 Not Modified with no body. Otherwise it returns 200 with the new body and a new ETag.

Example: the client holds ETag o5t6fkd2sa. It sends If-None-Match: o5t6fkd2sa. The server replies 304 and the client reuses its copy.

A worked example of the saving. A product image is 400 KB. A user opens a page showing it 5 times a day.

  • No caching: 5 × 400 KB = 2,000 KB downloaded a day.
  • max-age=86400 (one day): the first view downloads 400 KB. The other 4 use the browser's copy with no request at all. The total is 400 KB, a saving of 80 percent, and 4 round trips avoided.
  • ETag only, no max-age: every view sends a conditional request. The first returns 400 KB. The other four return 304 with a few hundred bytes (say 300 bytes). The total is 400 KB + 4 × 0.3 KB = 401.2 KB. The bytes are almost as low, but there are still 5 round trips, each adding latency.

So max-age saves round trips and bytes, while an ETag saves bytes and keeps freshness checks. They work well together: a max-age for the normal period, and an ETag for the check after it passes.

For files that change, a common trick is to put a version or hash in the file name: app.3f9a1c.js. The file can then carry a very long max-age, since a new version has a new name and old copies are never used by new pages. This also solves the problem in the cache poisoning story of module 4, where pages stuck in users' browsers could only be fixed by changing the URLs.

Content Suggested rule
Versioned static files Very long max-age (a year)
Images that rarely change max-age of hours to days, plus ETag
Public pages that change often max-age of seconds to minutes at the CDN
Personal pages (account, cart) Not shared: private, short or no caching
Sensitive data no-store

Common mistake

Letting a shared cache store a personal page. If the cache key leaves out who the user is, one user's account page can be served to another. Mark personal responses so shared caches do not store them.

Check yourself:

  • What does the server send back when the ETag matches?
  • Why does a file named with a hash in it allow a very long max-age?