Entity Tags(Etags) in asp.net - ETag vs Header Expires
Introduction
Entity Tags (ETags) are commonly used in Web applications to effectively leverage the power of using web farms, which is a non-fancy term for HTTP/S load balancing. In web farms, a common practice is to set what is called ETags as it helps enhance performance in web farm scenarios. ETags is controlled in IIS by a metabase property (ETAG_CHANGENUMBER) and this value is sent back to clients via response headers as instructed by the HTTP RFC 2616.
If the ETag value is the same across a number of servers, then clients are not required to re-download content that already exists in the clients cache. Without this value set to the same, clients might communicate on subsequent requests to another server in the web farm and receive an unnecessary response of data from the IIS server.
For purposes of this blog, I will focus on modifications rather than further definition of ETags. However, you can read more in the IIS 6.0 documentation on ETags to get more familiar with them.
I've looked around but haven't been able to figure out if I should use both an ETag and an Expires Header or one or the other.
What I'm trying to do is make sure that my flash files (and other images and what not only get updated when there is a change to those files.
I don't want to do anything special like changing the filename or putting some weird chars on the end of the url to make it not get cached.
Etag and Last-modified headers are validators.
They help the browser understand if a file/page, has changed, even if it preserves the same name.
Expires and Cache-control are giving refresh information.
This means that they inform, the browser and the reverse in-between proxies, up to what time or for how long, they may keep the page/file at their cache.
So the question usually is which one validator to use, etag or last-modified, and which refresh infomation header to use, expires or cache-control.
They help the browser understand if a file/page, has changed, even if it preserves the same name.
Expires and Cache-control are giving refresh information.
This means that they inform, the browser and the reverse in-between proxies, up to what time or for how long, they may keep the page/file at their cache.
So the question usually is which one validator to use, etag or last-modified, and which refresh infomation header to use, expires or cache-control.
They are slightly different - the ETag does
not have any information that the client can use to determine whether or
not to make a request for that file again in the future. If ETag is
all it has, it will always have to make a request. However, when the
server reads the ETag from the client request, it can then determine
whether to send the file (HTTP 200) or tell the client to just use their
local copy (HTTP 304). An ETag is basically just a checksum for a file
that semantically changes when the content of the file changes.
The Expires header is used by the client (and proxies/caches) to determine whether or not it even needs to make a request to the server at all. The closer you are to the Expires date, the more likely it is the client (or proxy) will make an HTTP request for that file from the server.
So really what you want to do is 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 send the file back.
One last note about ETag - if you are using a load-balanced server setup with multiple machines running Apache you will probably want to 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.
Related Posts
http://developer.yahoo.com/performance/rules.html#etags
http://www.iis.net/configreference/system.webserver/staticcontent/clientcache
http://kitsula.com/Article/Configure-entity-tags-ETags-on-IIS7
The Expires header is used by the client (and proxies/caches) to determine whether or not it even needs to make a request to the server at all. The closer you are to the Expires date, the more likely it is the client (or proxy) will make an HTTP request for that file from the server.
So really what you want to do is 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 send the file back.
One last note about ETag - if you are using a load-balanced server setup with multiple machines running Apache you will probably want to 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.
Related Posts
http://developer.yahoo.com/performance/rules.html#etags
http://www.iis.net/configreference/system.webserver/staticcontent/clientcache
http://kitsula.com/Article/Configure-entity-tags-ETags-on-IIS7
Comments
Post a Comment