An HTTP cache is a mechanism for storing local copies of HTTP responses so subsequent requests can be fulfilled without re-contacting the origin server. By saving web resources such as stylesheets, scripts, images, and API payloads closer to the user, caching reduces network round trips, lowers bandwidth consumption, and cuts Time to First Byte (TTFB) across web applications.
How it works
HTTP caching is governed by response and request headers exchanged between the origin server, intermediary proxies, and the client. When a client requests a resource for the first time, the server returns the payload along with freshness metadata, primarily through the Cache-Control header (or the legacy Expires header).
Once stored, a cached entry stays in one of two states:
- Fresh: The response is within the lifetime defined by directives like
max-ageors-maxage. The cache serves the stored copy immediately without contacting the origin. - Stale: The freshness window has expired. Before discarding the entry, the cache sends a conditional request to the origin using validators such as
ETag(viaIf-None-Match) orLast-Modified(viaIf-Modified-Since). If the content has not changed, the origin returns a lightweight304 Not Modifiedresponse without a message body, refreshing the local copy.
Caches fall into two architectural categories:
- Private cache: Tied to a single client, such as a web browser's local disk or memory cache. It can safely store personalized responses marked with
Cache-Control: private. - Shared cache: Sits between the origin and multiple users, including Content Delivery Networks (CDNs), reverse proxies like Varnish or Nginx, and ISP proxies.
Combining client-side caching with backend caching strategies—similar to the patterns covered in IP Cache for Parsers: Hard Data on Performance & Cost—keeps infrastructure costs predictable under heavy traffic spikes.
Example
In an Nginx reverse proxy or web server configuration, you can separate caching rules for versioned static assets and dynamic HTML documents:
location /assets/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location / {
add_header Cache-Control "no-cache";
etag on;
}
The /assets/ block instructs both browsers and CDNs to reuse hashed files for one year without revalidation. Meanwhile, no-cache on root HTML routes forces caches to revalidate with the origin via ETag on every request, ensuring users always receive updated references to newly deployed assets.
What matters when choosing a server or hosting
When evaluating hosting providers, reverse proxies, or managed platforms, look at how easily you can control cache behavior at the edge and origin:
- Header preservation and override controls: Ensure the CDN or load balancer respects origin
Cache-Controlheaders by default while allowing custom edge rules vias-maxage. - Cache invalidation speed: Look for instant purge APIs (by URL, prefix, or surrogate key) so urgent content updates do not wait for TTL expiration.
- Cache key customization: Good edge platforms let you vary cache keys by query parameters, headers, or cookies without fragmenting hit ratios unnecessarily.
Common mistakes
- Caching personalized data in shared caches: Omitting
Cache-Control: privateorno-storeon authenticated endpoints can leak one user's session data to another through a CDN or proxy. - Confusing
no-cachewithno-store:no-cacheallows storing a response as long as it is revalidated before reuse;no-storeforbids writing the response to any cache storage at all. - Using long
max-ageon unversioned URLs: Setting a long TTL on files likeapp.jswithout content hashes in the filename prevents clients from picking up bug fixes until the timer expires.
FAQ
Which HTTP methods are cacheable by default?
Only safe, idempotent methods—primarily GET and HEAD—are cached by standard browsers and proxies. Unsafe methods like POST, PUT, and DELETE bypass the cache and typically invalidate stored entries for that target URI.
What is the difference between max-age and s-maxage?
max-age applies to both private browser caches and shared caches. s-maxage overrides max-age exclusively for shared caches like CDNs, letting you serve fresh content at the edge while keeping shorter TTLs in end-user browsers.
When should you use Vary headers?
Include a Vary header (such as Vary: Accept-Encoding) whenever the origin returns different representations for the same URL based on request headers, preventing a cache from serving compressed assets to incompatible clients.