Skip to content
FrontDepth, Home

Back to the topic

Practice: HTTP caching

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Open questionJunior

    How does a browser decide whether it can reuse a cached response, and what happens when the response is stale?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  2. Predict the outputMiddle

    A simplified model of RFC 9111's freshness rules computes which header sets the lifetime and whether a response of a given age is still fresh. What does it log?

    JavaScript
    function directives(header = "") {
      const result = {};
      for (const part of header.split(",")) {
        const [name, value] = part.trim().toLowerCase().split("=");
        if (name) result[name] = value === undefined ? true : Number(value);
      }
      return result;
    }
    
    function lifetime(headers, shared) {
      const cc = directives(headers["cache-control"]);
      if (shared && cc["s-maxage"] !== undefined) return ["s-maxage", cc["s-maxage"]];
      if (cc["max-age"] !== undefined) return ["max-age", cc["max-age"]];
      const date = Date.parse(headers.date);
      if (headers.expires !== undefined) {
        const expires = Date.parse(headers.expires);
        if (Number.isNaN(expires)) return ["expires", 0];
        return ["expires", Math.max(0, (expires - date) / 1000)];
      }
      if (headers["last-modified"] !== undefined) {
        const sinceModified = (date - Date.parse(headers["last-modified"])) / 1000;
        return ["heuristic", Math.floor(sinceModified * 0.1)];
      }
      return ["none", 0];
    }
    
    function check(headers, age, shared = false) {
      const [rule, seconds] = lifetime(headers, shared);
      return `${rule} ${seconds} ${seconds > age ? "fresh" : "stale"}`;
    }
    
    const date = "Mon, 16 Nov 2026 10:00:00 GMT";
    
    const api = { "cache-control": "max-age=60, s-maxage=600", date };
    console.log(check(api, 120, true));
    console.log(check(api, 120));
    
    const both = { "cache-control": "max-age=300", expires: "Mon, 16 Nov 2026 12:00:00 GMT", date };
    console.log(check(both, 400));
    
    console.log(check({ expires: "0", date }, 0));
    
    const page = { date, "last-modified": "Fri, 06 Nov 2026 10:00:00 GMT" };
    console.log(check(page, 3600));
  3. Predict the outputMiddle

    Two toy caches sit in front of an origin that reads the currency from a cookie. One keys by URL, the other by URL plus currency. What does the code log?

    JavaScript
    const prices = { EUR: "30.00", USD: "32.50" };
    const currencyOf = (request) => request.cookie.match(/currency=(\w+)/)[1];
    
    function origin(request) {
      const currency = currencyOf(request);
      return `${currency} ${prices[currency]}`;
    }
    
    function createCache(keyOf) {
      const store = new Map();
      return (request) => {
        const key = keyOf(request);
        if (store.has(key)) return `HIT ${store.get(key)}`;
        const body = origin(request);
        store.set(key, body);
        return `MISS ${body}`;
      };
    }
    
    const byUrl = createCache((request) => request.url);
    const byUrlAndCurrency = createCache((request) => `${request.url} ${currencyOf(request)}`);
    
    const eur = { url: "/api/products/lamp-l3", cookie: "sid=a1; currency=EUR" };
    const usd = { url: "/api/products/lamp-l3", cookie: "sid=b2; currency=USD" };
    const usd2 = { url: "/api/products/lamp-l3", cookie: "sid=c3; currency=USD" };
    
    console.log(byUrl(eur));
    console.log(byUrl(usd));
    console.log(byUrlAndCurrency(eur));
    console.log(byUrlAndCurrency(usd));
    console.log(byUrlAndCurrency(usd2));
  4. Open questionMiddle

    Which caching headers would you set for a single-page app's HTML, its hashed JS/CSS bundles, unversioned images and its API responses — and why?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  5. Predict the outputSenior

    A model of a cache's reuse decision takes Cache-Control, the Age the CDN reported and the seconds the response has spent in this cache. What does it log?

    JavaScript
    function directives(header) {
      const result = {};
      for (const part of header.split(",")) {
        const [name, value] = part.trim().toLowerCase().split("=");
        result[name] = value === undefined ? true : Number(value);
      }
      return result;
    }
    
    function decide(cacheControl, ageHeader, secondsInCache) {
      const cc = directives(cacheControl);
      if (cc["no-store"]) return "not stored";
      const age = ageHeader + secondsInCache;
      const lifetime = cc["max-age"] ?? 0;
      if (cc["no-cache"]) return "validate first";
      if (lifetime > age) return `reuse, ${lifetime - age}s left`;
      const staleFor = age - lifetime;
      const swr = cc["stale-while-revalidate"] ?? 0;
      if (!cc["must-revalidate"] && staleFor < swr) return "reuse stale, validate in background";
      return "validate first";
    }
    
    console.log(decide("max-age=600", 540, 30));
    console.log(decide("max-age=600", 540, 90));
    console.log(decide("max-age=600, stale-while-revalidate=120", 540, 90));
    console.log(decide("max-age=600, must-revalidate, stale-while-revalidate=120", 540, 90));
    console.log(decide("max-age=600, no-cache", 0, 0));
    console.log(decide("no-store, max-age=600", 0, 0));
  6. Open questionSenior

    After a release that added caching, request volume dropped as planned, but some users now see data for the wrong setting — prices in another currency, text in another language. How do you investigate and fix it while keeping the cache?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  7. Open questionSenior

    Content or prices change. How do you make sure the CDN and users' browsers stop serving the old version, and what can and cannot be invalidated?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.