Part III — The Speed · Lesson 6.1
Database & Caching Overview
In one lineThe browser has five storage tiers — pick by size, lifetime, and who else needs to read it.
Where you've seen itA mail client holding a theme preference, an unsent draft, a session token, and a full offline mailbox — four different stores, deliberately.
Diagram
How it works
- Start from lifetime. Data that must not outlive the tab goes in
sessionStorage; everything longer-lived needs a deliberate invalidation story. - Never put secrets in web storage — any XSS reads all of it instantly (see 3.5).
- Prefer async storage for anything sizeable.
localStorageis synchronous and blocks the main thread, which shows up directly in INP. - Version your stored shapes. Old data in a new app is a crash waiting to happen — store a schema version and migrate or discard.
- Assume it can vanish. Storage is evictable under pressure and users clear it; treat every cache as an optimisation, never as the source of truth.
Trade-offs
| Store | Size | Sync? | Sent to server | Good for |
|---|---|---|---|---|
| Memory | RAM | sync | no | Derived/session state |
sessionStorage | ~5MB | sync | no | Per-tab drafts, wizards |
localStorage | ~5MB | sync | no | Small prefs, flags |
| Cookies | 4KB | sync | yes | Auth, server-read state |
| IndexedDB | GBs | async | no | Offline data, big lists |
| Cache Storage | GBs | async | no | Requests/responses (SW) |
Interview angle
"Where would you cache the user's feed?"
- Three layers: HTTP cache for the assets, an in-memory client cache for the current session, and IndexedDB for offline availability.
- Normalise entities (6.6) so one updated post updates everywhere rather than in three copies.
- Then state the invalidation rule explicitly — TTL, ETag revalidation, or event-driven purge — because that's the part that actually gets asked about.
Recap
- 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.