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
28

Frontend System Design · Lesson 28 of 86 · 4 min

CDN Cache Behavior and Invalidation

In an interview, you need to prove you can control the cache, not just use a CDN. This means talking specifics about HTTP headers and invalidation trade-offs.

When a user requests an asset, your CDN has to decide: serve a cached copy or fetch a new one from your origin server? You control that decision. Your job is to configure caching to be as aggressive as possible for performance, while still ensuring users get updates promptly.

How to Control the Cache with HTTP Headers

You instruct CDNs and browsers on how to behave using HTTP response headers set by your origin server. The most important one is Cache-Control.

Cache-Control is a directive to all caches between your server and the user.

  • public: Allows any cache (CDN, proxy, browser) to store the response. This is essential for CDN caching.
  • max-age=<seconds>: The time a response is considered fresh for all caches, including the user's browser.
  • s-maxage=<seconds>: Same as max-age, but only for shared caches (like a CDN). It overrides max-age for the CDN, letting you set different policies for the CDN and the browser.
  • immutable: A hint that the file will never change. Use this only on versioned assets (cache-busted files). It prevents the browser from even trying to revalidate.

Other useful headers include ETag and Last-Modified. These are validators. When a cache holds a stale asset, it can send the ETag value back to the origin. If the asset hasn't changed, the origin replies with a 304 Not Modified status and an empty body, saving bandwidth.

This flow determines whether a request is a "hit" or a "miss".

Here is how you'd set these headers in a Node.js/Express app serving static files. Notice the different strategies for versioned assets versus the main index.html.

// server.ts (using Express)
import express from 'express';
import path from 'path';

const app = express();

const staticOptions = {
  setHeaders: (res, filePath) => {
    // Versioned assets get a long cache time.
    // The 'immutable' directive tells browsers the content will never change.
    if (/\.[0-9a-f]{8,}\./.test(filePath)) { // Matches files like 'app.a1b2c3d4.js'
      res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
    } else if (filePath.endsWith('index.html')) {
      // The HTML file should be revalidated more often to pick up new asset URLs.
      // 's-maxage' lets the CDN cache for 1 hour, but browsers must revalidate.
      res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate, s-maxage=3600');
    }
  }
};

app.use(express.static(path.join(__dirname, 'public'), staticOptions));

Choosing Your Invalidation Strategy

When you deploy a change, you need to force the CDN to serve the new version. You have three ways to do this, but one is clearly superior for frontend assets.

1. Versioned URLs (Cache Busting) This is the best practice and should be your default. Your build process generates a unique filename for each asset based on its content, like main.d9e8f7a0.js. The URL is now different, so the CDN sees it as a brand-new asset and fetches it from the origin.

This strategy is bulletproof. You can set extremely long cache times (max-age=31536000) because you know the content at a given URL will never change. The old asset is left to expire naturally.

This is the answer interviewers want to hear for standard asset updates. It demonstrates you understand the build pipeline and prefer reliable, automated solutions over manual intervention.

2. Explicit Purging Most CDNs provide an API to forcibly remove a file from their cache globally. This is also called invalidation.

Use this for emergencies only. For example, if you accidentally deploy a critical bug or expose sensitive data. Purging is not a deployment strategy. Triggering a purge for every deploy is a sign of a broken process.

The trap: Purging can cause a "thundering herd" problem. When the asset is removed everywhere, the next wave of user requests all miss the cache and hit your origin server simultaneously, potentially overloading it.

3. TTL Expiration This is the passive approach. You simply wait for the max-age or s-maxage timer to run out. On the next request, the CDN will revalidate the asset with your origin.

This is too slow and unpredictable for deploying updates to your application code. It's only acceptable for content where freshness isn't critical, like a company logo that changes once a year.

The index.html Dilemma

Your index.html file is a special case. It's the entry point to your single-page application and contains the references to your versioned JavaScript and CSS files.

If you cache index.html for a year, users will never get the new asset URLs when you deploy. If you don't cache it at all, every single navigation to your site hits your origin server, defeating a major purpose of the CDN.

This is a classic trade-off question. Here’s a solid strategy:

  1. For the CDN: Use a moderate TTL. s-maxage=3600 (1 hour) is a reasonable starting point. This ensures most users get a cached index.html from the CDN edge.
  2. For the Browser: Force revalidation on every visit. max-age=0, must-revalidate tells the browser it can't use its local copy without checking with the CDN first.

This combination ensures that when a user visits your site, their browser asks the CDN for index.html. The CDN likely has a copy less than an hour old and serves it. When you deploy, you just have to wait for the CDN's cache (1 hour in this case) to expire, or you can run a targeted purge for just index.html. This one-file purge is acceptable and avoids the thundering herd problem.

Try it

Open the network tab in your browser's developer tools. Visit a major web application like GitHub, Stripe, or Vercel. Find the main JS bundle and the initial HTML document. Inspect their response headers and identify the Cache-Control policy. Can you explain why they chose those specific directives?

Next: We'll discuss the trade-offs between Global State vs Local Component State.

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice system design problems
Previous

CDN Benefits: Latency, Availability, Offload

Next

Global State vs Local Component State