Wesloo
All topicsTopics

Web technologies

How Does Browser Cache Work? A Practical Guide

Short answer

A browser cache stores local copies of web resources so repeat visits skip downloads. Servers set freshness with Cache-Control, and browsers validate or evict entries when space runs low.

Key points

  • Stores images, fonts, HTML, CSS, and JavaScript locally.
  • Cache-Control max-age sets how long a file stays fresh.
  • A cache hit avoids a network download; a miss fetches from the server.
  • Clearing the cache keeps passwords, downloads, and settings intact.

How does browser cache work? It keeps local copies of web resources so a repeat visit can reuse them instead of downloading every file again. The browser saves page resources such as images, fonts, markup, styles, and scripts, then checks server rules before using a saved copy.

What the cache stores

A browser cache is a local store of pieces from sites you visit. It can hold images, fonts, markup, styles, and scripts. The goal is simple: avoid repeating work that already happened.

Cached data is temporary. Entries appear and disappear as you browse, and browsers remove old ones automatically. Over months, stored files can still reach several hundred megabytes or a few gigabytes.

How does browser cache work step by step

On the first visit, the browser asks for needed files, and the server replies with caching instructions. Cache-Control is the principal header, and it defines a file's freshness lifetime in seconds through max-age. The browser then stores the response and its metadata.

On the next visit, the browser looks for a matching entry before making a network request. If it finds one and the entry is still fresh, it uses the local copy. That is a cache hit. If no entry exists, or the entry is stale, the browser fetches from the server. That is a cache miss.

During a normal reload, the browser sends Cache-Control: max-age=0 plus validation headers, so it checks freshness. A force reload sends Cache-Control: no-cache and requires a completely fresh copy from the server.

Cache-Control and freshness

Cache-Control is the main header that sets how long a file stays fresh, with max-age given in seconds. For example, max-age=604800 means one week, and 31536000 means one year, a common choice for logos that never change. When no caching rule is attached, browsers fall back to about one tenth of the elapsed time since the file was last modified.

DirectiveMeaning
max-ageSeconds a response stays fresh
no-cacheStore it, but validate before use
no-storeDo not store the response
privateFor one user, not shared caches

Other directives matter too. no-store tells the browser not to save the response at all. no-cache means the browser may store the response but must check with the server before using it. These rules let a site keep sensitive or frequently changing data out of long-term storage. Mozilla's guide to HTTP caching walks through every header.

Validation with ETag and Last-Modified

Freshness is not the only check. When an entry becomes stale, the browser can ask the server whether its copy is still valid. The server may reply with a 304 Not Modified response, which means the local copy can be reused. This avoids downloading the full file again.

Two common validators are ETag and Last-Modified. ETag is a token that changes when the content changes. Last-Modified is a timestamp. The browser sends the stored value back to the server, and the server decides whether the file changed. This is why a stale entry can still be cheap to reuse.

Cache hit, cache miss, and eviction

A cache hit means the requested data is found locally, so the browser can read it without a network round trip. A cache miss means the data is not found, so the browser requests it from the source. The first case saves time and bandwidth; the second pays the normal cost of a download.

Caches have limited room. Chromium's disk cache removes the least recently used files first. Browsers prune entries constantly, so a cache does not grow without limit. Eviction is automatic and usually invisible.

Memory cache and disk cache

Browsers use more than one cache. A memory cache keeps recently used resources in RAM for very fast access during a session. A disk cache writes resources to storage so they survive after the browser closes. The memory cache is faster but smaller; the disk cache is larger but slower.

Incognito mode keeps its cache in memory instead of writing it to disk, by design, so session files are not left behind. During an incognito session, Chrome may keep certain site data in memory to keep pages functioning, and it removes that data once the session closes. The session ends only after every incognito window is closed, not just one tab.

Browser cache vs cookies vs history

The cache stores files that make pages load faster. Cookies store small pieces of state, such as login sessions and preferences. History stores the list of pages you visited. These systems sit next to each other but do different jobs.

Clearing the cache does not touch saved passwords, downloaded files, or custom settings, because none of those are stored in the cache. Clearing cookies as well can log you out and wipe preferences, but that effect comes from removing cookies rather than from clearing the cache. For a closer look at cookie behavior, see How Do Web Cookies Work? A Practical Guide.

When clearing the cache helps

