Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 3: DNS, HTTP and HTTPS

    HTTP Caching

    Cache-Control: max-age — 3600 — "you may reuse this response for 3,600 seconds without asking."

    Chapter 8 of 132 min read

    The Plain Picture#

    You photocopy a timetable and stick it on your fridge. For a week you trust the copy. After a week you phone the office: "Has the timetable changed since version 12?" — "No." You keep your copy. Only if it changed do they send a new one.

    The Real Names#

    • Cache-Control: max-age=3600 — "you may reuse this response for 3,600 seconds without asking."

    • Cache-Control: no-store — "never keep a copy" (for sensitive data). private = only the user's browser may cache it, not shared caches (Ch 12).

    • ETag — a version tag for a response, like "v12" (often a hash of the content).

    • Conditional request — when the copy is too old, the client asks If-None-Match: "v12". If unchanged, the server replies 304 Not Modified with no body — tiny and fast.

    Output
      first visit:   GET /logo.png            →  200 OK, ETag: "v12", max-age=86400, [30 KB image]
      within a day:  (no request at all — served from the browser's cache)
      after a day:   GET /logo.png
                     If-None-Match: "v12"     →  304 Not Modified   [0 bytes of body]

    The standard trick for static files: put a hash of the content in the file name (app.3f9a2c.js) and cache it for a year. When the file changes, its name changes, so browsers fetch the new one. Frontend build tools do this automatically (JavaScript Modules and Tooling (coming soon)).

    Check yourself

    3 questions
    1

    What does max-age=600 mean?

    Show answer

    The response may be reused for 600 seconds without contacting the server.

    2

    What is an ETag and what does a 304 response mean?

    Show answer

    A version identifier for a response; 304 means "your cached version is still current", with no body.

    3

    Why put a content hash in a JavaScript file's name?

    Show answer

    So it can be cached for a very long time — any change produces a new file name, forcing a fresh download.