
Client-Side Storage Options: Choosing the Right Store
Storage bugs can trace back to defaulting to the familiar store instead of the fitting one. A five-input decision procedure and a worked table for the browser's five stores.
TL;DR: The browser gives you five places to store data — cookies, localStorage/sessionStorage, IndexedDB, the Cache API, and OPFS — and storage bugs can trace back to defaulting to the familiar one instead of choosing by fit. This guide gives you a five-input decision procedure and a worked table mapping common data classes to the store that fits each, so the choice is made on data shape rather than habit.
One decision, five stores
Choosing the wrong store is cheap today and expensive later. String-stuffed localStorage becomes an unqueryable blob the moment you need to look something up by a field. State parked in a cookie taxes every request the cookie matches with bytes the server may not need. And migrating from one store to another after launch means migrating user data in the field, on devices you do not control, for users who may be offline when your new version loads. The store you pick is a decision your future self inherits.
A deliberate choice runs on five inputs, and stating them turns an instinct into a procedure. Data shape: is this a string, a structured record, a request/response pair, or raw file bytes? Size order: kilobytes, megabytes, or larger? Access pattern: a single key lookup, a query by index, or a full scan? Thread: does only the main thread touch it, or does a Web Worker need it too? Lifetime class: what happens to the user if this data is gone tomorrow morning? Run those five questions against a piece of data and the right store usually names itself.
The four script storage APIs — localStorage/sessionStorage, IndexedDB, the Cache API, and OPFS — share the same underlying physics: origin-scoped, quota-governed, and, for data in local storage, evictable unless the origin has been granted persistent storage. Cookies sit outside that layer, scoped by domain and path rather than by origin, with their own size limits. That shared layer is the quota-and-eviction foundation's territory, not this article's. Here the physics enters only as the fifth input: the lifetime question. This article owns the choice among the stores; the foundation owns the ground the script stores stand on.
Cookies: headers, not storage
A cookie is not really a storage mechanism. It is a request header the browser replays to the server on every matching request, and its reason to exist is server-visible state — a session identifier the backend reads to know who is calling. Treating it as general storage is the original client-side sin.
The costs are structural. Each cookie carries a tiny size ceiling, and because cookies ride along on every request they match, anything you store in them is bandwidth you pay repeatedly for the life of the session. Cookies also sit inside a legal layer, though not one unique to them. In the EU, for example, Article 5(3) of the ePrivacy Directive requires Member States to ensure that storing information, or gaining access to information already stored, in the terminal equipment of a subscriber or user is allowed only if that subscriber or user has given consent, having been provided with clear and comprehensive information, among other things about the purposes of the processing. The provision does not mention cookies; it speaks of storing or accessing information in the terminal equipment generally. It does not prevent technical storage or access for the sole purpose of carrying out the transmission of a communication over an electronic communications network, or as strictly necessary for the provider of an information society service explicitly requested by the subscriber or user to provide that service. As a directive, it applies through each Member State's national law, and other markets need their own check; that legal layer belongs to the privacy literature and is noted here rather than explored. The rule that keeps cookies in their lane is short: if the server does not need this value on every request, it does not belong in a cookie.
localStorage and sessionStorage: the synchronous small stuff
localStorage can feel like the obvious first choice, and it is the one to reach for least often. It is synchronous, string-only, and holds roughly megabytes of data (the Storage Standard registers a 5 MiB quota for localStorage, and MDN reports that browsers can store up to 5 MiB of local storage and 5 MiB of session storage per origin; as of Nov 2026). Its legitimate uses are genuinely small: a handful of preferences, a feature-flag snapshot, the last tab the user had open — values that fit in a few keys and can be re-derived if lost.
Three disqualifiers rule out everything else, and naming them plainly helps prevent misuse. localStorage is synchronous and main-thread-blocking: every read and write stalls the thread that also paints your UI, so a large value is a jank source. It is string-only: storing structured data means serializing it, and once you are calling JSON.stringify on anything past trivial size you have chosen the wrong store. And it is invisible to workers: Web Workers cannot touch localStorage at all, so any data a background thread needs is disqualified from it by definition. Application datasets, anything security-sensitive, anything a worker reads — none of it belongs here.
sessionStorage shares localStorage's shape but has one honest niche: per-tab scoping. Each tab keeps its own session storage that other tabs do not share; a tab or window opened from it with an opener (for example through window.open) does start with a copy of that data, but the two copies then change independently. That makes it the right home for state that belongs to a single tab — a multi-step flow the user might open twice, for instance — and the wrong home for anything that should persist past the tab closing.
IndexedDB: the structured floor
IndexedDB is the default answer for application data on the device, and the useful mental correction is to stop seeing it as difficult and start seeing it as the floor everything structured stands on. Its operations are asynchronous: a read or write call returns a request object and the result arrives later through events, instead of handing back a value the way localStorage does — although the specification has put() and add() copy the value on the calling thread before the request is queued (implementations can optimize this where the difference is not observable). It stores structured records with indexes and transactions, so you can look data up by key or by index without scanning. It is accessible from workers. And its quota is large enough to hold real datasets rather than a handful of keys.
What it is shaped for follows directly: record sets you retrieve by key or index, offline caches of server entities, and the write outbox that holds mutations made while disconnected. When a feature needs to keep application data on the device, IndexedDB is the answer until a specific reason points elsewhere.
What it is not is a relational engine. There are no joins, no ad hoc query language, no SQL. When your query shapes genuinely outgrow key-and-index access — when you find yourself hand-rolling joins across object stores — that is the signal to consider the OPFS-backed SQLite ceiling, which its own article covers. And working with IndexedDB well — schema design, version upgrades, the wrapper libraries worth adopting — is its own article too. Here the only claim is the decision boundary: structured application data on the device starts at IndexedDB.
Cache API and OPFS: request pairs and raw bytes
Two stores serve narrower shapes, and knowing their exact fit keeps them from being either overlooked or misused.
The Cache API stores request/response pairs. It is the correct store precisely when the unit of data is an HTTP response — a fetched asset you want to serve again without the network. The asset-caching strategy built on top of it, the service-worker patterns and the freshness rules, belong to the PWA and HTTP-caching literatures; this article's claim stops at "the Cache API stores request/response pairs," one boundary sentence and no further.
OPFS — the Origin Private File System — stores raw byte files in a filesystem that belongs to your origin and is invisible to the user's file manager. It is the substrate for database engines, large binary blobs, and anything that needs byte-range access rather than whole-value reads. Two facts make it a real option rather than an experiment. Its synchronous access handles are available only in dedicated workers, so choosing OPFS's synchronous path is also choosing a worker architecture (the depth lives in the ceiling article); reading through getFile() and writing through createWritable() also work on the main thread, although createWritable() did not reach Safari until Safari 26, per MDN's browser-compat-data (as of Nov 2026). And its cross-browser baseline is genuinely broad — it has been Baseline Widely available since September 2025 per web-features, with the full feature, including the synchronous form of the access-handle methods, arriving in Chrome 108 (Chrome for Android 109), Firefox 111, and Safari 16.4, and the navigator.storage.getDirectory() entry point earlier, in desktop Chrome 86 and Safari 15.2 (as of Nov 2026; check the current status before relying on it). OPFS is a legitimate row in the decision, not a Chromium-only frontier.
The worked decision table
The procedure becomes concrete when common data classes are mapped to the store each fits. This table is the payoff of the five inputs applied in advance.
| Data class | Store | Why |
|---|---|---|
| Session token | Cookie (httpOnly, set server-side) | The server reads it on each request it is sent with; it is server-bound state by nature |
| UI preferences, theme, last-view | localStorage | Small, string-serializable, re-derivable if lost, no worker needed |
| Per-tab wizard state | sessionStorage | Kept per tab, though copied into a tab it opens with an opener; discardable when the tab closes |
| Offline cache of server entities | IndexedDB | Structured records queried by key/index, worker-accessible, large |
| Queued offline mutations (outbox) | IndexedDB | Transactional, but evictable unless persistent storage is granted, so unrecoverable until synced — the floor for it |
| Large relational working set | OPFS-backed SQLite (WASM) | Query shapes need joins and a planner IndexedDB cannot give |
| Binary blobs (media, model files) | OPFS or Cache API | By access pattern: byte-range and engines to OPFS, whole HTTP responses to Cache API |
Combining stores is normal, not a smell. An application can use three or four at once — a cookie for the session, localStorage for preferences, IndexedDB for the offline cache and the outbox, OPFS for a large binary — each holding the data class it fits. And two questions can catch misfits before they ship. Does a worker need this data? If so, localStorage is out, and among workers only a service worker can reach cookies, through the Cookie Store API (as of Nov 2026). And what happens to the user if this is evicted tonight? If the answer is "they lose work," the data is unrecoverable, it belongs in a durable store, and it must be synced off the device rather than trusted to survive on it — which hands the thread back to the quota-and-eviction physics behind the script storage on this list.