{"success":true,"name":"cache-on-purpose-decide-what-may-be-stale-for-how-long-and-w","description":"App feels slow or repeats the same expensive fetches, and caching sounds tempting","category":"ai-brimind","path":"ai-brimind/cache-on-purpose-decide-what-may-be-stale-for-how-long-and-w/SKILL.md","file":"SKILL.md","content":"---\nname: cache-on-purpose-decide-what-may-be-stale-for-how-long-and-w\ndescription: \"App feels slow or repeats the same expensive fetches, and caching sounds tempting\"\n---\n\n# Cache on Purpose: Decide What May Be Stale, for How Long, and Who Clears It\n\n## When to use\nYou are about to add caching anywhere in the stack (frontend data cache, backend in-memory cache, HTTP cache headers) — or you already sprinkled some — without a written answer to what data it holds, how long it may be stale, and what event clears it.\n\n## Example request\nProduct list cached on the backend 'for speed' → admin edits a price and the site shows the old one for hours; nobody decided how the cache gets cleared, and that question IS the design. Frontend keeps fetched data in a global variable forever → user updates their profile, other screens still show the old name; data-fetching libraries exist precisely to manage 'when is this stale'. Everything cached 'just in case' → bugs become unreproducible because every layer may serve a different past; cache only what is expensive AND tolerably stale. Exchange rates refreshed once per hour by design → perfect caching: expensive to fetch, fine to be an hour old, with a stated lifetime.\n\n## Instructions\nAct as an architect who respects the old joke that cache invalidation is one of the two hard problems in computer science — because a cache is not a speed-up, it is a deliberate decision to show possibly-old data, and every cache without an invalidation plan is a stale-data bug on a timer. Before adding (or keeping) any cache, do this: (1) Name the exact data being cached and answer in writing: how expensive is it to fetch fresh, and how stale may it be before it lies to users? 'Price of items in a shop' tolerates seconds, not hours; 'list of countries' tolerates days. If the answer is 'it must always be current', do not cache it — speed up the query instead (often the real fix is an index or an N+1 removal, not a cache). (2) Choose the invalidation trigger on purpose and say it: time-based (expires after X — simplest and often enough) or event-based (cleared when the underlying data changes — stronger, but now EVERY write path to that data must clear it; list those write paths before committing to this). (3) Use the mechanism that already exists at that layer instead of a hand-rolled global variable: the data-fetching library's staleTime/refetch on the frontend, HTTP cache headers for static resources, a proper cache store on the backend — caching is a solved, named problem at every layer. (4) Plan for the cache being wrong, because one day it will be: how do you SEE that a value came from cache (log it) and how do you flush it manually when debugging? A cache you cannot inspect or clear turns every data bug into a ghost hunt. (5) If the app is small and the queries are fast, say the honest thing: no cache — the fastest cache to debug is the one that does not exist. Only add the cache once you can state its contents, its maximum staleness, its clearing trigger, and its escape hatch — 'it makes things faster' answers none of those four.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #201\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}