Trending

World technology news

→
Avoiding redundant cache validation for versioned files with RFC 8246
Software Engineering · 2017
Software Engineering2017

Avoiding redundant cache validation for versioned files with RFC 8246

Explore the immutable response directive introduced in 2017 through real HTTP responses for two JavaScript versions. Check deployment ordering, conditional responses and rollback separately.

Archive retrospective · Topic year 2017

Developer's JournalPublished: Dec 5, 20266 min read
Reading progress0%
In this article
  1. One address can keep representing the same file
  2. Connect the file name to the build output
  3. Three observable HTTP exchanges
  4. Freshness and an explicit reload request
  5. Keep earlier assets available for rollout and rollback
  6. Verify the promise within its intended scope

One address can keep representing the same file

When a reader visits a page for a second time, the HTML can be fetched again while its JavaScript stays the same. Even a validation response with no body uses a round trip. RFC 8246, published in September 2017, added the immutable response directive to Cache-Control for cases in which the server can promise that a representation will stay the same during its freshness lifetime. Versioned URLs make that promise practical. One HTML release can point to app.abc.js and the next to app.def.js. Each address keeps serving the bytes originally assigned to it. The HTML entry point can have a separate caching policy so clients discover the new paths on time.

Connect the file name to the build output

A build can compute a content hash for each output and include it in the filename. The sample suffixes abc and def illustrate the approach but are not calculated hashes. A real build can record each source and generated URL in a manifest that the HTML renderer uses for its asset links. Upload and check the referenced objects before sending the new HTML to visitors. If the page is released first, early requests may reach objects that have not been uploaded. Retain sample copies of both HTML versions so the deployment can check the whole set of referenced addresses.

Software delivery pipelineConceptual sketch · summary of the article's technical flow

Three observable HTTP exchanges

The Node.js sample serves two immutable files on loopback. It retrieves app.abc.js and checks the 200 status, its content and the caching directive. Then it deliberately sends a request with If-None-Match for the old ETag. The expected 304 response contains no body. The third request uses the new URL and expects 200, a different ETag and different JavaScript. The test exercises a contemporary runtime and the origin's HTTP behavior. It does not measure an automatic browser cache. Sending a conditional request manually does not establish when a browser would send the same request on its own.

import assert from 'node:assert/strict';
import { createServer } from 'node:http';
import { once } from 'node:events';

const assets = new Map([['/app.abc.js', 'export const revision = 1;'], ['/app.def.js', 'export const revision = 2;']]);
const server = createServer((request, response) => {
  const content = assets.get(request.url);
  if (!content) {
    response.writeHead(404).end();
    return;
  }
  const etag = '"' + request.url + '"';
  const headers = { 'Cache-Control': 'public, max-age=31536000, immutable', ETag: etag };
  if (request.headers['if-none-match'] === etag) {
    response.writeHead(304, headers).end();
    return;
  }
  response.writeHead(200, { ...headers, 'Content-Type': 'text/javascript' }).end(content);
});

server.listen(0, '127.0.0.1');
await once(server, 'listening');
try {
  const origin = 'http://127.0.0.1:' + server.address().port;
  const old = await fetch(origin + '/app.abc.js');
  assert.equal(old.status, 200);
  assert.equal(old.headers.get('cache-control'), 'public, max-age=31536000, immutable');
  assert.equal(await old.text(), assets.get('/app.abc.js'));

  const replay = await fetch(origin + '/app.abc.js', { headers: { 'If-None-Match': old.headers.get('etag') } });
  assert.equal(replay.status, 304);
  assert.equal(await replay.text(), '');

  const newAsset = await fetch(origin + '/app.def.js');
  assert.equal(newAsset.status, 200);
  assert.notEqual(newAsset.headers.get('etag'), old.headers.get('etag'));
  assert.equal(await newAsset.text(), assets.get('/app.def.js'));
  console.log(JSON.stringify({ node: process.version, initial: old.status, conditional: replay.status, newUrl: newAsset.status }));
} finally {
  server.close();
}

Freshness and an explicit reload request

RFC 8246 tells clients to avoid a conditional revalidation for a fresh immutable response unless the user explicitly indicates that validation is required, as with a force reload. Usual HTTP revalidation rules apply after the response becomes stale. The 304 in the example answers the simulated conditional request and still costs a network exchange. To measure the practical effect, observe two normal visits to the same page using a browser with the cache enabled. Record whether the same versioned URL required another request. Measure force reload separately so its intentional override is not mistaken for the ordinary cache behavior.

Keep earlier assets available for rollout and rollback

Older browser tabs, earlier HTML and partially updated instances may still ask for a file after its replacement is published. Both versioned resources can be served at once. Keep the previous assets available for as long as the live pages and rollback procedure need them. Returning to the old HTML should work without changing which bytes the old asset address delivers. If the old file has been removed, rollback can expose an HTML page that loads but refers to a missing script. Test the stored rollback HTML as well as the new build, especially when a deployment prunes older objects. A staged rollout can exercise both HTML versions during the same check. Request the new HTML through a newly updated instance and the old HTML through a second instance, then request each referenced bundle. This reproduces more of the situation a reader can see during a gradual deployment.

Verify the promise within its intended scope

Review the freshness requirements of each kind of response. A mutable HTML entry point or a personalized API reply may need a different policy from versioned public build artifacts. Secure transport and the CDN cache key still matter. In a release check, record the generated URL, retrieval status and response headers for both the old and new builds. Then verify the HTML points to the intended version. Include a request using an earlier HTML page to check that clients already holding it can still complete the load. These observations test the integrity of deployment and rollback along with the caching instruction, rather than assuming the header alone made the release safe.

726 reads ✎ Write a comment

Reader discussion

Discussion prompt

Which bottleneck would you expect as this solution scales?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.