http caching basics

43
HTTP Caching Basics

Upload: martin-breest

Post on 13-Jan-2017

59 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Http caching basics

HTTP Caching Basics

Page 2: Http caching basics

Agenda

HTTP Basics

Cache-Related HTTP Headers

Expiration

Revalidation

Variation

Page 3: Http caching basics

HTTP Basics

Page 4: Http caching basics

HTTP Basics - Browser and origin server

Browser OriginServer

Page 5: Http caching basics

HTTP Basics - Resource

Browser OriginServer

Resource/index.html

Page 6: Http caching basics

HTTP Basics - Request and response

Browser OriginServer

Resource/index.html

Request

Response

Page 7: Http caching basics

HTTP Basics - Representation

Browser OriginServer

Resource/index.html

Request

Response

Representation

Cached Representation

Page 8: Http caching basics

HTTP Basics - Method and status code

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OK

Representation

HTTP Method

HTTP Status Code

Page 9: Http caching basics

HTTP Basics - Request and response headers

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1Accept: text/html ...Accept-Encoding: gzip, deflate, sdch, brUser-Agent: Mozilla/5.0 …Cookie: PHPSESSID=12b239d7825bd8d21d5e60df56cbc3a5;

ResponseHTTP/1.1 200 OKContent-Type: text/htmlContent-Encoding: gzipDate: Fri, 16 Sep 2016 12:13:45 GMT

Representation

Request header

Response header

Page 10: Http caching basics

HTTP Basics - Intermediaries

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OK

Representation

Intermediary

Representation

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OK

Resource/index.html

Page 11: Http caching basics

Quiz: What kind of intermediaries can you think of?

Page 12: Http caching basics

HTTP Basics - Intermediate caches

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OK

Representation

Intermediate Cache

(Akamai, Varnish)

Representation

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OK

Resource/index.html

CachedRepresentations

Page 13: Http caching basics

Cache-Related HTTP Headers

Page 14: Http caching basics

Quiz: Which cache-related HTTP headers do you know?

Page 15: Http caching basics

Cache-related HTTP headersExpiration

Cache-Control

Expires

Age

Pragma

Variation

Vary

Revalidation

ETag

Last-Modified

If-Modified-Since

If-None-Match

Page 16: Http caching basics

Quiz: Which are request and which are response headers?

Header Request Response

Cache-Control

Expires

Age

Pragma

ETag

Last-Modified

If-Modified-Since

If-None-Match

Vary

Page 17: Http caching basics

Cache-related HTTP headers in requests and responses

Header Request Response

Cache-Control x x

Expires x

Age x

Pragma x x

ETag x

Last-Modified x

If-Modified-Since x

If-None-Match x

Vary x

Page 18: Http caching basics

Expiration

Page 19: Http caching basics

Expiration - Absolute vs. relative expiration date

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTExpires: Fri, 16 Sep 2016 13:00:00 GMT

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: max-age=2700

CachedRepresentation

CachedRepresentation

Page 20: Http caching basics

Quiz: Which Cache-Control directives do you know?

Page 21: Http caching basics

Expiration - Cache-Control directivesDirectives Request Response

no-cache x x

no-store x x

max-age x x

s-maxage x

max-stale x

min-fresh x

no-transform x x

only-if-cached x

public x

private x

must-revalidate x

proxy-revalidate x

Page 22: Http caching basics

Expiration - Enforce revalidation with origin server

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: max-age=2700, must-revalidate

CachedRepresentation

RepresentationNever use stale representation(even if origin server is down)

Page 23: Http caching basics

Expiration - No-cache shortcut

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: max-age=0, must-revalidate

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: no-cache

=Cached

Representation

CachedRepresentation

Page 24: Http caching basics

Expiration - No-cache vs. no-store

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: no-cache

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: no-store

CachedRepresentation

CachedRepresentation

Page 25: Http caching basics

Expiration - Age specifies how long representation is cached already

ResponseHTTP/1.1 200 OKCache-Control: max-age=2700

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKCache-Control: max-age=2700Age: 0

Intermediate Cache

(Akamai, Varnish)

RequestGET /index.html HTTP/1.1

Resource/index.html

1st Request

2nd Request 15 Minutes Later Different Browser

Browser

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKCache-Control: max-age=2700Age: 900

Intermediate Cache

(Akamai, Varnish)Resource

/index.html

CachedRepresentation

CachedRepresentation

Page 26: Http caching basics

Revalidation

Page 27: Http caching basics

Revalidation - Based on what?

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: max-age=2700, must-revalidate

CachedRepresentation

Representation

Revalidation based on what?

Page 28: Http caching basics

Revalidation - Weak vs. strong cache validator

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: max-age=2700, must-revalidateLast-Modified: Fri, 15 Sep 2016 12:00:00 GMT

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: max-age=2700, must-revalidateETag: "3e86-410-3596fbbc"

CachedRepresentation

CachedRepresentation

Page 29: Http caching basics

Revalidation - Revalidation based on Last-Modified

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1If-Modified-Since: Fri, 15 Sep 2016 12:00:00 GMT

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 13:00:00 GMTCache-Control: max-age=2700, must-revalidateLast-Modified: Fri, 16 Sep 2016 13:00:00 GMT

Browser OriginServer

Resource/index.html

ResponseHTTP/1.1 304 Not ModifiedDate: Fri, 16 Sep 2016 13:00:00 GMTCache-Control: max-age=2700, must-revalidateLast-Modified: Fri, 15 Sep 2016 12:00:00 GMT

CachedRepresentation

CachedRepresentation

OR

Page 30: Http caching basics

Revalidation - Revalidation based on ETag

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1If-None-Match: "3e86-410-3596fbbc"

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 13:00:00 GMTCache-Control: max-age=2700, must-revalidateETag: "6e35-240-2672fbbc"

Browser OriginServer

Resource/index.html

ResponseHTTP/1.1 304 Not ModifiedDate: Fri, 16 Sep 2016 13:00:00 GMTCache-Control: max-age=2700, must-revalidateETag: "3e86-410-3596fbbc"

CachedRepresentation

CachedRepresentation

OR

Page 31: Http caching basics

Quiz: How does revalidation behave for different Cache-Control setups?

Page 32: Http caching basics

Expiration - No-store means no revalidation possible

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: no-cacheLast-Modified: Fri, 15 Sep 2016 12:00:00 GMT

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1

ResponseHTTP/1.1 200 OKDate: Fri, 16 Sep 2016 12:15:00 GMTCache-Control: no-storeLast-Modified: Fri, 15 Sep 2016 12:00:00 GMT

CachedRepresentation

CachedRepresentation

Page 33: Http caching basics

Variation

Page 34: Http caching basics

Variation - Most common is Vary on Accept-Encoding

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1Accept-Encoding: gzip

ResponseHTTP/1.1 200 OKContent-Encoding: gzipVary: Accept-Encoding

Representation

Means different zipped and unzipped variants of representation are possible.

Page 35: Http caching basics

ResponseHTTP/1.1 200 OKVary: Accept-Encoding

Variation - Used to control behavior of intermediate caches

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1Accept-Encoding: gzip

ResponseHTTP/1.1 200 OKVary: Accept-Encoding

Representation

Intermediate Cache

(Akamai, Varnish)

Representation

RequestGET /index.html HTTP/1.1Accept-Encoding: gzip

Resource/index.html

CachedRepresentations

Accept-Encoding: gzip Empty Accept-Encoding

Page 36: Http caching basics

ResponseHTTP/1.1 200 OKVary: Accept-Encoding, User-Agent

Variation - Also used in combination with User-Agentto deliver device type specific content

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1Accept-Encoding: gzipUser-Agent: Chrome …

ResponseHTTP/1.1 200 OKVary: Accept-Encoding, User-Agent

Representation

Intermediate Cache

(Akamai, Varnish)

Representation

RequestGET /index.html HTTP/1.1Accept-Encoding: gzipUser-Agent: Chrome …

Resource/index.html

CachedRepresentations

Accept-Encoding: gzipUser-Agent: Chrome …

Accept-Encoding: gzip User-Agent: Edge …

Page 37: Http caching basics

Quiz: What is the problem with that setup?

Page 38: Http caching basics

Combinations are endless and hit rate will be really low

ResponseHTTP/1.1 200 OKVary: Accept-Encoding, User-Agent

Variation - Wrong usage of Vary header can lead to low hit rates

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1Accept-Encoding: gzipUser-Agent: Chrome …

ResponseHTTP/1.1 200 OKVary: Accept-Encoding, User-Agent

Representation

Intermediate Cache

(Akamai, Varnish)

Representation

RequestGET /index.html HTTP/1.1Accept-Encoding: gzipUser-Agent: Chrome …

Resource/index.html

CachedRepresentations

Accept-Encoding: gzipUser-Agent: Chrome …

Accept-Encoding: gzipUser-Agent: Edge …

Page 39: Http caching basics

Combinations are endless and hit rate will be really low

ResponseHTTP/1.1 200 OKVary: Accept-Encoding, User-Agent

Variation - Don’t do it like that

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1Accept-Encoding: gzipUser-Agent: Chrome …

ResponseHTTP/1.1 200 OKVary: Accept-Encoding, User-Agent

Representation

Intermediate Cache

(Akamai, Varnish)

Representation

RequestGET /index.html HTTP/1.1Accept-Encoding: gzipUser-Agent: Chrome …

Resource/index.html

CachedRepresentations

Accept-Encoding: gzipUser-Agent: Chrome …

Accept-Encoding: gzipUser-Agent: Edge …

Page 40: Http caching basics

ResponseHTTP/1.1 200 OKVary: Accept-Encoding, X-Device

Variation - Create generic header to improve hit rate and return private in Cache-Control to avoid caching

Browser OriginServer

Resource/index.html

RequestGET /index.html HTTP/1.1Accept-Encoding: gzipUser-Agent: Chrome ...

ResponseHTTP/1.1 200 OKCache-Control: private

Representation

Intermediate Cache

(Akamai, Varnish)

Representation

RequestGET /index.html HTTP/1.1Accept-Encoding: gzipX-Device: Mobile

Resource/index.html

CachedRepresentations

Accept-Encoding: gzipX-Device: Mobile

Accept-Encoding: gzipX-Device: Desktop

Page 41: Http caching basics

Conclusion

Page 42: Http caching basics

Conclusion

Use Cache-Control header in general to control caching behaviour

Implement ETag and/or Last-Modified to allow for revalidation

Do not use User-Agent in Vary header and rather extract required attributes and work with X-... headers

Page 43: Http caching basics

Questions?