Cache is a stack of layers, not one mechanism
HTTP caching distinguishes private and shared caches. A browser cache is the common private-cache example, while a CDN or proxy is a common shared-cache example. [1][3]
Redis works at another layer. It does not automatically cache HTTP responses; the application chooses which values to store, such as database results, user profiles or computed aggregates. [9]
These layers can operate at the same time, so a caching policy should define browser TTL, CDN TTL and application-cache TTL separately.
Fresh, stale and revalidation in HTTP
RFC 9111 defines a response as fresh while its age does not exceed its freshness lifetime. Once that lifetime is exceeded, the response is stale. [1]
A fresh response can be reused without contacting the origin. A stale response normally requires revalidation or another explicit permission to use it. [1]
Freshness can be defined with Cache-Control: max-age or Expires. When an explicit freshness lifetime exists, caches should not replace it with heuristic freshness. [1]
no-cache, no-store and private mean different things
| Directive | May be stored | May be reused without origin | Typical use |
|---|---|---|---|
| no-cache | Yes | No, requires revalidation | HTML and APIs that must stay current |
| no-store | No | Not applicable | Data that must not be stored |
| private | Yes, private caches only | Depends on freshness | Personalized responses |
| public | Yes, including shared caches | Depends on freshness | Public content |
Cache-Control: no-cache allows a response to be stored but requires validation with the origin before reuse. [3][4]
Cache-Control: no-store tells caches not to store the response. private allows storage in a private cache but not in a shared cache. [4]
For personalized responses after sign-in, private is especially important because it prevents a shared cache from reusing the response for another user. [4]
ETag, Last-Modified and 304 Not Modified
HTTP supports representation validators. An ETag can later be sent in If-None-Match, while Last-Modified can be used with If-Modified-Since. [1]
If the representation has not changed, the origin can return 304 Not Modified, avoiding retransmission of the complete response body. [1][3]
For HTML or APIs that should always be current, no-cache together with ETag or Last-Modified is often a better trade-off than disabling storage entirely with no-store. [3]
immutable and versioned static assets
Static files whose URLs contain a content hash or version can use a long max-age because a content change creates a new URL. MDN recommends this cache-busting pattern for assets that do not change under the same URL. [3]
The immutable directive tells caches that the representation will not change while it is fresh, reducing unnecessary revalidation on reload. [3][4]
This pattern fits JavaScript bundles, CSS, fonts and generated images particularly well.
stale-while-revalidate and stale-if-error
RFC 5861 defines stale-while-revalidate, allowing a cache to serve a stale response for a specified interval while it attempts revalidation in the background. [2]
stale-if-error allows a stale response to be used when the origin fails or is temporarily unavailable, within the configured staleness window. [2]
These mechanisms can improve latency and availability, but they intentionally increase the time during which a user may see stale data.
A CDN cache key is as important as TTL
A cache key determines which requests map to the same stored response. Cloudflare's default key includes elements such as scheme, host, URI with query string and selected headers. [6]
An overly broad key definition can fragment the cache and reduce hit rate. An overly narrow key can merge requests that need different responses.
If a response varies on a header named by Vary, RFC 9111 requires that difference to be considered when selecting a stored response. [1]
A CDN follows origin policy only within its edge configuration
Cache-Control sent by the origin can control CDN behavior. Cloudflare describes Origin Cache Control as the mechanism for respecting cache directives from the origin. [5]
CDN rules can still override or augment origin policy, so effective caching semantics can differ from the response header emitted by the application. [5]
Debugging therefore requires checking application headers, edge rules and the actual cache status observed at the CDN.
Purge and invalidation are not the same CDN operation
| CDN operation | What happens | Next request | When to use |
|---|---|---|---|
| Purge | Object is removed | Full fetch from origin | Stale content must never be served again |
| Invalidate / soft purge | Object stays but is marked stale | Revalidation with origin | You want to reuse 304 or stale policies |
Cloudflare purge removes cached content. The next request becomes a miss and fetches a full response from the origin. [7]
Invalidation, also called soft purge, keeps the object but marks it stale. The next request revalidates with the origin and can reuse the object if the origin returns 304 Not Modified. [8]
Purge is appropriate when stale content must not be served again. Invalidation is useful when revalidation is acceptable and may avoid downloading the full body. [8]
Purging by tag is more precise than purge everything
Cloudflare lets responses carry Cache-Tag values and later purge all cached objects associated with a tag. [7]
That makes it possible to connect multiple URLs to one domain entity, such as a product affecting its detail page, category page and home page.
After a purge, the next request must rebuild the cache, so broad purges can temporarily increase load on the origin. [7]
Redis cache-aside is a common application pattern
With cache-aside, the application first tries Redis. On a miss it reads the source of truth, such as the primary database, and stores the result in cache. [9]
On update, the application writes to the primary store and invalidates the relevant cache key. Redis documents this as a way to bound staleness with TTL plus explicit invalidation. [9]
Cache-aside stores only data that is actually requested, but the first request after a cold miss still pays the source-read cost.
Redis TTL and eviction solve different problems
| Redis mechanism | Purpose | Example |
|---|---|---|
| TTL | Limit entry lifetime | SET key value EX 300 |
| Eviction | Control memory when maxmemory is reached | allkeys-lru, allkeys-lfu |
| Explicit invalidation | Remove immediately after a data change | DEL cache:product:123 |
TTL defines when a specific key expires. Redis SET supports EX, PX, EXAT and PXAT, so expiration can be attached when the value is written. [11]
Eviction defines what Redis does when it reaches the configured memory limit. Available policies include LRU, LFU, random and variants limited to keys with TTL. [10]
TTL controls time-based freshness of an entry, while eviction manages memory pressure. They are not substitutes.
Cache stampede and hot keys
Redis describes a cache stampede as the situation where a popular key expires and many concurrent application instances query the primary database at the same time to rebuild it. [9]
Mitigations include a mutex or lock during regeneration, probabilistic early refresh, stale-while-revalidate at an appropriate layer, or adding jitter to expiration times.
The goal is to prevent hundreds of concurrent requests from performing the same expensive rebuild after one popular key expires.
Event-driven invalidation needs a reliable channel
Redis keyspace notifications can report key changes, but they are implemented with Pub/Sub. Redis documents Pub/Sub as fire-and-forget, so a subscriber that is disconnected misses events published during the outage. [12]
Keyspace notifications therefore should not be the only mechanism for business-critical cross-service invalidation.
If invalidation must be guaranteed, use a durable event channel or another mechanism with retry and replay semantics.
How to design caching from the beginning
Start by classifying data into immutable assets, public content, personalized content, frequently changing API data and expensive computed data. Each category can have a different policy.
Separate browser cache, CDN cache and Redis cache. For each layer, document the cache key, TTL, source of truth, invalidation trigger and error behavior.
Design correctness and the maximum acceptable staleness window first, then optimize hit rate.
- Use long TTL plus
immutablefor versioned assets. - For current HTML, consider
no-cacheplus ETag or Last-Modified rather than automatically usingno-store. - Use
privatefor personalized responses that must not enter shared caches. - Design cache keys together with language, query parameters and other representation variants.
- Prefer granular CDN purge or invalidation by URL, tag, prefix or host.
- Set Redis TTL deliberately and choose an eviction policy that matches the access pattern.
- Protect popular keys against cache stampede.
- Monitor hit ratio, miss ratio, evictions, origin latency and invalidation volume.
