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
13

Frontend System Design · Lesson 13 of 86 · 4 min

Client-Side Storage: Limits and Security Risks

Knowing the limits and security traps of browser storage separates a junior who knows the APIs from a senior who can be trusted with user data.

How Storage Quotas Will Break Your App

Web Storage is not infinite. Browsers typically grant each origin about 5MB for localStorage and another 5MB for sessionStorage. When you exceed this, the browser will throw an exception, and your app will crash if you haven't handled it.

This isn't a theoretical problem. A feature that caches API responses or a client-side logger can fill this space surprisingly quickly. Users in private browsing or with strict security settings might also have storage disabled entirely.

The trap: Assuming setItem will always succeed.

Never write to localStorage or sessionStorage without a try...catch block. This is non-negotiable for production code.

// theme-manager.ts
type Theme = 'light' | 'dark';
const THEME_KEY = 'user-theme';

/**
 * Sets the user's preferred theme in localStorage, with error handling.
 */
export function setUserTheme(theme: Theme): void {
  try {
    localStorage.setItem(THEME_KEY, theme);
  } catch (error) {
    console.error("Failed to set theme in localStorage:", error);
    // The app should still function, just without persistence.
  }
}

/**
 * Retrieves the user's theme, defaulting if not found or on error.
 */
export function getUserTheme(): Theme {
  try {
    const storedTheme = localStorage.getItem(THEME_KEY);
    if (storedTheme === 'light' || storedTheme === 'dark') {
      return storedTheme;
    }
  } catch (error) {
    console.error("Failed to retrieve theme from localStorage:", error);
  }
  return 'dark'; // Default theme
}

The Performance Cost of Synchronous Storage

localStorage and sessionStorage have a simple, synchronous API. This simplicity is also their biggest performance liability.

Every call to getItem, setItem, or removeItem is a blocking operation on the browser's main thread. If you're storing a small string, it's unnoticeable. But if you read or write a large JSON object—say, a 1MB user profile—you have to JSON.parse() or JSON.stringify() it. This can easily take several milliseconds, blocking rendering and causing UI jank.

This is why IndexedDB exists. Its asynchronous, promise-based API doesn't block the main thread, making it the only viable choice for storing and retrieving large or numerous pieces of data on the client.

Why Storing Auth Tokens in localStorage is a Trap

Storing a session token (like a JWT) in localStorage is a common pattern, but it's also a significant security risk. Anything in localStorage is accessible to any JavaScript running on the page.

If an attacker can inject a script into your page—a Cross-Site Scripting (XSS) attack—they can read the token from localStorage and send it to their own server. With that token, they can impersonate your user.

The more secure alternative is to store the session token in an HttpOnly cookie. The HttpOnly flag makes the cookie inaccessible to JavaScript, completely mitigating this XSS-based theft vector.

An interviewer asks: "Where do you store a JWT?"

Your answer should start with security. "My default is an HttpOnly, Secure, and SameSite=Lax cookie. HttpOnly prevents XSS from accessing the token, and SameSite mitigates CSRF. This is the most secure method for traditional web apps.

If the architecture requires JavaScript access—for example, a SPA that must send the token as a Bearer token to a different domain's API—then we might be forced to use localStorage. In that case, we accept the XSS risk and must be extremely diligent about sanitizing all user input and implementing a strict Content Security Policy (CSP)."

This answer shows you understand the trade-off between security and architectural constraints.

When to Suffer IndexedDB's API

Let's be direct: the IndexedDB API is verbose and complex. You don't use it because you want to; you use it because you have to.

You reach for IndexedDB when you need to build offline-first applications or handle datasets too large for localStorage. Think of apps like Figma, which need to store complex design data locally, or a PWA for a field technician that must function without a network connection.

Its key features justify the complexity:

  • Asynchronous: It doesn't block the main thread.
  • Large Storage: Quotas are much larger than localStorage, often in the hundreds of megabytes or even gigabytes, depending on available disk space.
  • Structured Data: It's a true object store, not just a string-based key-value store. It can index data, allowing for efficient querying.

The practical advice: Don't use the raw IndexedDB API. Use a lightweight wrapper library like idb by Jake Archibald or a more powerful ORM-like library such as Dexie.js. They provide modern, promise-based APIs that are far more pleasant to work with. Mentioning these libraries in an interview shows practical experience.

Try it

Go to a complex web app you use daily (like GitHub, Reddit, or your company's internal dashboard). Open DevTools, go to the Application tab, and inspect its localStorage, sessionStorage, and Cookies. Identify what's being stored. Can you find a session or auth token? If it's in localStorage, you've found a potential XSS vulnerability.

Next: Choose the right client-side storage for the job by combining these constraints into a decision framework.

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice system design problems
Previous

Browser Storage: Cookies, Web Storage, IndexedDB

Next

Choosing the Right Client-Side Storage