FSD Frontend System Design

Chapter 6 · Database & Caching

Revision sheet

Every lesson's idea and recap on one page. Read it before an interview, or print it.

  1. 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.
  2. 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.
  3. 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 storage event, because nothing is shared.
  4. 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 + SameSite is the non-negotiable trio for auth.
    • Size matters: cookies are upload cost on every single request.
  5. 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 onupgradeneeded and nowhere else.
    • Use a promise wrapper, index every query field, and treat it as evictable.
  6. 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.
  7. 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 304 revalidation.
    • Hash your filenames and cache them for a year; never cache the HTML that points at them.
    • stale-while-revalidate buys instant responses without long-term staleness.
  8. 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.
  9. 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.
    • staleTime per resource, mutation invalidation for correctness, events for multi-user accuracy.
    • Optimistic updates without a snapshot and rollback are a bug, not a feature.
  10. 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.