Caching
Caching avoids doing the same work twice — in the browser, in IIS memory or in HTTP.sys itself.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
Interview questions
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#
<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>
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#
- /assets holds fingerprinted files → long cache.
- Everything else defaults to no-cache.
- An output caching profile caches .json responses for 5 minutes.
- 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#
Users keep old versions after a release. Only long-cache files whose names change.
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
<location path="images">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
</system.webServer>
</location>
Mini quiz#
Which files are safe to cache for a year?
Want more? Practise all IIS MCQs · IIS interview questions
My notes#
Saved in this browser.