sessionStorage, IndexedDB, and Choosing the Right API
Compare browser storage options by persistence, capacity, and synchronous vs asynchronous access.
sessionStorage, IndexedDB, and Choosing the Right API is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
sessionStorage Shares the API
sessionStorage exposes the exact same methods as localStorage but its lifetime is tied to the tab. When the tab or window closes, its data is cleared automatically.
It is also isolated per tab, so two tabs do not share sessionStorage data.
A Multi-Step Form Use Case
sessionStorage shines for a multi-step wizard where you want to preserve progress across reloads within the session but discard it once the tab closes.
The user can refresh mid-form without losing entries, yet nothing lingers after they finish or leave.
IndexedDB: Async and Large
IndexedDB is an asynchronous, transactional database built into the browser. It can store far more than localStorage 5MB limit and handles structured objects, arrays, and even binary data.
Because it is async, it does not block the main thread like localStorage does.
IDB-Keyval Promise Wrapper
The raw IndexedDB API is verbose and event-based, so libraries like idb-keyval wrap it in simple Promise-based functions such as get, set, and del.
This gives you localStorage-like ergonomics with IndexedDB capacity and async behavior.
IndexedDB for Large Blobs
Because IndexedDB can store Blob and ArrayBuffer values, it is ideal for caching images, audio, downloaded files, or offline assets.
These would quickly blow past localStorage limits and would have to be stringified, which IndexedDB avoids.
The Cache API for HTTP Responses
The Cache API, often used with service workers, stores whole HTTP Response objects keyed by request. It is purpose-built for offline-first caching of network responses.
Use it to serve assets and API responses without hitting the network when offline.
Persistence Comparison
localStorage persists permanently until cleared, sessionStorage lasts only for the tab session, IndexedDB persists permanently, and cookies have configurable expiry via their attributes.
Matching the lifetime you need to the right store avoids surprises about when data disappears.
Capacity Comparison
localStorage and sessionStorage are limited to roughly 5MB, cookies to about 4KB each, while IndexedDB can use hundreds of megabytes or more depending on the device and browser.
For anything sizable, IndexedDB is the only practical browser option.
Synchronous vs Asynchronous
localStorage, sessionStorage, and cookies are synchronous, blocking the main thread during access. IndexedDB and the Cache API are asynchronous and Promise-based.
Async access keeps the UI responsive even when reading or writing larger amounts of data.
Choosing by Use Case
Use httpOnly cookies for auth tokens so scripts cannot read them, localStorage for small persistent preferences, IndexedDB for large files and complex offline data, and sessionStorage for short-lived drafts within a tab.
Match the store to the data sensitivity, size, and lifetime.
Putting It Together
There is no single best storage API; each trades off capacity, persistence, sync behavior, and security. Knowing their differences lets you pick deliberately.
A typical app uses several: cookies for auth, localStorage for prefs, and IndexedDB for offline assets.
Quick Check
Test your understanding of storage choices.
Recap
You compared sessionStorage, localStorage, IndexedDB, cookies, and the Cache API across persistence, capacity, sync behavior, and security.
You learned to choose by use case: cookies for auth, localStorage for small prefs, IndexedDB for large or complex data, and sessionStorage for tab-scoped drafts.
Frequently asked questions
Is the “sessionStorage, IndexedDB, and Choosing the Right API” lesson free?
Yes — the full text of “sessionStorage, IndexedDB, and Choosing the Right API” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “sessionStorage, IndexedDB, and Choosing the Right API”?
Compare browser storage options by persistence, capacity, and synchronous vs asynchronous access. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “sessionStorage, IndexedDB, and Choosing the Right API” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reading and Writing localStorage in React
- Syncing State with localStorage
- Handling Storage Events Across Tabs
- sessionStorage, IndexedDB, and Choosing the Right API