Chapter 6 · Database & Caching
Revision sheet
Every lesson's idea and recap on one page. Read it before an interview, or print it.
-
6.1Database & Caching Overview
The browser has five storage tiers — pick by size, lifetime, and who else needs to read it.
- Size, lifetime, and audience pick the tier; nothing else matters much.
- Cookies are the only store the server sees, and they cost bytes on every request.
- Caching is easy; invalidation is the design decision worth defending.
-
6.2Local Storage
Five megabytes of persistent strings, read and written synchronously on the main thread.
- Persistent, synchronous, string-only, ~5MB, per origin.
- Wrap in try/catch and parse defensively — it will contain old or invalid data.
- Small preferences only; anything larger belongs in IndexedDB.
-
6.3Session Storage
Same API as localStorage, but the data dies when the tab does — and never leaves that tab.
- Tab-scoped, origin-scoped, dies on close, copied on tab duplication.
- Right for wizards, drafts, and per-tab UI state.
- No cross-tab
storageevent, because nothing is shared.
-
6.4Cookie Storage
Four kilobytes that ride along on every single request — which is both the feature and the cost.
- The only client store the server sees, and the only one with an XSS-proof mode.
HttpOnly+Secure+SameSiteis the non-negotiable trio for auth.- Size matters: cookies are upload cost on every single request.
-
6.5Indexed DB
An asynchronous, transactional, indexed store for hundreds of megabytes of structured data.
- Async, transactional, indexed, gigabytes — the only serious client-side store.
- Schema changes live in
onupgradeneededand nowhere else. - Use a promise wrapper, index every query field, and treat it as evictable.
-
6.6Normalization
Store every entity once, by id, and reference it everywhere else — duplicates are bugs waiting to happen.
- One entity, one place, referenced by id.
- Keep order in separate id arrays so a record can appear in many lists.
- Normalize at the API boundary; re-join with memoised selectors.
-
6.7HTTP Caching
Two headers decide whether a request happens at all, and whether it costs a full download.
- Fresh = no request at all; stale = a cheap
304revalidation. - Hash your filenames and cache them for a year; never cache the HTML that points at them.
stale-while-revalidatebuys instant responses without long-term staleness.
- Fresh = no request at all; stale = a cheap
-
6.8Service Worker Caching
A programmable proxy inside the browser — you write the cache policy in JavaScript.
- The service worker is a proxy you program; HTTP caching is a policy you declare.
- Match strategy to resource: shell cache-first, documents network-first, feeds SWR.
- Version and bound your caches, and always have an escape hatch.
-
6.9API Caching
Cache responses by key in the client, then decide the one hard thing: when they stop being true.
- The key is the contract: endpoint plus every parameter that changes the answer.
staleTimeper resource, mutation invalidation for correctness, events for multi-user accuracy.- Optimistic updates without a snapshot and rollback are a bug, not a feature.
-
6.10State Management
Sort state by who owns it — server, URL, or client — and most of the hard problem disappears.
- Server, URL, client, form — classify before choosing a tool.
- Filters and pagination belong in the URL, not in a store.
- Context broadcasts; stores let components subscribe selectively.
Capstone
Design the full caching strategy for a news feed that must open instantly, work offline, and never show a deleted article.