ch07: Caching

1. The Benefits of Caches

  • reduce redundant data transfers.

  • reduce network bottlenecks.

  • reduce demand on origin servers.

  • reduce distance delays.

2. Hits and Misses

2.1 Revalidations

  • Revalidate hit: HTTP 304 Not Modified response.

  • Revalidate miss: HTTP 200 OK.

  • Object deleted: HTTP 404 Not Found.

2.2 Hit Rate & Byte Hit Rate

  • Hit rate: describes how many web transactions are kept off the outgoing network.

  • Byte hit rate: describes how many bytes are kept off the Internet.

3. Cache Topologies

Private caches and public caches.

Caches can also be built into hierarchy.

4. Cache Processing Steps

  1. Receiving: Cache reads the arriving requests message from the network.

  2. Parsing: Cache parses the message, extracting the URL and headers.

  3. Lookup: Cache checks if a local copy is available and, if not, fetches a copy (and stores it locally).

  4. Freshness check: Cache checks if cached copy is fresh enough and, if not, asks server for any updates.

  5. Response creation: Cache makes a response message with the new headers and cached body.

  6. Sending: Cache sends the response back to the client over the network.

  7. Logging: Optionally, cache creates a log file entry describing the transaction.

Flow chart:

5. Keeping Copies Fresh

Document expiration and server revalidation.

5.1 Document Expiration

HTTP lets an origin server attach an "expiration date" to each document, using special HTTP Cache-Control and Expires headers.

5.2 Expiration Dates and Ages

Header

Description

Cache-Control: max-age

The max-age defines maximum age of the document(in seconds).

Expires

Specifies an absolute expiration date. If the expiration date is in the past, the document is no longer fresh.

5.3 Server Revalidation

  • If revalidation shows the content has changed, the cache gets a new copy of the document, stores it in place of the old data, and sends the document to the client.

  • If revalidation shows the content has not changed, the cache gets a new headers, including a new expiration date, and updates the headers in the cache.

5.4 Revalidation with Conditional Methods

Two conditional headers used in cache revalidation:

Header

Description

If-Modified-Since: \

Perform the requested method if the document has been modified since the specified date. This is used in conjunction with the Last-Modified server response header, to fetch content only if the content has been modified from the cached version.

If-None-Match: \

Instead of matching on last-modified date, the server may provide special tags on the document that act like serial numbers. The If-None-Match header performs the requested method if the cached tags differ from the tags in the server's document.

If-Modified-Since:

If-None-Match:

6. Controlling Cachability

A server can define how long a document can be cached before it expires. In decreasing order of priority, the server can:

  • Attach a Cache-Control: no-store header to the response.

  • Attach a Cache-Control: no-cache header to the response.

  • Attach a Cache-Control: must-revalidate header to the response.

  • Attach a Cache-Control: max-age header to the response.

  • Attach an Expires date header to the response.

  • Attach no expiration information, letting the cache determine tis own heuristic expiration date.

6.1 No-Cache and No-Store Response Headers

  • No-Store: forbids a cache from making a copy of the response.

  • No-Cache: a response can actually be stored in the local cache storage. It just cannot be served from the cache to the client without first revalidating the freshness with the origin server.

6.2 Max-Age Response Headers

The Cache-Control: max-age header indicates the number of seconds since it came from the server for which a document can be considered fresh.

s-maxage applies only to shared caches.

Cache-Control: max-age=3600
Cache-Control: s-maxage=3600

Servers can request that caches either not cache a document or refresh on every access by setting the maximum aging to zero:

Cache-Control: max-age=0
Cache-Control: s-maxage=0

6.3 Expires Response Headers

Deprecated

Use an absolute time.

6.4 Must-Revalidate Response Headers

Cache-Control: must-revalidate

6.5 Heuristic Expiration

LM-Factor algorithm: guess a fresh time.

7. Detailed Algorithms

Age:

Last updated