# Edge Caching vs. Browser Caching

> Balancing blazingly fast zero-latency load times with fresh data.

Subject: Internet | Lesson 23 of 35 | 5 min read
Interactive lesson: https://codewithanimation.in/internet/edge-caching
Previous: [Content Delivery Networks](https://codewithanimation.in/internet/cdns.md) | Next: [The Parsing Phase](https://codewithanimation.in/internet/parsing-phase.md)

## What it is

**Edge Caching (CDN Level)**: A shared cache located on geographically distributed servers closer to end-users. It intercepts requests before they reach the origin server, serving the same cached asset to multiple users.

**Browser Caching (Local Level)**: A private cache stored locally on the user's device. It eliminates network requests entirely on repeat visits, making load times nearly instantaneous for that specific user.

## Why it matters

Understanding caching is critical for balancing performance (fast load times) with freshness (ensuring users see the latest updates).

Developers control this balance primarily through HTTP response headers like `Cache-Control`, directly impacting whether a user gets a 0ms load or is forced to download the whole page again.

## How it works

When a browser requests an asset, the request travels through a hierarchy.

1. **Browser Cache**: The browser checks its local cache. If fresh, it serves it immediately (0ms latency).

2. **Edge Cache**: If the browser cache misses, the request hits the nearest CDN node. If fresh, the CDN responds.

3. **Origin Server**: If the edge cache misses, the request travels to the application's origin server. The origin generates the response and passes it back, storing it in the Edge and Browser caches along the way.

## Common mistake: The 'Forever Stale' Trap

Setting a high `max-age` (e.g., 1 year) on mutable files like `index.html` or CSS/JS files without using file fingerprinting (hashing) is a disaster.

You might deploy a critical bug fix, but users keep seeing the broken version for months because their local browser cache refuses to check the server for updates.

## Tip: Pro tip

**Browser Cache is your Backpack**: It's right there with you. Pulling a book out is instant, but it only holds your books (private cache).

**Edge Cache is the Local Branch Library**: It's a short walk down the street (low latency). It holds books for everyone in your neighborhood (shared cache).

**Origin Server is the Main Publishing House**: Across the country. It has everything, but ordering a new book takes a long time to ship (high latency).

## Try it: Try it

Send requests and watch how the Cache-Control headers dictate where the file is served from.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/edge-caching

## Check yourself

### Question 1

Which Cache-Control directive forces the browser to check with the server before using a cached file?

- A. no-store
- B. public, max-age=3600
- C. immutable
- D. no-cache

Correct answer: D

no-cache actually means 'you can keep a cached copy, but you MUST revalidate it with the server before using it'.

- Why not A: Prevents caching entirely, forcing a full download every single time.
- Why not B: Tells the browser to blindly use the cached copy for an hour without checking the server.
- Why not C: Tells the browser the file will literally never change.

### Question 2

What is the primary functional difference between a browser cache and an edge cache (CDN)?

- A. A browser cache stores only images, while an edge cache stores HTML.
- B. An edge cache is faster to access than a browser cache.
- C. A browser cache is private to a single user on their device, while an edge cache is a shared cache.
- D. Browser caches use Cache-Control headers, while edge caches use proprietary CDN settings.

Correct answer: C

Browser caches only benefit the specific device they are on (private cache), whereas edge caches benefit any user physically near that edge server (shared cache).

- Why not A: Both caches can store any type of HTTP response.
- Why not B: Accessing the local browser cache requires zero network requests, making it inherently faster.
- Why not D: Both caches fundamentally respect standard HTTP Cache-Control headers.

### Question 3

Your team implemented 'Cache-Control: max-age=31536000, immutable' for JS files. What critical practice MUST accompany this strategy?

- A. Setting stale-while-revalidate alongside it.
- B. File name hashing/fingerprinting (e.g., app.v2.js).
- C. Clearing the CDN cache via API on every deployment.
- D. Using a Service Worker to force-refresh.

Correct answer: B

By hashing filenames, any change to the file results in a new URL. The browser downloads the new file while safely ignoring the old, heavily-cached one.

- Why not A: immutable tells the browser never to revalidate. This is contradictory.
- Why not C: Purging the CDN does not wipe the end-user's private browser cache.
- Why not D: Force-refreshing ruins the user experience.

## Remember this

- Browser cache is private and instantaneous.
- Edge cache is shared and low-latency.
- Always hash filenames for long-lived cached assets, and use `no-cache` for HTML files.
