Compare Revisions
HTTP caching
Revision 1107781:
Revision 1107781 by teoli on
Revision 1108035:
Revision 1108035 by anthony-geoghegan on
- Title:
- HTTP caching
- HTTP caching
- Slug:
- Web/HTTP/Caching
- Web/HTTP/Caching
- Tags:
- "Caching" "Guide" "HTTP"
- "Caching" "Guide" "HTTP"
- Comment:
- clarify content and grammar to make it clearer and more readable
- Content:
-
Revision 1107781 Revision 1108035 n 11 The performance of web sites and applications can be signif n 11 The performance of web sites and applications can be signif > cantly improved by reusing previously fetched resources. Web cach > icantly improved by reusing previously fetched resources. Web cac > es reduce latency and network traffic and thus lessen the time ne > hes reduce latency and network traffic and thus lessen the time n > eded to display a representation of a resource. By making use of > eeded to display a representation of a resource. By making use of > HTTP caching, Web sites become more responsive. > HTTP caching, Web sites become more responsive. n 17 A cache is a tool that stores versions of a given document n 17 Caching is a technique that stores a copy of a given resour > and serve it back when requested. When a cache have a requested r > ce and serves it back when requested. When a web cache has a requ > esource, it intercepts the requests and achieve several goals: it > ested resource in its store, it intercepts the request and return > eases the load of the server that doesn't need to serve all clie > s its copy instead of re-downloading from the originating server. > nts itself, and it improves performances by being closer to the c > This achieves several goals: it eases the load of the server tha > lient: it takes less time to transmit the resource back. For a We > t doesn’t need to serve all clients itself, and it improves perfo > b site, it is a major component in achieving high performance. On > rmance by being closer to the client, i.e., it takes less time to > the other side, it has to be configured properly as not all reso > transmit the resource back. For a web site, it is a major compon > urces stay identical forever: it is important to cache a resource > ent in achieving high performance. On the other side, it has to b > only until it changes, not longer. > e configured properly as not all resources stay identical forever > : it is important to cache a resource only until it changes, not > longer. 18 </p> 19 <p> 18 </p> 19 <p> 20 There several kinds of caches that can be grouped in two ma 20 There several kinds of caches: these can be grouped into tw > in categories: private or shared caches. A shared cache is a cach > o main categories: private or shared caches. A <em>shared cache</ > e that stores responses to be reused by more than one user and a > em> is a cache that stores responses for reuse by more than one u > private cache is only dedicated to a single user. This page will > ser. A <em>private cache</em> is dedicated to a single user. This > mostly talk about browser and proxy caches, but there are also ga > page will mostly talk about browser and proxy caches, but there > teway caches, CDN, reverse proxy caches and load balancers that a > are also gateway caches, CDN, reverse proxy caches and load balan > re deployed on web servers for better reliability, performance an > cers that are deployed on web servers for better reliability, per > d scaling of web sites and web applications. > formance and scaling of web sites and web applications. n 29 A private cache is dedicated to a single user. You might ha n 29 A private cache is dedicated to a single user. You might ha > ve seen "caching" in your browser's settings already. A browser c > ve seen "caching" in your browser's settings already. A browser c > ache holds all documents downloaded via <a href="/en-US/docs/Web/ > ache holds all documents downloaded via <a href="/en-US/docs/Web/ > HTTP" title="en/HTTP">HTTP</a> by the user. This cache is used to > HTTP" title="en/HTTP">HTTP</a> by the user. This cache is used to > make visited documents available for back/forward, saving, viewi > make visited documents available for back/forward navigation, sa > ng-as-source, etc. without requiring an additional trip to the se > ving, viewing-as-source, etc. without requiring an additional tri > rver. It likewise improves offline browsing of cached content. > p to the server. It likewise improves offline browsing of cached > content. n 35 A shared cache is a cache that stores responses to be reuse n 35 A shared cache is a cache that stores responses to be reuse > d by more than one user. For example, an ISP or your company migh > d by more than one user. For example, an ISP or your company migh > t have set up a proxy to serve thousands of users and popular res > t have set up a web proxy as part of its local network infrastruc > ources are reused a number of times to reduce network traffic and > ture to serve many users so that popular resources are reused a n > latency. > umber of times, reducing network traffic and latency. n 41 HTTP caching is an optional operation, but reusing a cached n 41 HTTP caching is optional, but reusing a cached resource is > response is desirable often. However, common HTTP caches are typ > usually desirable. However, common HTTP caches are typically limi > ically limited to caching responses to {{HTTPMethod("GET")}} and > ted to caching responses to {{HTTPMethod("GET")}} and may decline > may decline other methods. The primary cache key consists of the > other methods. The primary cache key consists of the request met > request method and target URI (oftentimes only the URI is used as > hod and target URI (oftentimes only the URI is used as only GET r > only GET requests are caching targets). Common forms of caching > equests are caching targets). Common forms of caching entries are > entries are: > : n 65 The {{HTTPHeader("Cache-Control")}} HTTP/1.1 general-header n 65 The {{HTTPHeader("Cache-Control")}} HTTP/1.1 general-header > field is used to specify directives for caching mechanisms in bo > field is used to specify directives for caching mechanisms in bo > th, requests and responses. Use this header to define your cachin > th requests and responses. Use this header to define your caching > g policies with the variety of directives it provides. > policies with the variety of directives it provides. n 127 Once a resource is stored in a cache, it can theoretically n 127 Once a resource is stored in a cache, it could theoreticall > be served by the cache forever. Caches have finite storage though > y be served by the cache forever. Caches have finite storage so i > periodically remove items they stored. This process is called <e > tems are periodically removed from storage. This process is calle > m>cache eviction</em>. On the other side, some resources may chan > d <em>cache eviction</em>. On the other side, some resources may > ges in the server, and the cache should be updated. As HTTP is a > change on the server so the cache should be updated. As HTTP is a > client-server protocol, servers can't contact caches and clients > client-server protocol, servers can't contact caches and clients > when a resource change; they have to communicate an expiration ti > when a resource change; they have to communicate an expiration t > me for the resource. Before this expiration time, the resource is > ime for the resource. Before this expiration time, the resource i > <em>fresh</em>, after its expiration time, the resource if <em>s > s <em>fresh</em>; after its expiration time, the resource if <em> > tale</em>. Eviction algorithm often privileges fresh resources ov > stale</em>. Eviction algorithms often privileges fresh resources > er stale resources. Note that a stale resource is not evicted or > over stale resources. Note that a stale resource is not evicted o > ignored; when the cache receives a request for a stale resource, > r ignored; when the cache receives a request for a stale resource > it forwards this requests with a {{HTTPHeader("If-None-Match")}} > , it forwards this requests with a {{HTTPHeader("If-None-Match")} > to check if it isn't in fact still fresh. If so, the server retur > } to check if it isn't in fact still fresh. If so, the server ret > ns a {{HTTPStatus("304")}} (Not Modified) message and don't send > urns a {{HTTPStatus("304")}} (Not Modified) header without sendin > the resource back, saving some bandwidth. > g the body of the requested resource, saving some bandwidth. n 152 Web developers invented a technique that Steve Sounders cal n 152 Web developers invented a technique that Steve Sounders cal > led <em>revving</em><sup><a href="https://www.stevesouders.com/bl > led <em>revving</em><sup><a href="https://www.stevesouders.com/bl > og/2008/08/23/revving-filenames-dont-use-querystring/">[1]</a></s > og/2008/08/23/revving-filenames-dont-use-querystring/">[1]</a></s > up>. Infrequently updated files are named in specific way: in the > up>. Infrequently updated files are named in specific way: in the > ir URL, usually in the filename, a revision number is added. That > ir URL, usually in the filename, a revision (or version) number i > way each new revision of this resource is considered as a resour > s added. That way each new revision of this resource is considere > ce on its own that <em>never</em> changes and that can have an ex > d as a resource on its own that <em>never</em> changes and that c > piration time very far in the future, usually one year or even mo > an have an expiration time very far in the future, usually one ye > re. In order to have the new versions, all the links to them must > ar or even more. In order to have the new versions, all the links > be changed, that is the drawback of this method: additional comp > to them must be changed, that is the drawback of this method: ad > lexity that is usually taken care by the tool chain used by Web d > ditional complexity that is usually taken care by the tool chain > evelopers. When the infrequently variable resources change they i > used by Web developers. When the infrequently variable resources > nduce an additional change to often variable resources. When thes > change they induce an additional change to often variable resourc > e are read, the new versions of the others are also read. > es. When these are read, the new versions of the others are also > read. 153 </p> 154 <p> 153 </p> 155 This technique has an additional benefits: it allows to gua 154 <p> > rantee that two resources can be updated at the same time and tha > t caches will not lead to the situation where the old version of > one of this resource is used with the new version of this other o > nes. This is very important when Web sites have CSS stylesheets o > r JS scripts that have dependencies and must be read coherently. 155 This technique has an additional benefit: updating two cach > ed resources at the same time will not lead to the situation wher > e the out-dated version of one resource is used in combination wi > th the new version of the other one. This is very important when > web sites have CSS stylesheets or JS scripts that have mutual dep > endencies, i.e., they depend on each other because they refer to > the same HTML elements. n 176 The {{HTTPHeader("ETag")}} response header is an <em>opaque n 176 The {{HTTPHeader("ETag")}} response header is an <em>opaque > -to-the-useragent</em> value that can be used as a strong validat > -to-the-useragent</em> value that can be used as a strong validat > or. That means that the client, like the browser, has don't know > or. That means that a HTTP user-agent, such as the browser, does > what this string represents and can't predict it, even if it is n > not know what this string represents and can't predict what its v > ot generated randomly on the server side. If the <code>ETag</code > alue would be. If the <code>ETag</code> header was part of respon > > header is present in a response, then the client can issue an { > se for a resource, the client can issue an {{HTTPHeader("If-None- > {HTTPHeader("If-None-Match")}} request header to validate the cac > Match")}} in the header of future requests – in order to validate > hed document. > the cached resource. t 182 When a validation request is made, the server can either ig t 182 When a validation request is made, the server can either ig > nore the validation request and response with a normal {{HTTPStat > nore the validation request and response with a normal {{HTTPStat > us(200)}} <code>OK</code>, or it can return {{HTTPStatus(304)}} < > us(200)}} <code>OK</code>, or it can return {{HTTPStatus(304)}} < > code>Not Modified</code> to instruct the browser to use its cache > code>Not Modified</code> (with an empty body) to instruct the bro > d copy. The latter response can also include headers that update > wser to use its cached copy. The latter response can also include > the expiration time of the cached document. > headers that update the expiration time of the cached document.