http caching basics
TRANSCRIPT
HTTP Caching Basics
Agenda
HTTP Basics
Cache-Related HTTP Headers
Expiration
Revalidation
Variation
HTTP Basics
HTTP Basics - Browser and origin server
Browser OriginServer
HTTP Basics - Resource
Browser OriginServer
Resource/index.html
HTTP Basics - Request and response
Browser OriginServer
Resource/index.html
Request
Response
HTTP Basics - Representation
Browser OriginServer
Resource/index.html
Request
Response
Representation
Cached Representation
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
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
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
Quiz: What kind of intermediaries can you think of?
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
Cache-Related HTTP Headers
Quiz: Which cache-related HTTP headers do you know?
Cache-related HTTP headersExpiration
Cache-Control
Expires
Age
Pragma
Variation
Vary
Revalidation
ETag
Last-Modified
If-Modified-Since
If-None-Match
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
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
Expiration
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
Quiz: Which Cache-Control directives do you know?
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
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)
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
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
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
Revalidation
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?
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
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
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
Quiz: How does revalidation behave for different Cache-Control setups?
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
Variation
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.
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
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 …
Quiz: What is the problem with that setup?
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 …
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 …
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
Conclusion
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
Questions?