awesome-everything RU
↑ Back to the climb

Caching

Caching capstone: code and header reading

Crux Read real headers, handlers, and a single-flight lock across the caching stack, predict the behaviour, and pick the highest-leverage fix.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Caching bugs are read in headers, handlers, and logs, not in prose. Read each snippet, predict how the layers will behave under real traffic, then choose the fix a senior would make first.

Goal

Practise the loop you run on every caching incident: read the directive or the code on the hot path, predict where each layer caches or leaks, and reach for the change that fixes the composition — not a louder TTL.

Snippet 1 — the Cache-Control header

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: public, max-age=600

This is a public price-list API that the team wants cached at the CDN edge, refreshed often, and still served if origin goes down.

Quiz

What does this header actually do versus what the team wants, and what is the highest-leverage fix?

Snippet 2 — the conditional GET handler

app.get("/articles/:id", async (req, res) => {
  const article = await db.getArticle(req.params.id);
  const etag = `"${article.version}"`;
  res.setHeader("ETag", etag);
  res.setHeader("Cache-Control", "private, max-age=0, must-revalidate");
  // always sends the full body
  res.json(article);
});
Quiz

The handler sets an ETag but the conditional-request optimisation never fires. What is missing, and what is the win when fixed?

Snippet 3 — the stampede lock

async function getHot(key) {
  let val = await cache.get(key);
  if (val !== null) return val;

  // cold: every concurrent caller reaches here and recomputes
  val = await expensiveQuery(key);   // hits the DB
  await cache.set(key, val, { ttl: 60 });
  return val;
}
Quiz

A hot key expires and 5,000 requests arrive in the same second. What happens, and what is the single-flight fix?

Snippet 4 — SWR timing

Cache-Control: public, s-maxage=60, stale-while-revalidate=300

A response is cached at 12:00:00. Requests arrive at 12:00:30, 12:01:30, and 12:07:00. No write or purge occurs.

Quiz

What does the shared cache serve at each of the three times?

Recap

Read top to bottom: a Cache-Control header has to split edge from browser (s-maxage vs max-age) and carry SWR plus stale-if-error to refresh gracefully and survive an outage; an ETag is inert until the server reads If-None-Match and answers 304; a cold hot-key needs single-flight so 5,000 misses become one DB query; and SWR’s two windows decide whether a reader gets a fresh hit, an instant-stale-with-background-refresh, or a blocking revalidation. Diagnose from the header and the log, fix the composition, then re-check under the same load.

Continue the climb ↑Caching capstone: design a multi-layer strategy
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.