1. Concept library
  2. Backend with Node.js
  3. Module 10: Caching and performance

Concept

HTTP caching

Cache-Control, ETag and Last-Modified let browsers and CDNs reuse responses, so the server does less and pages load faster. Public, cacheable data gets a max-age; private data gets no-store.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what Cache-Control, ETag and Last-Modified each do
  2. Set caching headers for public data and for private data
  3. Spot the long max-age on a user-specific response

In practice

A few lines that show HTTP caching doing its job.

JavaScript
app.get("/catalog/:sku", async (req, reply) => {
  const product = await catalog.get(req.params.sku);
  const etag = '"' + product.version + '"';
  if (req.headers["if-none-match"] === etag) return reply.code(304).send();
  return reply
    .header("etag", etag)
    .header("cache-control", "public, max-age=60, stale-while-revalidate=600")
    .send(product);
});

The common mistake

Watch for

Setting a long max-age on a response that includes user-specific data. A shared cache or CDN then serves one user's page to another. Private data is no-store or at least private.

What to know first

Your roadmap puts these before HTTP caching, unless you already know them.

How it stays learned

Once a session shows you understand HTTP caching, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Next in the moduleIn-memory caches →