Negative Caching: Why and How to Cache Error Responses

· Mei-Lin Wu · 9 min read

Negative caching is the practice of storing error responses at the CDN edge so that repeated requests for missing or failed resources are served from cache rather than forwarded to the origin. While most caching discussions focus on successful responses, error responses can generate enormous origin load if left uncached. A single broken link shared on social media can produce millions of 404 requests that bypass the cache and hammer your origin, creating the same effect as a denial-of-service attack. Understanding which errors to cache, for how long, and when caching errors becomes dangerous is a critical part of any comprehensive caching strategy.

What Is Negative Caching and Why Does It Matter?

In caching terminology, a positive cache stores successful responses, while a negative cache stores error or empty responses. The term originates from DNS, where negative caching stores NXDOMAIN responses to prevent repeated lookups for nonexistent domains. In CDN contexts, negative caching applies the same principle to HTTP error responses.

CDN Cache Hierarchy — Multi-tier caching architecture from edge to originCDN Cache Hierarchy — Multi-tier caching architecture from edge to origin CDN Cache Hierarchy — Multi-Tier Architecture L1 Edge Cache Hit Rate: 95% Latency: < 5ms miss L2 Regional Cache Hit Rate: 85% Latency: 10–30ms miss Origin Shield Hit Rate: 70% Latency: 50–80ms miss Origin Server Hit Rate: — Latency: 100–300ms
Figure 1: CDN Cache Hierarchy — Multi-tier caching architecture from edge to origin

Without negative caching, every request for a missing resource passes through the CDN to the origin. For high-traffic sites, this creates several problems:

Which Error Codes Should Be Cached?

The HTTP specification in RFC 9110 defines which response codes are cacheable by default. Not all errors should be cached, and the distinction matters for both correctness and reliability.

Performance Comparison: Edge vs Origin Processing
Metric Edge Processing Origin Processing Improvement
Latency (p50)8–15 ms120–300 ms15–20x faster
Cache Hit Ratio85–95%N/AReduces origin load
TLS Handshake1–5 ms (nearby PoP)50–150 ms10–30x faster
Bandwidth Cost$0.01–0.04/GB$0.05–0.12/GB3–5x cheaper
Availability99.99% (multi-PoP)99.9% (single DC)10x fewer outages

Cacheable Error Codes

Status codeMeaningDefault cacheableRecommended TTLRationale
301Moved PermanentlyYes1 hour to 24 hoursPermanent redirect unlikely to change soon
308Permanent RedirectYes1 hour to 24 hoursSame as 301 but preserves HTTP method
404Not FoundYes1 to 10 minutesResource may be created or restored
410GoneYes1 to 24 hoursExplicitly signals permanent removal
302FoundWith Cache-Control30 seconds to 5 minutesTemporary, but cacheable if origin permits
307Temporary RedirectWith Cache-Control30 seconds to 5 minutesSame as 302 but preserves HTTP method

Error Codes You Should Not Cache

Server-side errors represent transient conditions and should almost never be cached:

The one exception is micro-caching server errors for one to five seconds during an active cache stampede. This prevents hundreds of edge nodes from simultaneously retrying against an already-overwhelmed origin, giving it breathing room to recover.

How Do You Configure Negative Caching TTLs?

The right TTL for a negative cache entry depends on the error type and your application's behavior. Setting it too short negates the protection benefit. Setting it too long serves stale errors after the resource becomes available.

TTL Guidelines by Error Type

404 Not Found: Use 60 to 600 seconds. For applications with frequent deployments where new URLs appear regularly, stay at the shorter end. For static content sites where URLs rarely change, the longer end is appropriate. Always set shorter negative TTLs than your positive cache TTLs to ensure that newly created resources become visible quickly.

410 Gone: Use 3,600 to 86,400 seconds. A 410 explicitly tells clients the resource has been permanently removed. A longer TTL is appropriate because the origin has deliberately signaled that this URL will not return content. This also helps search engines deindex the URL faster.

301 and 308 Permanent Redirects: Use 3,600 to 86,400 seconds. These are permanent by definition. Caching them aggressively reduces redirect-chain latency and origin load. However, leave enough room to correct a misconfigured redirect without waiting hours for the cache to expire.

Configure negative caching through Cache-Control headers on your origin error responses:

# Nginx: Cache 404 responses for 5 minutes
error_page 404 /404.html;
location = /404.html {
    internal;
    add_header Cache-Control "public, max-age=300, s-maxage=300";
}

