Web Proxy / Caching Fundamentals Series

🗄️ Understanding Cache-Control and the Vary Header From a Top 1% Perspective — Why the Same URL's Cache Gets Treated as Multiple 'Different' Things

Introduction

This article is part of the Top 1% Series Complete Article Guide, and the sixth article in the Web Proxy/Caching Fundamentals Series.

Prerequisite Knowledge

Getting the Big Picture

The mechanism controlling caching splits broadly into two roles: Cache-Control, instructing “how long is this response allowed to be cached,” and Vary, instructing “under which differing conditions should this same URL be treated as separate caches.”

graph TB
    Response["The Server's Response"]
    CC["Cache-Control Header<br/>(cache duration, allowed or not)"]
    Vary["Vary Header<br/>(the condition that splits the cache)"]
    Response --> CC
    Response --> Vary

Deep Dive Into the Fundamentals

Cache-Control: the Main Directives in Common Use

DirectiveMeaning
max-age=<seconds>How long, in seconds, the response is considered “fresh.”
no-cachePermits storing it in the cache, but forces re-validation (confirming it’s still valid) with the server before it’s ever used.
no-storeEntirely forbids storing it in a cache at all.
privatePermits storage only in a personal cache, like a browser’s; forbids storage in a shared cache, like a CDN’s.
publicExplicitly permits storage in a shared cache too.
If It's Called "no-cache," Why Does It Still Get Stored in a Cache?

no-cache is the most commonly misunderstood directive in real-world practice. The name suggests “never cache this,” but it actually instructs “store this in the cache, but always confirm with the server before ever using it.” A response marked no-cache genuinely does get stored in a cache, but the next time the same request arrives, the server always gets asked “is this cache still valid?” (re-validation, typically via an ETag) before the stored content is ever returned as-is. If what you actually intend is “never store this in a cache at all,” you need no-store instead. Mixing up these two directives leads to real-world confusion like “I never meant to cache this, but old content briefly flashed on screen.”

Age: a Header Communicating “How Old” a Response Actually Is

When a caching server (a CDN, say) returns a response it holds in its own cache, it communicates, via the Age header, exactly how many seconds have elapsed since that response was originally fetched from the origin server. The client can compare this Age value against max-age to judge “is this response still within its valid period?”

Vary: Managing the Same URL as Multiple Separate Cache Entities

The directives covered so far control “whether to cache, and for how long.” What the Vary header addresses is an entirely different axis. Even for the same URL (say, /products), the content that should be returned can differ based on a request header’s content.

Vary: Accept-Language, User-Agent

When a caching server stores a response carrying this Vary header, it uses, as the key distinguishing a cache entity, not just “the URL,” but “the URL plus the combination of values from the specified request headers.” This lets the same /products URL correctly use and distinguish separate caches — serving the Japanese-language response for a request carrying Accept-Language: ja, and the English-language response for one carrying Accept-Language: en.

What Happens If You Forget to Specify Vary?

If the server’s implementation returns a different response per language, but you forget to specify Accept-Language in the Vary header, the caching server mistakenly concludes “the URL is the same, so the same response is fine.” The result is a very common real-world failure: the response matching whichever user happened to access it first, based on their language setting, gets stored in the cache, and then gets incorrectly served to every subsequent user, even ones with an entirely different language setting. The Difference Between GraphQL and RESTful APIs covered how a GraphQL response doesn’t lend itself to simple URL-based caching — the exact scenario where Vary becomes necessary shares the same underlying structure: a single axis, the URL, alone can’t correctly distinguish a response’s real identity.

What a Pro Sees Here (Top 1% Understanding)

A Caching Bug Is Always One of Two Things: “Stored Too Long” or “Not Distinguished Enough”

A real-world caching incident almost always falls into one of two categories: “it’s being cached unintentionally too long” (a Cache-Control misconfiguration), or “responses that should genuinely be separate are being treated as the same cache” (insufficient Vary configuration). Being able to isolate these as two separate axes is the single most important lens for troubleshooting a caching problem. When you get a report of “the cache is stale,” deciding which of these two it actually is immediately narrows down where to investigate.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. Content you updated keeps showing its old version: Check whether max-age is set too long, or whether no-cache is being used in a situation that actually calls for no-store.
  2. A user with a different language setting sees the page in the wrong language: Check whether the implementation varies its response based on Accept-Language, but forgot to specify Vary: Accept-Language.
  3. A mobile user ends up seeing the PC-version response via the cache: For an implementation that varies its response by device, check the Vary: User-Agent (or equivalent header) setting.

Summary

Takeaways to Apply Today

  1. When you run into a caching bug, first isolate whether it’s “a duration problem” or “a distinction problem.”
  2. When implementing a response that varies based on a request header, never forget to specify the Vary header.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).