Dung (Donny) Nguyen

Senior Software Engineer

ETag Header

The ETag header is a mechanism used in HTTP to efficiently cache responses and avoid unnecessary data transfers. It works by generating a unique identifier, or tag, for a resource, and then sending this tag along with the response to the client.

Here’s how it works:

  1. Resource Modification: When a resource on the server is modified, the server generates a new ETag for that resource.
  2. ETag in Response: The server includes the ETag in the HTTP response header for the resource.
  3. Client Caching: The client stores the ETag along with the cached resource.
  4. Subsequent Requests: When the client makes a subsequent request for the same resource, it includes the previously stored ETag in the If-None-Match header of the request.
  5. Server Comparison: The server compares the ETag in the If-None-Match header with the ETag of the current resource.
  6. Conditional Response:
    • If the ETags match, the server responds with a 304 Not Modified status code, indicating that the resource has not been modified since the last request. The client can use its cached copy without downloading the entire resource again.
    • If the ETags do not match, the server sends the updated resource along with a new ETag. The client caches the new resource and ETag.

Benefits of ETag:

When to use:

ETags are generally preferred over Last-Modified headers because they provide a more accurate and reliable way to determine if a resource has changed. They can handle scenarios where the content of a resource has changed but the modification timestamp remains the same.

Key points to remember: