Unlock the Full Potential of HTTP Caching



Unlock the Full Potential of HTTP Caching

0 0


http_caching

Unlock the Full Potential of HTTP Caching

On Github thuva / http_caching

Unlock the Full Potential of HTTP Caching

Thuva Tharma | Developer @ theScore Inc. | @ttharma

RFC 2616, Section 13

The goal of caching in HTTP/1.1 is to eliminate the need to send requests in many cases, and to eliminate the need to send full responses in many other cases

So How Do We Go About Achieving This Goal?

HTTP Request/Response Headers

  > GET /thescore_logo.png
  > If-Modified-Since: Tue, 10 Aug 2013 03:51:19 GMT
  > If-None-Match: "5ee39d70f536014bdb599c1442d"

  < 200 OK
  < ...
  < Cache-Control: max-age=300
  < Expires: Tue, 10 Sep 2013 03:56:19 GMT
  < Last-Modified: Tue, 10 Sep 2013 03:51:19 GMT
  < Etag: "3bbe876fc7acb81cfe03b925fa142710"
          

Expiration: Eliminating the Need to Send Requests

This reduces network round-trips

  > GET /thescore_logo.png

  < 200 OK
  < Cache-Control: max-age=300
  < Expires: Tue, 10 Sep 2013 03:56:19 GMT
          

Validation: Eliminating the Need to Send Full Responses

This reduces network bandwidth

Last-Modified Validator

  > GET /thescore_logo.png

  < 200 OK
  < Last-Modified: Tue, 10 Sep 2013 03:51:19 GMT
          
  > GET /thescore_logo.png
  > If-Modified-Since: Tue, 10 Sep 2013 03:51:19 GMT

  < 304 Not Modified
  < Last-Modified: Tue, 10 Sep 2013 03:51:19 GMT
          

Etag Validator

  > GET /thescore_logo.png

  < 200 OK
  < Etag: "3bbe876fc7acb81cfe03b925fa142710"
          
  > GET /thescore_logo.png
  > If-None-Match: "3bbe876fc7acb81cfe03b925fa142710"

  < 304 Not Modified
  < Etag: "3bbe876fc7acb81cfe03b925fa142710"
          

Combining Expiration & Validation

  > GET /thescore_logo.png

  < 200 OK
  < Cache-Control: max-age=300
  < Etag: "3bbe876fc7acb81cfe03b925fa142710"
          
  > GET /thescore_logo.png
  > If-None-Match: "3bbe876fc7acb81cfe03b925fa142710"

  < 304 Not Modified
  < Cache-Control: max-age=300
  < Etag: "3bbe876fc7acb81cfe03b925fa142710"
          

Non-Browser Clients

If you're developing a mobile application, use a library that takes care of the caching bits for the HTTP client library that you're using:

  • iOS: NSURLCache (drop-in caching for NSURLConnection)
  • Android: HttpResponseCache (drop-in caching for HttpUrlConnection)

Gateway Caches

Gateway caches or reverse proxy caches let you take advantage of HTTP caching on server-side

You can use Rack Cache, Varnish, Squid, Akamai, etc. as your gateway caches.

Let's see what happens when you put a gateway cache between the client and the server

Nate makes the first request

Roel makes a request after 30 seconds

Mark makes a request after 2 minutes

Geo-Distributed Gateway Caches

Reduce network latency for your clients by distributing the gateway caches geographically

Once you have proper HTTP caching implemented on your server, you can use services like Akamai or Fastly for this.

Discussion: How to Come Up with Effective Expiration & Validation Values

Thank You!

thuva.github.com/http_caching