Part III β The Speed
6 Database & Caching
GoalDecide what to store, where to store it, and how long it stays valid.
- 6.1 Database & Caching Overview The browser has five storage tiers β pick by size, lifetime, and who else needs to read it.
- 6.2 Local Storage Five megabytes of persistent strings, read and written synchronously on the main thread.
- 6.3 Session Storage Same API as localStorage, but the data dies when the tab does β and never leaves that tab.
- 6.4 Cookie Storage Four kilobytes that ride along on every single request β which is both the feature and the cost.
- 6.5 Indexed DB An asynchronous, transactional, indexed store for hundreds of megabytes of structured data.
- 6.6 Normalization Store every entity once, by id, and reference it everywhere else β duplicates are bugs waiting to happen.
- 6.7 HTTP Caching Two headers decide whether a request happens at all, and whether it costs a full download.
- 6.8 Service Worker Caching A programmable proxy inside the browser β you write the cache policy in JavaScript.
- 6.9 API Caching Cache responses by key in the client, then decide the one hard thing: when they stop being true.
- 6.1 State Management Sort state by who owns it β server, URL, or client β and most of the hard problem disappears.
Capstone
Design the full caching strategy for a news feed that must open instantly, work offline, and never show a deleted article.