When it come to optimization of a site or web-server, ETAGs and Expiry headers are the two main things you should be looking at. ETAGs and Expiry headers will be used to understand by the client whether to fetch the latest file or not, but still there is a difference between these two and are used at different scenarios.

Entity Tags (ETAGs):

An Etag is an HTTP header that was introduced in HTTP 1.1. EATGs are more like validators, they doesn’t have any information that the client can use to determine whether or not to make a request for that file again. ETAG is more like a checksum which changes when the contents of the file changes. When you request a file, the client’s browser will go to the server and will tell the ETAG of the file what it have currently in it’s local cache. Then the server will check the ETAG on it’s end and will see if it’s the same ETAG or a different one. If the ETAG is same, server will reply with a HTTP 304 (Not Modified) to the client, which simply means that client should use the local cache of the file. On the other hand, if the ETAG of the client and the server is different, then server will reply with HTTP 200 (OK) and will send the latest file to the client.

Expire Headers:

Expiry headers are used when you don’t even want client (and proxies/caches) to make a request to the server. In ETAG, the client will check with the server for the update, but in expiry headers, the client will know, when to expire the file and check for an update, till then it (browsers and proxies/caches) won’t bother server for checking the update.

FAQs:

Q. Can I use both Expiry headers and ETAGs at the same time, and will it be of any use or harm ?

A. Yes, you can use both of them at the same time. So when you want to do is to use BOTH headers – set the Expires header to a reasonable value based on how often the content changes. Then configure ETags to be sent so that when clients do send a request to the server, it can more easily determine whether or not to update the file. Configuring both at the same time will always be useful.

Q. How ETAG is generated ?

A. By default, Apache will generate an Etag based on the file’s inode number, last-modified date, and size. More information about the ETAGs could be find in my previous blog.

Q. Why can’t I use the ETAGs on load-balanced multiple servers.

A. If you are using a load-balanced server setup with multiple machines running Apache you should turn off ETag generation. This is because inodes are used as part of the ETag hash algorithm which will be different between the servers. You can configure Apache to not use inodes as part of the calculation but then you’d want to make sure the timestamps on the files are exactly the same, to ensure the same ETag gets generated for all servers. So, I would suggest not to use the ETAGs in this setup, to save the trouble.

Q. Should I use ETAGs for all of my content ?

A. For static content it’s recommended to send Cache-Control: max-age=… header with higher max-age value. In this case browser won’t send any request on normal page views. So for static content, there is no use of ETag header.

Q. Where I would like to use ETAGs and not expiry headers ?

A. If we cannot predict lifetime of page content then it’s better to use the ETAGs so that you don’t need to go back to server every time and fetch the data.

References:

  1. HTTP Status Codes
  2. Stack Overflow
  3. Xpertdeveloper
If you enjoyed this post, make sure you subscribe to my RSS feed!!!!