{"canonical_url":"https://engineer.company/glossary/caching/","content_markdown":"# Caching\n\n## What it is\n\nCaching is the practice of saving the result of some piece of work, such as an HTTP response, a\nquery result or a rendered page, and handing that saved result to later requests under rules that\nsay how long it can be trusted and what retires it. The verb is to cache, and a result kept this\nway is cached; caching the return values of one function inside a program is usually called\nmemoisation.\n\nThe store that holds the copies is the cache. Caching is the policy around it, and the policy has\nthree questions. How long is a copy fresh, meaning fit to serve without asking anyone? Once it is\nstale, how is it checked? And when the original changes, what removes or replaces the copy?\n\nHTTP answers all three in headers. RFC 9111 calls a stored response fresh while its age is below\nits freshness lifetime, which the origin sets with the `max-age` directive of `Cache-Control` or\nwith an `Expires` date. Past that point the response is stale, and a cache normally has to\nvalidate it before reuse. It sends a conditional request carrying a validator it kept, usually the\nresponse's `ETag` in an `If-None-Match` header, and if nothing has changed the origin answers 304\nNot Modified with no body. Revalidation still costs a round trip, but it saves the transfer and\nthe work of building the body again.\n\n```http\nHTTP/1.1 200 OK\nCache-Control: max-age=3600, stale-while-revalidate=60\nETag: \"v42\"\n```\n\nInside an application the same questions take other shapes. In cache-aside, the code reads the\ncache first, and on a miss reads the database and fills the cache itself. In write-through, every\nwrite updates the cache together with the database, so a read finds the current value. An expiry\non each entry, its time to live or TTL, plays the part of `max-age`. Even a database can cache: a\nPostgreSQL materialized view stores a query's result as a table, and that result changes only when\nsomeone runs `REFRESH MATERIALIZED VIEW`.\n\n## Why it matters\n\nEvery caching rule trades freshness for speed, and the right trade differs by kind of data. A logo\ncan be a year old. A product price can be a minute old. A balance or a seat count should be\nconfirmed with the source before anyone acts on it. Caching most often goes wrong when one rule is\napplied to all three.\n\nStatic files get the cleanest answer. When a build writes a hash of each file's content into its\nname, a changed file gets a new URL, so the old URL can be cached for a year. RFC 8246 adds the\n`immutable` directive, which tells the browser the response will stay the same while it is fresh,\nso even a reload can skip revalidating it. The HTML page that names those files has to stay\nshort-lived or be revalidated on every use, or visitors keep loading the old names.\n\nTwo directives are easy to confuse. `no-cache` lets a cache store the response but forbids reusing\nit without first validating it with the origin; `no-store` forbids storing it at all. For a page\nthat changes often, `no-cache` with an `ETag` gives current content and still saves the transfer\nwhen nothing has changed.\n\nInvalidation is the hard half. RFC 9111 requires a cache to invalidate a URL when an unsafe\nrequest to it, such as `POST`, `PUT` or `DELETE`, succeeds, and the same section points out that\nthis reaches only the caches that request passes through. A browser, a CDN and an application\ncache are three separate places, and removing a copy from one leaves the other two in place. So\nmost systems rely on expiry as the backstop and treat explicit invalidation as a way to be current\nsooner.\n\nStale data can also be useful. RFC 5861 defines `stale-while-revalidate`, which lets a cache serve\na stale response for a stated number of seconds while it fetches a fresh one in the background,\nand `stale-if-error`, which lets it keep serving a stale copy when the origin returns an error.\nBoth turn a hard expiry into a soft one: the visitor gets an answer at once, and the update\nfollows.\n\n## What it looks like in practice\n\nMany of one platform's users are seafarers and field staff at sea, on phones and often offline,\nand for them\n[caching made the web app installable and usable at sea](/portfolio/delivered-full-progressive-web-app-support/).\nThe service worker is configured through next-pwa, and Workbox runtime caching serves fonts,\nimages, audio, video and CDN assets cache first: from the device when a copy is there, from the\nnetwork only on a miss. Those are the files that stay the same from one request to the next, and\nHTTP cache-control headers go with them. Because service workers need HTTPS, install and offline\nbehaviour were tested over HTTPS on real devices. The outcome is an app that installs, keeps\nworking offline and loads repeat visits from cache rather than the network. A cache-first rule\nskips the freshness check entirely while an entry exists, which is why it suits files whose\ncontent is tied to their address.\n\nAt the other end of a system sits\n[a PostgreSQL database made ten times faster](/portfolio/accelerated-postgresql-performance-by-10x/)\nthat holds users, tenants, geographic data and daily electricity readings. Caching was one measure\namong several. Next to targeted indexes, GiST for spatial queries and composite ones for tenant\nand date filters, and time-based partitioning of the electricity table, some query results were\nkept as materialized views, and query caching and connection pooling were set up to cut overhead.\nResponse times improved by 10x, critical operations ran in milliseconds and resource use fell by\n40%. Those gains belong to the work as a whole, so the share that caching contributed is not\nmeasured on its own.\n\n## Where it goes wrong\n\n**After a deploy, some visitors still get the old version.** The response carried no explicit\nfreshness, so caches estimated one. RFC 9111 lets them use a fraction of the time since\n`Last-Modified` and names 10% as a typical setting, so a file last changed a month ago can be\nreused for about three days without asking. Send an explicit `Cache-Control` on every response,\nput a content hash in static file names, and keep the HTML that points at them short-lived.\n\n**A purge clears the CDN, and browsers keep the old copy.** A purge removes the CDN's copy and\nnothing else; each browser that stored the response keeps it until its own lifetime runs out. Give\npublic responses a short `max-age` for browsers and a longer `s-maxage`, which only shared caches\napply, so the copy that can be purged is the one that lives longest. When a browser copy must\nchange at once, change the URL.\n\n**A page that was briefly missing stays missing.** RFC 9110 makes a 404 heuristically cacheable,\nso a cache may keep one that came with no freshness information for a lifetime it estimates\nitself. A link followed a moment before a page was published can keep returning 404 after the page\nexists. Give error responses an explicit short lifetime, and purge the URL when the page goes\nlive.\n\n**An old value is written back after an update.** In cache-aside, a reader misses and reads the\nrow; before it stores the value, a writer updates the row and deletes the entry; then the reader\nstores what it read, and the old value stays until something removes it. Delete the entry after\nthe transaction commits, and give every entry a TTL so that a raced or missed invalidation ends on\nits own. In Redis, a plain `SET` discards any earlier expiry, so set the TTL in the same command:\n\n```text\nSET price:42 \"19.90\" EX 300\n```\n\n## Further reading\n\n- [RFC 9111, Section 3: Storing Responses in Caches](https://www.rfc-editor.org/rfc/rfc9111#section-3)\n  — when a response may be stored, followed by the freshness, revalidation and invalidation rules.\n- [RFC 9110, Section 13: Conditional Requests](https://www.rfc-editor.org/rfc/rfc9110#section-13)\n  — `ETag`, `If-None-Match` and the 304 response that make revalidation cheap.\n- [RFC 5861: HTTP Cache-Control Extensions for Stale Content](https://www.rfc-editor.org/rfc/rfc5861)\n  — the two directives that let a cache serve stale responses on purpose.\n- [RFC 8246: HTTP Immutable Responses](https://www.rfc-editor.org/rfc/rfc8246) — the `immutable`\n  directive for files whose URL changes with their content.\n- [Workbox: caching strategies](https://developer.chrome.com/docs/workbox/caching-strategies-overview)\n  — cache first, network first and stale-while-revalidate in a service worker, with code.\n- [PostgreSQL: Materialized Views](https://www.postgresql.org/docs/current/rules-materializedviews.html)\n  — keeping a query result inside the database and refreshing it on demand.\n\n\u003chttps://engineer.company/glossary/caching/\u003e\n","date_modified":"2026-10-30T02:51:31Z","description":"How caching decides when a stored answer is still safe to serve, how HTTP headers and databases express that rule, and the mistakes that serve stale data.","formats":{"gemtext":"https://engineer.company/glossary/caching/index.gmi","gopher":"https://engineer.company/glossary/caching/gophermap","html":"https://engineer.company/glossary/caching/","json":"https://engineer.company/glossary/caching/index.json","markdown":"https://engineer.company/glossary/caching/index.md","pdf":"https://engineer.company/glossary/caching/index.pdf"},"kind":"page","language":"en","title":"What is caching?","url":"https://engineer.company/glossary/caching/","version":1}