# Nginx: Cache 410 responses for 1 hour
error_page 410 /410.html;
location = /410.html {
    internal;
    add_header Cache-Control "public, max-age=3600, s-maxage=3600";
}

How Does Negative Caching Prevent Error Amplification?

Error amplification occurs when a single origin failure produces cascading errors across the CDN. Without negative caching, the sequence is destructive:

Cache Decision Flow — How the CDN determines cache hit vs missCache Decision Flow — How the CDN determines cache hit vs miss CDN Request Flow — From Browser to Edge CDN Request Lifecycle Step 1 DNS Lookup Step 2 TCP Connect Step 3 TLS Handshake Step 4 Edge Server Step 5 Cache Check Step 6 Response Cache HIT → 5ms response Cache MISS → Fetch origin 0ms ~50ms (cache hit) ~200ms (cache miss)
Figure 2: Cache Decision Flow — How the CDN determines cache hit vs miss
  1. Origin returns a 404 for /product/discontinued-item.
  2. The CDN does not cache the error and discards the response.
  3. The next request for the same URL bypasses cache and hits the origin again.
  4. At 1,000 requests per second, the origin processes 1,000 identical lookups every second, each returning the same 404.
  5. This load consumes database queries, application logic, and network bandwidth that should serve valid requests.

With negative caching enabled, step 2 changes: the CDN caches the 404 response. Steps 3 through 5 do not occur because subsequent requests are served from the edge cache. The origin processes one lookup instead of thousands.

This protection becomes especially valuable during link rot events. When an old blog post linking to your site spreads through social media, thousands of users may click a URL that no longer exists. Without negative caching, each click becomes an origin request. With a 5-minute negative cache TTL, only one request per 5 minutes per edge PoP reaches the origin.

How Do Custom Error Pages Interact with Negative Caching?

Many sites serve custom-branded error pages with navigation, search boxes, and suggested content. These pages can be significantly larger than a simple error text response, making negative caching even more important for bandwidth savings.

When implementing custom error pages with negative caching, consider these factors:

How Does Negative Caching Interact with Health Checks?

CDN health checks and negative caching operate on related but separate timelines. Health checks determine whether an origin is available for new requests. Negative caching determines how long an error response is served from edge cache. Misalignment between these systems creates confusing behavior.

Scenario 1: Origin recovers, cached errors persist. The origin was returning 500 errors. It recovers and health checks pass. But the CDN is still serving cached 500 responses until their TTL expires. Users see errors despite a healthy origin. This is why caching 5xx errors is generally discouraged.

Scenario 2: Origin fails, no cached errors. Without negative caching, each edge node independently discovers the origin failure on every request. The origin, already struggling, receives maximum traffic at its worst moment. Health checks eventually mark it down, but the damage window can be significant.

The solution is to coordinate TTLs with health check intervals. If health checks run every 30 seconds, consider negative cache TTLs that are multiples of 30 seconds. This way, the cached error expires around the same time the next health check would run, and fresh requests coincide with updated health status.

What Are Provider Default Behaviors?

CDN providers handle negative caching differently by default, and understanding these defaults prevents surprises:

Provider404 default5xx defaultOverride method
CloudflareNot cached by defaultNot cachedCache Rules or Page Rules
CloudFrontCached for 5 minutesCached for 10 secondsError Response settings in distribution config
FastlyFollows origin headersNot cachedVCL custom logic or Compute
AkamaiFollows origin headersNot cached by defaultProperty Manager behaviors

CloudFront is notable for its built-in negative caching with configurable TTLs per error code. You can set different cache durations for 400, 403, 404, 405, 414, 500, 501, 502, 503, and 504 responses. Cloudflare requires explicit opt-in through Cache Rules or origin Cache-Control headers.

When Should You Avoid Negative Caching Entirely?

Negative caching is not universally beneficial. Several scenarios require careful consideration:

Negative caching is a simple but powerful technique that belongs in every CDN configuration. By caching error responses appropriately, you protect your origin from unnecessary load, improve response times for error pages, and prevent cascading failures during outage events. The key is choosing the right TTL for each error type and coordinating with cache hierarchy settings and health check systems to ensure cached errors expire at the right time. Combined with cache hit ratio optimization, proper negative caching rounds out a complete caching posture that handles both successful and failed responses efficiently.

External References

References & Standards: OWASP CDN Security · HTTP/2 specification