Error Codes Wiki

HTTP ETag and Conditional Requests — If-None-Match and 304 Not Modified

Informational3xx redirection

Overview

Master HTTP conditional requests using ETag, If-None-Match, If-Match, and 304 Not Modified responses for efficient caching and optimistic concurrency control.

Key Details

  • ETag (Entity Tag) is a unique identifier for a specific version of a resource
  • If-None-Match: sends the cached ETag — server returns 304 if unchanged, 200 with new data if changed
  • If-Match: used for write operations — server rejects the update with 412 if the ETag changed (optimistic locking)
  • Strong ETags ("abc123") indicate byte-for-byte identical content; weak ETags (W/"abc123") indicate semantically equivalent
  • ETags reduce bandwidth by avoiding redundant full-content responses

Common Causes

  • Server not generating ETags, forcing full content download on every request
  • ETag mismatch during updates causing 412 Precondition Failed (concurrent edit detected)
  • Load balancer routing requests to different servers with different ETag generation
  • CDN caching stale ETags after content updates on the origin server

Steps

  1. 1Enable ETag generation on your server: Nginx and Apache generate ETags by default for static files
  2. 2For APIs: generate ETags from a hash of the response body or the resource's last-modified timestamp
  3. 3Include If-None-Match in GET requests to enable 304 responses and reduce bandwidth
  4. 4Use If-Match in PUT/PATCH requests to prevent lost updates from concurrent editing
  5. 5Ensure all servers behind a load balancer generate identical ETags for the same content
  6. 6After a 412 Precondition Failed: re-fetch the current version, merge changes, and retry the update

Tags

etagconditional-request304if-none-matchcache-validation

Related Items

More in 3xx Redirection

Frequently Asked Questions

ETag is content-based (hash of the content), Last-Modified is time-based. ETag is more precise — it detects changes even if the modification time is the same. Use both for maximum compatibility.