Chapter 6 · Database & Caching
Interview drill
10 questions from this chapter. Answer each one out loud first, then open it to compare.
-
6.1"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.
-
6.2"Why not keep the whole Redux store in localStorage?"
- It's synchronous, so serialising a large store on every change blocks the main thread and hurts INP.
- 5MB is a hard ceiling with no eviction, and writes throw once you hit it.
- Persist a small deliberate slice (preferences, draft ids) and use IndexedDB for anything substantial.
-
6.3"Session storage or local storage for a checkout flow?"
sessionStorage: the draft is meaningful only for this attempt, and two tabs must not overwrite each other.- It survives an accidental refresh, which is the actual failure users hit.
- Anything that must outlive the tab — a saved cart — belongs on the server, keyed to the user.
-
6.4"Where do you store the session, and why?"
HttpOnly; Secure; SameSite=Laxcookie: XSS can't read it and most CSRF is closed by default.- Short-lived access token with server-side refresh rotation, so a stolen cookie has a small window.
- Keep everything else out of cookies — each byte is uploaded on every request for the life of the session.
-
6.5"How do you make a mail client work offline?"
- IndexedDB for messages with indexes on thread and date, plus an
outboxstore for queued sends. - Service worker (9.1) serves the shell and static assets; the data layer reads IDB first, then revalidates.
- Sync is the real design: a cursor/
updatedAtwatermark, last-write-wins or explicit conflict UI, and replay of the outbox on reconnect.
- IndexedDB for messages with indexes on thread and date, plus an
-
6.6"A WebSocket sends you an updated post. How does the UI update?"
- Normalized store: write the entity once by id, and every list referencing it re-renders consistently.
- Denormalized store: you'd have to locate every copy, and the one you miss becomes an intermittent bug.
- Keep order arrays separate from entities so live inserts and removals don't disturb the data itself.
-
6.7"You deployed but users still see the old version. Why?"
- The HTML was cached with a
max-age, so browsers never asked for the new one and kept referencing old asset URLs. - Correct split:
no-cacheon the HTML document,immutable+ longmax-ageon hashed assets. - Purge the CDN for the document too — the edge can hold a stale copy even after the origin updates.
- The HTML was cached with a
-
6.8"How would you make a news site work offline?"
- Precache the shell, network-first for documents with an offline fallback, cache-first for hashed assets.
- Stale-while-revalidate for the feed so it opens instantly and refreshes behind the user.
- Version the caches, cap the image cache, and keep payments network-only — never serve a stale transaction.
-
6.9"Two components need the same data. How many requests?"
- One — the cache deduplicates by key and both components subscribe to the same entry.
- That's precisely why the key must include every parameter; a missing filter serves the wrong cached data.
- Layer invalidation: TTL as the floor, mutation invalidation for your own writes, and events for other users' writes.
-
6.10"Which state management library would you choose?"
- Reframe first: most of the state is server state, so the real answer is a caching library plus the URL.
- What's left is genuinely small — theme, modals, cart — and
useStateplus one lightweight store usually covers it. - Choose Redux when you need time-travel debugging, middleware, or a large team needing strict conventions — not by default.