Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Frontend System Design Roadmap
  1. 1.HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)
  2. 2.TCP and TLS Handshakes: Connection Setup Costs
  3. 3.Browser Network Stack and Connection Pooling
  4. 4.GET vs POST and Idempotency in HTTP
  5. 5.The Critical Rendering Path (CRP)
  6. 6.DOM, CSSOM and the Render Tree
  7. 7.Layout, Paint and Compositing
  8. 8.Reflows and Repaints Explained
  9. 9.Event Loop: Call Stack, Task and Microtask Queues
  10. 10.Async JavaScript: Promises and async/await
  11. 11.Web Workers: Multi-threading in the Browser
  12. 12.Browser Storage: Cookies, Web Storage, IndexedDB
  13. 13.Client-Side Storage: Limits and Security Risks
  14. 14.Choosing the Right Client-Side Storage
  15. 15.HTTP Caching: Cache-Control, ETag, Last-Modified
  16. 16.Browser-Level Caching Mechanisms
  17. 17.Cache Invalidation for Frontend Apps
  18. 18.Structuring a Large Single-Page App
  19. 19.Component-Based Architecture Principles
  20. 20.Props, State and Component Lifecycle
  21. 21.Module Bundling Basics
  22. 22.Core Web Vitals: LCP, INP and CLS
  23. 23.FCP and Time to Interactive (TTI) Explained
  24. 24.Measuring Performance: Lighthouse and WebPageTest
  25. 25.Asset Compression and Minification Basics
  26. 26.What Is a CDN and How Does It Work?
  27. 27.CDN Benefits: Latency, Availability, Offload
  28. 28.CDN Cache Behavior and Invalidation
  29. 29.Global State vs Local Component State
  30. 30.State Patterns: Redux, Zustand and Context
  31. 31.Data Fetching: React Query and SWR
  32. 32.Immutable Data in Frontend State
  33. 33.SSR vs CSR: Rendering Trade-offs
  34. 34.Hydration and Time to First Byte (TTFB)
  35. 35.Static Site Generation (SSG) for Speed and SEO
  36. 36.Incremental Static Regeneration (ISR)
  37. 37.Micro-Frontends: Core Principles
  38. 38.Micro-Frontend Integration Strategies
  39. 39.Communication Between Micro-Frontends
  40. 40.Micro-Frontends: Pros and Cons for Large Teams
  41. 41.Real-Time: Polling, SSE and WebSockets
  42. 42.Real-Time Use Cases: Chat, Alerts, Live Data
  43. 43.Designing a Real-Time Data Flow
  44. 44.Scaling WebSocket Connections
  45. 45.Responsive Images: srcset, sizes and picture
  46. 46.Modern Image Formats: WebP, AVIF, JPEG XL
  47. 47.Lazy Loading Images and Videos
  48. 48.Video Streaming: HLS, DASH, Adaptive Bitrate
  49. 49.REST and GraphQL for Frontend Engineers
  50. 50.Batching, Throttling and Debouncing API Calls
  51. 51.Handling API Errors in the Frontend
  52. 52.Authentication Flows: OAuth and JWT
  53. 53.Global Error Boundaries in React
  54. 54.Error Reporting: Sentry and LogRocket
  55. 55.Graceful Degradation and Fallback UIs
  56. 56.Real User Monitoring (RUM)
  57. 57.CORS: Cross-Origin Resource Sharing
  58. 58.Preventing XSS (Cross-Site Scripting)
  59. 59.CSRF Protection for Frontend Apps
  60. 60.Content Security Policy (CSP)
  61. 61.Storing Auth Tokens: Cookies vs localStorage
  62. 62.PWA Manifest and Installability
  63. 63.Service Workers: Lifecycle and Caching Strategies
  64. 64.Offline Support and Background Sync
  65. 65.Push Notifications for Web Apps
  66. 66.Webpack vs Vite vs Parcel
  67. 67.Code Splitting, Tree Shaking, Asset Optimization
  68. 68.Module Resolution and Hot Module Replacement
  69. 69.Monorepo Tooling: Nx and Lerna
  70. 70.Atomic Design for Component Libraries
  71. 71.Theming and Customization in Design Systems
  72. 72.Accessibility in Component Libraries
  73. 73.Component Library Docs, Testing and Versioning
  74. 74.Publishing and Consuming a Component Library
  75. 75.Full-Text Search: Levenshtein and Fuzzy Matching
  76. 76.Indexing Large Datasets in the Browser
  77. 77.Search Performance: Debouncing and Web Workers
  78. 78.Integrating Backend Search Services
  79. 79.Choosing a Chart Library: D3, Chart.js, Recharts
  80. 80.Rendering Large Datasets: Canvas vs SVG
  81. 81.Interactive, Responsive Data Visualizations
  82. 82.Data Fetching and Aggregation for Dashboards
  83. 83.A/B Test Design and Rollout Strategies
  84. 84.Client-Side vs Server-Side A/B Testing
  85. 85.Feature Flags and Remote Configuration
  86. 86.Experiment Data Collection and Analytics
