Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Web Performance & Core Web Vitals
  1. 1.Introduction to Web Performance & Business Impact
  2. 2.Core Web Vitals & Practical Measurement Tools
  3. 3.Optimizing the Critical Rendering Path & Basic Asset Delivery
  4. 4.JavaScript Performance & Bundle Optimization
  5. 5.Image, Video, and Media Optimization Strategies
  6. 6.Network Performance: HTTP/2, Caching, and CDNs
  7. 7.Resource Hints & Advanced Loading Strategies
  8. 8.Frontend Architectures & Server-Side Rendering Performance
  9. 9.Performance Budgets, RUM, and Continuous Monitoring
  10. 10.Advanced Browser APIs & Offloading Work with Workers
Web Performance & Core Web Vitals
06

Web Fundamentals · Lesson 6 of 10 · 5 min

Network Performance: HTTP/2, Caching, and CDNs

This is how you explain making a site faster on the wire. Master these topics to answer questions about latency, scalability, and infrastructure-level performance.

How HTTP/2 Fixes HTTP/1.1's Bottlenecks

HTTP/1.1 forces requests into a queue on a single TCP connection, a problem called head-of-line blocking. A slow request for a large image holds up subsequent requests for small, critical CSS files. The old workaround was to open multiple TCP connections (usually 6 per domain), but each connection has overhead.

HTTP/2 solves this with multiplexing. It sends multiple requests and responses concurrently over a single TCP connection, broken into binary frames. A large response doesn't block smaller ones. This single connection is more efficient, especially over high-latency mobile networks.

Most modern servers and CDNs enable HTTP/2 by default over HTTPS. You can verify this in your browser's DevTools: right-click the Network tab's header and enable the "Protocol" column. You should see h2 for your assets.

The interview question: "With HTTP/2, should we stop bundling our assets?"

Not entirely. The performance penalty for many small files is much lower with HTTP/2, so creating dozens of tiny files is no longer an anti-pattern. However, bundling still has benefits: better compression ratios on larger files and reduced overhead from processing many modules. The modern approach is a balance: split code into logical chunks (e.g., per-route, vendor libraries) instead of one giant app.js or hundreds of tiny modules.

A few other HTTP/2 features are worth knowing, but less critical. Header compression (HPACK) reduces the size of redundant HTTP headers. Server Push was an early feature for sending assets before the browser requested them, but it was hard to use correctly and is now largely deprecated in favor of Resource Hints.

How to Design a Caching Strategy

Caching is about avoiding network requests. There are two main layers you control: the HTTP cache (in the browser) and the Service Worker cache (in your code). You will be expected to know how and when to use both.

HTTP Caching: Instructions for the Browser

You configure the browser's cache using HTTP response headers. The browser handles the rest. This is your first and most important caching layer.

  • Cache-Control is the main header.
    • max-age=<seconds>: Tells the browser how long to use the cached asset without checking back with the server.
    • no-cache: The browser can cache the file but must revalidate with the server (using ETag or Last-Modified) before using it. This is ideal for your main index.html file.
    • no-store: Do not cache this resource at all. Use for sensitive data.
    • public vs. private: public allows shared caches (like a CDN) to store the resource.
    • immutable: A strong hint that the file will never change. Use it with max-age.

The trap: Using the wrong Cache-Control policy. A common mistake is setting a long max-age on index.html, which prevents users from getting new deployments. Another is not setting a long max-age on fingerprinted assets, wasting bandwidth on revalidation.

A solid strategy for a modern web app:

  1. Fingerprinted Assets (app.a1b2c3d4.js, styles.e5f6g7h8.css): Cache-Control: public, max-age=31536000, immutable These files are named by their content. If they change, the URL changes. They can be cached aggressively for one year by browsers and CDNs.
  2. Entry Point (index.html): Cache-Control: no-cache This ensures the user's browser always checks for a new version of the HTML, which will contain references to any new fingerprinted assets.
  3. Dynamic API Data (/api/user): Cache-Control: no-store or a short max-age This depends on data freshness requirements.

This flow chart shows how the browser uses headers like ETag to revalidate a cached resource when max-age has expired or no-cache is set. A 304 Not Modified response saves downloading the resource again.

Service Worker Caching: Programmatic Control

A Service Worker is a JavaScript file that acts as a client-side proxy. It can intercept network requests and decide how to respond: from a cache, from the network, or a combination. This gives you fine-grained control and enables offline functionality.

This is more complex than HTTP caching. You are now responsible for cache storage, versioning, and invalidation. Use it when you need offline support or complex caching logic that HTTP headers can't provide (like stale-while-revalidate).

Here is a basic cache-first strategy for static assets. The Service Worker caches known URLs on install. For any subsequent fetch, it tries the cache first. If not found, it goes to the network, serves the response, and adds it to the cache for next time.

// service-worker.js
const CACHE_NAME = 'static-cache-v1';
const urlsToCache = [
  '/',
  '/styles.css',
  '/app.js',
  '/images/logo.png'
];

self.addEventListener('install', (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then((cache) => {
      return cache.addAll(urlsToCache);
    })
  );
});

self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request).then((response) => {
      // Cache hit - return response from cache
      if (response) {
        return response;
      }
      // Not in cache - fetch from network, then cache it
      return fetch(event.request).then((networkResponse) => {
        // Check for a valid response before caching
        if (networkResponse && networkResponse.status === 200) {
          const responseToCache = networkResponse.clone();
          caches.open(CACHE_NAME).then((cache) => {
            cache.put(event.request, responseToCache);
          });
        }
        return networkResponse;
      });
    })
  );
});

When and Why to Offload to a CDN

A Content Delivery Network (CDN) is a global network of servers that cache your content. When a user requests an asset, the request is routed to the nearest CDN "edge" server instead of your origin server.

Using a CDN is standard practice for any production application with a distributed user base. The primary benefit is reduced latency. Serving a file from a server 100 miles away is much faster than serving it from 5,000 miles away.

CDNs are best for static assets: JS, CSS, images, videos, and fonts. They offload traffic from your origin server, reducing load and bandwidth costs. They also provide a layer of security, often including DDoS mitigation.

The trap: Thinking a CDN solves all latency problems. It only solves latency for static, cacheable content. Requests for dynamic, personalized data must still travel to your origin server. Solving that requires more advanced techniques like edge computing, which is beyond the scope of most frontend interviews.

Try it

Open the network tab on a major media site like nytimes.com or theguardian.com. Reload the page and inspect the main CSS and JavaScript files.

  1. Check the "Protocol" column. Is it h2?
  2. Look at the "Domain". Is it a CDN domain (e.g., assets.guim.co.uk, static.nytimes.com)?
  3. Click on a file and inspect its Response Headers. What is the Cache-Control header? Can you justify why that policy was chosen?

Next: We'll cover how to give the browser hints about what to load next with Resource Hints & Advanced Loading Strategies.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Image, Video, and Media Optimization Strategies

Next

Resource Hints & Advanced Loading Strategies