Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Benefits of a RESTful Architecture | 207

be the same as if no cache were involved. What this means in practice depends not
only on what type of caches are used and where they are placed within a network
architecture, but also on the semantics of updates and “freshness” within the appli-
cation itself.


Because caching behavior is so application-dependent, the decision was made for
HTT Pto provide mechanisms for caching without specifying actual behavior or client/
server responsibility in great detail. As Fielding describes it (in section 6.3.2.6 of his
thesis):


Because REST tries to balance the need for efficient, low-latency behavior with the desire
for semantically transparent cache behavior, it is critical that HTT Pallow the applica-
tion to determine the caching requirements rather than hard-code it into the protocol
itself. The most important thing for the protocol to do is to fully and accurately
describe the data being transferred, so that no application is fooled into thinking it has
one thing when it actually has something else. HTTP/1.1 does this through the addi-
tion of theCache-Control,Age,Etag, andVary header fields.

Section 13 of the HTTP/1.1 specification (RFC 2616) details HTTP’s caching behavior.*
HTT Pprovides several features for cache control, which interact to provide a frame-
work on which servers, clients, and intermediaries can build a caching policy. These
features include the following:


ConditionalGET
A conditionalGETis an HTTPGETrequest containing one or more additional
header fields specifying conditions under which the requested resource should or
should not be sent. It is used in conjunction with theLast-Modifiedheader and/or
entity tags, discussed next.


Last-Modified
TheLast-Modifiedresponse header contains a date/time value indicating the
time at which the provided resource was last modified. When a client holds a
(potentially old) version of this resource, it canvalidatethat version by perform-
ing a conditionalGETwith anIf-Modified-Sinceheader set to the time its ver-
sion was last modified. If the server has a newer version, it will send the new
version with an updatedLast-Modifiedheader. If there is no newer version, the
server will send a 304 Not Modified response with no body, saving time and
bandwidth.


Entity tags (ETags)
Entity tags are short labels that serve to identify a particular version of an entity.
Typically, they are a hash of the response body; in this way, entities can be com-
pared by exchanging the tag rather than the entire body.
Entity tags, likeLast-Modified, also use conditionalGET. The client sends a request
with anIf-No-Matchheader specifying the ETag of its latest version. If theserver
calculates a different ETag for the newest version of that resource, it will send


*http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

Free download pdf