Frontend System Design Roadmap
14

Frontend System Design · Lesson 14 of 86 · 4 min

Choosing the Right Client-Side Storage

This is a classic frontend interview question that tests your understanding of trade-offs in performance, security, and state management.

After this, you will know exactly which storage to pick for any given scenario and be able to justify your choice. The decision comes down to four factors: persistence, scope, size, and whether the server needs the data.

The Four APIs and Their Trade-offs

You have four tools in the browser for storage. Don't just list them in an interview; frame them by their primary use case.

Cookies are for data the server needs. They are automatically sent with every HTTP request to your domain, which is their main feature and their main performance cost. The size limit is tiny, around 4KB.

Use cookies for session identifiers. A server-set, HttpOnly, Secure, and SameSite=Strict cookie is the standard way to handle authentication tokens.

LocalStorage is for persistent client-only data. It's a simple key-value store (strings only) that persists until cleared by you or the user. The limit is much larger, typically 5-10MB.

Use localStorage for user preferences like theme (dark/light mode) or remembering a user's name.

The trap: Never store sensitive information like session tokens or JWTs in localStorage. It's accessible via JavaScript, making it vulnerable to Cross-Site Scripting (XSS) attacks. If an attacker injects a script onto your page, they can read everything in localStorage and impersonate your user.

SessionStorage is localStorage with a shorter lifespan. It's a key-value store that is cleared when the browser tab is closed. The data is isolated to that specific tab.

Use sessionStorage for temporary data related to a single user task, like persisting form data across refreshes in a multi-step checkout flow.

IndexedDB is a client-side database. It's for large amounts of structured data you need to query, like you would with a NoSQL database. It's asynchronous, transactional, and can store complex objects, files, and blobs. The size limits are generous and often configurable by the user.

Use IndexedDB for building offline-first applications (Progressive Web Apps) or for caching large, complex datasets on the client, like messages in a chat app or documents in an editor.

A Decision Flowchart

When you're asked to choose, walk the interviewer through your logic. This flowchart models that thought process.

How to Store Auth Tokens

This is the most common follow-up question, and getting it right is critical.

The short answer: Use a secure, HttpOnly cookie.

An HttpOnly cookie is inaccessible to JavaScript running on the page. This is your primary defense against a stolen token via an XSS attack. If your authentication token is in localStorage, any injected script can grab it and send it to an attacker's server.

localStorage.setItem('jwt', token); // Don't do this.

The counter-argument is that cookies are vulnerable to Cross-Site Request Forgery (CSRF). This is true, but CSRF can be mitigated effectively by using the SameSite cookie attribute (Strict or Lax) and anti-CSRF tokens. The risk of token theft from XSS is generally considered more severe and harder to prevent than CSRF.

Don't Block the Main Thread

Performance is another key trade-off. localStorage and sessionStorage have synchronous APIs.

// This call blocks rendering until the data is read from disk.
const theme = localStorage.getItem('userTheme');

For small, infrequent reads and writes, this is fine. But if you read or write large amounts of data, you can block the main thread and cause your UI to freeze. Iterating and calling setItem in a loop is a common way to introduce jank.

IndexedDB has an asynchronous API. It uses events (or Promises with a wrapper library) so you can perform database operations without blocking the UI. This makes it the only viable choice for storing and querying large datasets on the client.

Code: Persisting a User Preference

This is the canonical example for localStorage: a dark mode toggle. It's simple, non-sensitive data that should persist across sessions.

const THEME_KEY = 'userTheme';
const LIGHT_THEME = 'light-theme';
const DARK_THEME = 'dark-theme';

function applyTheme(theme) {
  document.body.className = theme;
}

function toggleTheme() {
  const currentTheme = localStorage.getItem(THEME_KEY) || LIGHT_THEME;
  const newTheme = currentTheme === DARK_THEME ? LIGHT_THEME : DARK_THEME;
  
  localStorage.setItem(THEME_KEY, newTheme);
  applyTheme(newTheme);
}

// Apply the saved theme on initial load
const savedTheme = localStorage.getItem(THEME_KEY);
if (savedTheme) {
  applyTheme(savedTheme);
}

// Example: hook up to a button
// const themeToggleButton = document.getElementById('theme-toggle');
// themeToggleButton.addEventListener('click', toggleTheme);

In an interview, you can quickly write this to demonstrate you know the API and its most appropriate use case.

Try it

Take a simple multi-step form and use sessionStorage to save the input from each step. The data should persist if the user refreshes the page, but be gone if they close and reopen the tab. Be ready to explain why sessionStorage is a better choice here than localStorage.

Next: We'll move from client storage to network-level improvements in HTTP Caching: Cache-Control, ETag, Last-Modified.

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice system design problems
Previous

Client-Side Storage: Limits and Security Risks

Next

HTTP Caching: Cache-Control, ETag, Last-Modified