Clearing helps when files are stale or corrupted. Clearing can repair broken layouts, slow-loading pages, app crashes, and incorrectly displayed files because it forces a fresh download. It removes the local copy and makes the browser fetch a new one.

Clearing the cache is usually simple. It does not remove passwords, downloads, or settings. It also does not fix every problem. If a site is down, a network is slow, or an account is locked, a cache purge will not change that.

When clearing does nothing

A cache purge cannot fix server errors, DNS problems, or expired sessions. It also cannot speed up a site that already sends fresh files on every request. In those cases, the delay comes from somewhere else.

The safest default is to leave the cache alone. The cache usually manages itself: it runs automatically, removes old entries over time, and seldom requires manual action. Clear it only when a site behaves strangely and a normal reload does not help.

For site owners: making caching work well

Good caching depends on clear rules. Use long max-age values for files that rarely change, such as logos and versioned assets. Use short values or validation for HTML and API responses that change often. Keep sensitive responses out of shared caches with no-store or private directives.

Versioned file names help too. When a CSS or JavaScript file changes, changing its name lets the browser fetch the new file while old entries expire naturally. This avoids serving stale assets without forcing users to clear anything.

API responses deserve the same care. A REST endpoint that returns user-specific data should not be cached like a public image. For background on API design, see What is a REST API vs API? A Practical Guide.

Security and privacy notes

Caching can expose data if rules are wrong. Shared caches, such as proxies between the client and server, may store responses that were meant for one user. Directives such as private and no-store reduce that risk. Browser security features such as CORS also limit how scripts read responses from other origins; see What is CORS in IT? Browser security explained.

Private browsing reduces disk traces but does not make a user invisible. It keeps cache data in memory and discards it when the session ends. It does not hide activity from networks, servers, or accounts.

Questions people also ask

Does clearing the cache delete passwords?

No. Passwords, downloaded files, and custom settings are not stored in the cache, so they survive a cache clear. Only a cookie clear signs you out and resets preferences.

What is the difference between a cache hit and a cache miss?

A cache hit means the browser found the requested file locally and can reuse it. A cache miss means it was not found, so the browser downloads it from the server.

Why does a force reload bypass the cache?

A force reload sends Cache-Control: no-cache, which tells the browser to fetch a completely fresh copy from the server. A normal reload only asks the browser to check freshness.

Does incognito mode use the cache?

Yes, but it keeps that cache in memory and discards it when the session ends. Closing every incognito window ends the session.

Related pages

  • Client vs Server Rendering: Architecture and Performance

    Client-side rendering generates HTML directly inside the user browser via downloaded JavaScript, whereas server-side rendering compiles the full HTML document on the origin or edge server prior to transmission. This core difference shifts computational burdens between client hardware and cloud infrastructure, altering First Contentful Paint times, search indexing reliability, and hosting costs. Teams select between them based on content volatility, interaction requirements, and performance objectives.

  • How Do Web Cookies Work? A Practical Guide

    A web cookie is a small piece of data a server sends to a browser, which the browser stores and returns with later requests to the same site. Cookies let stateless HTTP remember logins, preferences, and activity, but they also raise privacy and security concerns.

  • What is WebSocket and how it works?

    WebSocket is a protocol for bidirectional, low-latency communication over a persistent connection between a client and a server. It begins with an HTTP handshake and then stays open so either side can send messages without creating a new request each time. Use it when an application needs frequent real-time updates; use ordinary HTTP when request-response traffic is enough.

  • What is a REST API vs API? A Practical Guide

    An API is a contract that lets software systems exchange requests and responses. A REST API is one kind of API that uses HTTP methods and resource-based URLs to manage data, usually with JSON. All REST APIs are APIs, but many APIs use other protocols or styles.

  • What is GraphQL API? A Practical Guide

    A GraphQL API is a query language and runtime for APIs that lets a client request only the fields it needs, including related data, in one operation. It is backed by a strongly typed schema that defines types, fields, and allowed operations. Clients control the shape of the response, while the server resolves data from its sources.

  • What is CORS in IT? Browser security explained

    Cross-Origin Resource Sharing (CORS) is a browser security mechanism that lets a server declare which other origins may read its responses. It extends the same-origin policy so trusted sites can use APIs and resources hosted elsewhere. Without the right headers, a script's cross-origin request fails in the browser.

How this page was prepared

Before writing, the topic was researched in the sources below. The page then passed automatic quality checks.

Report a mistake on this page