What is HTTP ETag?

ETag :

An entity tag (ETag) is an HTTP header used for Web cache validation and conditional request from browsers to resources. The value of an ETag is an identifier that represents a specific version of the resource. Additionally, ETags help prevents simultaneous updates of a resource from overwriting each other. Example of ETag header is

Working of ETag

  • The server receives an HTTP request for a particular resource.
  • The server generates a response and attached an ETag header. For Eg: ETag: “response_version1”.
  • The server sends the response with the above header with the status code 200. Then the application represents the resource and at the same time caches the resource copy along with header information.
  • Later the same application makes another request for the same resource with following conditional request header: If-None-Match: “response_version1”
  • On receiving the request for the resource along with the ‘If-None-Match’ header, the server-side logic compare the current value of the ETag identifier on the server-side and the one which is received in the request header.
  • If the request’s If-None-Match is the same as the currently generated value of ETag on the server, then status code 304 (Not Modified) with the empty body is sent back and the application uses the cached copy of the resource.
  • If the request’s If-None-Match value doesn’t match the currently generated/assigned value of ETag (say “response_version2”) for the same resource then the server sends back the new content in the body along with status code 200. The ‘ETag’ header with the new value is also included in the response. The application uses the new resource and updates its cache with the new data.