FSD Frontend System Design

Part III β€” The Speed Β· Lesson 6.3

Session Storage

In one lineSame API as localStorage, but the data dies when the tab does β€” and never leaves that tab.

Where you've seen itA multi-step application form open in two tabs for two different people, with neither overwriting the other.

  • Time11 min
  • DiagramTab lifetime and isolation boundaries
  • Chapter6 Β· Database & Caching

Diagram

diagram
ISOLATION β€” per tab, not per origin

  β”Œβ”€β”€ Tab 1 ──────────────┐  β”Œβ”€β”€ Tab 2 ──────────────┐
  β”‚ sessionStorage        β”‚  β”‚ sessionStorage        β”‚
  β”‚  draft = {name:"Asha"}β”‚  β”‚  draft = {name:"Ravi"}β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β–² independent copies, same origin, no collision

  β”Œβ”€β”€ Tab 1 ──────────────┐  β”Œβ”€β”€ Tab 2 ──────────────┐
  β”‚ localStorage: draft ──┼──┼── same single value   β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β–² one shared store β€” the two tabs fight
diagram
LIFETIME β€” what survives what

  action                          sessionStorage   localStorage
  ────────────────────────────────────────────────────────────
  reload (F5)                        βœ“ kept           βœ“ kept
  navigate away and back             βœ“ kept           βœ“ kept
  restore tab after crash            βœ“ often kept     βœ“ kept
  duplicate the tab                  βœ“ COPIED         βœ“ shared
  close the tab                      βœ— gone           βœ“ kept
  open a new tab to the same site    βœ— empty          βœ“ kept
  browser restart                    βœ— gone           βœ“ kept

How it works

  1. Identical API to localStorage β€” setItem, getItem, removeItem, clear, strings only, ~5MB.
  2. Scope is the tab plus the origin. A duplicated tab starts with a copy, then diverges.
  3. The storage event does not fire across tabs, because there's nothing shared to notify about.
  4. Survives reloads and in-tab navigation, which makes it right for multi-page flows.
js
// step 2 of a wizard survives a refresh, but not a new tab
sessionStorage.setItem('claim:draft', JSON.stringify(form));

// on mount
const draft = JSON.parse(sessionStorage.getItem('claim:draft') ?? 'null');

Trade-offs

Use sessionStorageUse localStorage
Multi-step form draftsTheme and locale
Scroll position for this visitDismissed banners
Per-tab UI state (open panel)Feature-flag overrides
Anything that must not leak between tabsAnything that should persist

A note on tokens: sessionStorage is sometimes recommended for tokens "because it expires". It doesn't expire β€” it just ends with the tab, and it's equally readable by XSS. The reasoning in 3.5 still applies: prefer HttpOnly cookies.

Interview angle

"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.

Recap

  • Tab-scoped, origin-scoped, dies on close, copied on tab duplication.
  • Right for wizards, drafts, and per-tab UI state.
  • No cross-tab storage event, because nothing is shared.