Learn API Caching

API caching keeps copies of answers, so the server doesn't have to work on the next request again, or the request doesn't have to be sent. Cache-Control sets…

Open one book's page, close it, then open it again a few seconds later. The second time, the data appears faster. In DevTools (the browser's built-in inspection tools), the Network tab marks that request (memory cache) or (disk cache). This means the browser didn't ask the server for the data again, because it still had the earlier answer.

Keeping a copy of an answer to use again is called API caching. The goal: the server doesn't have to work on the next request again, or the request doesn't even have to be sent. Think of a bookshop's price leaflet. In its corner it says "valid for 1 minute". While it's valid, you read your own leaflet without going to the shop. After 1 minute has passed, you phone the shop: "My leaflet is edition v3, still valid?" If it is, the shop just says "still valid" without sending a new leaflet. If it changed, the shop sends the latest edition. In this picture, the leaflet is the copy of the answer, you are the browser, and the shop is the server.

Cache-Control: how long, and who may store it

Here is the Bookshop API's answer to GET /books/1:

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: public, max-age=60
ETag: "4b307d096b50f2ed"
Last-Modified: Fri, 09 Oct 2026 08:00:00 GMT

{"id":1,"title":"The Rainbow Troops","author_id":1,"price":89000,"stock":12}

Cache-Control is the main header that sets how long a copy of an answer is used, in the browser and in a CDN. A CDN is a network of servers in between that keeps copies of answers for many users at once. Cache-Control holds a few directives (short rules, separated by commas). In this answer, max-age=60 is the same as "valid for 1 minute" on the leaflet:

Directive Meaning
max-age=60 this copy is fresh for 60 seconds; until then it's used without asking the server
public the browser and shared caches (CDN) may store it
private only the browser of the user who asked may store it; a CDN may not
no-cache may be stored, but must be checked with the server every time it's used
no-store don't store it at all
s-maxage=300 a max-age just for shared caches (CDN)
stale-while-revalidate=300 after it expires, the old copy may be used 300 more seconds while the new one is fetched in the background

An old price after an update

The answer above carries max-age=60. This table shows what happens when the price of The Rainbow Troops changes during those 60 seconds:

Second What happens The price you see
0 GET /books/1, the server answers 200 and the browser stores a copy 89,000
10 the admin changes the price to 79,000 on the server
20 GET /books/1 again: the copy is only 20 seconds old, still fresh, no request 89,000
65 the copy has expired, the browser asks the server 79,000

At second 20 you still see 89,000, the old price. During max-age the browser doesn't ask the server at all, so it doesn't know the data changed. So set max-age to how long the data may be out of date. Data that must always be current uses no-cache and validators (markers that check whether a copy still matches the data on the server), which the next section covers.

ETag and If-None-Match: 304 Not Modified

An ETag is a version label for an answer, like the edition number on the leaflet. It's usually a hash of the body: a short code calculated from the content, which changes when the content changes. When the copy expires (its age passes max-age), the browser asks the server again. It sends a conditional request (a request with a condition) that carries its old ETag in the header If-None-Match:

GET /books/1 HTTP/1.1
Host: api.bookshop.test
If-None-Match: "4b307d096b50f2ed"

Here is the server's logic, with article data and the simple ETag "v" + version as in the exercise:

function handleRequest(article, ifNoneMatch) {
  const etag = "v" + article.version;
  if (ifNoneMatch === etag) {
    return { status: 304, body: null, etag };
  }
  return { status: 200, body: article, etag };
}
Call etag Result
handleRequest(article, null) with version: 3 v3 200, the article as body
handleRequest(article, "v3") v3 304, body null
handleRequest(article, "v2") v3 200, the client has an old version

In real HTTP an ETag value is written in quotes (ETag: "v3"). This lesson's Lab uses a SHA-256 (one way to calculate a hash) of the body, through crypto.subtle.digest("SHA-256", ...). Express also makes an ETag for res.json on its own, then answers 304 automatically when If-None-Match matches.

Last-Modified and If-Modified-Since

The second validator, after the ETag, uses time, not a version label. The server sends Last-Modified: Fri, 09 Oct 2026 08:00:00 GMT, which is when the data last changed. The browser then asks with If-Modified-Since: Fri, 09 Oct 2026 08:00:00 GMT. If the data hasn't changed since then, the answer is 304. It's only precise to 1 second, so two changes within the same second can't be told apart. When a request carries both If-None-Match and If-Modified-Since, the server uses If-None-Match.

🤔 Shared caches (CDN) and stale-while-revalidate

Cache-Control and ETags are HTTP caching: the copy is kept in the browser or a CDN. There are other options that people also call "API caching":

Way When to pick it
HTTP cache (Cache-Control, ETag) The same answer for everyone, for example the book list. The request does not even need to reach the server. This is what this lesson covers.
Server-side cache (memory or Redis, with a TTL) The query is heavy, but the request must still go through the server, for example because it checks the login. Keep the query result for a few seconds or minutes (TTL, the copy's maximum age), then delete the copy when the data changes. In Laravel: Cache::remember('books', 60, fn () => ...).
no-store Secret data or data that belongs to one user, for example a balance or a cart.

⚠️ Misread directives, badly made ETags

TL;DR: API caching keeps copies of answers, so the server doesn't have to work on the next request again, or the request doesn't have to be sent. Cache-Control sets how long a copy is used in the browser and the CDN (a server in between that keeps copies for many users). The rules in Cache-Control: max-age=N means fresh for N seconds, public may be stored by a CDN, private only by the browser, no-cache may be stored but must be checked with the server first, no-store isn't stored. During max-age the browser doesn't ask the server, so a change on the server only shows after the copy expires. Then the browser sends If-None-Match with its old ETag (the version label of the answer). If it still matches the latest ETag, the server answers 304 Not Modified with no body. If not, it answers 200 with the body and a new ETag. Last-Modified and If-Modified-Since do the same, but with time. stale-while-revalidate uses the old copy while fetching the new one in the background.

⚡ Cache-Control: how long, and who may store it

Meaning: The Cache-Control header controls the copies of answers that are kept (the cache). max-age=60 means the copy is fresh for 60 seconds, and during that time it's used without asking the server. public means a CDN (a server in between that keeps copies for many users) may store it. private means only the browser of the user who asked may store it. no-cache means it may be stored, but must be checked with the server first. no-store means it isn't stored at all.

⚡ An old price after an update

Guess first: a max-age=60 copy is stored at second 0. At second 10 the price changes on the server, from 89,000 to 79,000. At second 20, which price do you see?

The old one, 89,000. The copy is only 20 seconds old, still fresh, so the browser doesn't ask at all and doesn't know the data changed.

Meaning: Set max-age to how long the data may be out of date. Data that must always be current uses no-cache, so the browser checks with the server every time.

⚡ ETag and 304 Not Modified

Write:

function handleRequest(article, ifNoneMatch) {
  const etag = "v" + article.version;
  if (ifNoneMatch === etag) {
    return { status: 304, body: null, etag };
  }
  return { status: 200, body: article, etag };
}

Meaning: An ETag is a version label for an answer, here "v" plus the article's version number. When the copy expires, the browser sends the header If-None-Match holding its old ETag, and that value goes into ifNoneMatch. On a match, the server answers 304 Not Modified with no body, and the browser uses its own copy. On a mismatch, it answers 200 with the body and a new ETag.

What you will learn