IIS (Internet Information Services) · Performance & Behaviour

Caching

Caching avoids doing the same work twice — in the browser, in IIS memory or in HTTP.sys itself.

Intermediate 5 min Lesson 22 of 33Covers IIS 10 on Windows Server 2019/2022/2025 and Windows 10/11.
Before you start:Compression
0/33 in IIS
On this page · 5 min · Intermediate

In simple words#

Client caching is keeping a copy of a book at home so you don’t visit the library again. Output caching is the librarian keeping popular answers ready at the desk. Kernel caching is the security guard handing over the most common leaflets directly at the gate.

clientCache
Cache-Control headers telling browsers how long to keep files.
Output caching
IIS stores generated responses in memory.
Cache busting
New file names (hashes) when content changes.

Learning objectives#

  • Set browser cache headers for static files
  • Configure output caching
  • Use cache busting safely
  • Avoid caching private data

Explanation#

staticContent/clientCache sets Cache-Control for static files. Use long max-age for fingerprinted files (app.3f2a9c.js).

Output caching (and kernel-mode caching) stores responses in memory, keyed by URL and optionally query string or headers.

Never cache personalised pages (dashboards, carts) in shared caches.

ASP.NET Core’s Output Caching middleware is often a better place for app-level caching rules.

Code example: Cache assets for a year, HTML never#

XMLweb.config Illustrative
<configuration>
  <location path="assets">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" cacheControlCustom="immutable" />
      </staticContent>
    </system.webServer>
  </location>


  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="DisableCache" />
    </staticContent>
    <caching enabled="true" enableKernelCache="true">
      <profiles>
        <add extension=".json" policy="CacheForTimePeriod" duration="00:05:00" varyByQueryString="*" />
      </profiles>
    </caching>
  </system.webServer>
</configuration>
Expected output
Files in /assets are cached by browsers for a year; HTML is revalidated each time; static .json files are cached in IIS memory for 5 minutes per query string.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. /assets holds fingerprinted files → long cache.
  2. Everything else defaults to no-cache.
  3. An output caching profile caches .json responses for 5 minutes.
  4. varyByQueryString keeps different queries separate.

Real-world use case#

A news site serves its hashed JS/CSS from browser cache and caches public JSON feeds for a few minutes, cutting server load sharply.

Common mistakes#

Long cache on non-fingerprinted files

Users keep old versions after a release. Only long-cache files whose names change.

Caching personalised content

Other users could see someone else’s data. Never cache private pages in shared caches.

Best practices#

  • Fingerprint asset file names
  • Long cache for assets, no-cache for HTML
  • Cache public data only
  • Measure with browser DevTools

Remember this#

Hashed files → cache forever.

HTML → no-cache.

Private data → never cache.

Practice exercise#

Your task: Set images in /images to cache for 7 days.

Show solution
XML Illustrative
<location path="images">
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>
  </system.webServer>
</location>

Mini quiz#

Caching quizMultiple choice Question 1 of 1

Which files are safe to cache for a year?

Score so far: 0/0

Want more? Practise all IIS MCQs · IIS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.