SlideShare una empresa de Scribd logo
1 de 141
Descargar para leer sin conexión
Speaker | Company
Title //
Presentation Name
Caching on the Edge
Fabien Potencier
Who are you?
May 1996
RFC1945 – HTTP/1.0
March 1999
RFC2616 – HTTP/1.1
http://www.ietf.org/rfc/rfc2616.txt
HTTP Bis –Work in progress
http://tools.ietf.org/wg/httpbis/
http://www.flickr.com/photos/visualpanic/233508614
 p1: Messaging: Low-level message parsing and connection management
 p2: Semantics: Methods, status codes and headers
 p3: Payload: Dealing with content, message-specific headers
 p4: Conditional Requests: e.g., If-Modified-Since
 p5: Range Requests: Getting partial content
 p6: Caching: Browser and intermediary caches
 p7: Authentication: HTTP authentication framework
HTTP
The Client sends a Request to the Server
The Server sends back a Response to the Client
A Client (browser, bot,WS, curl, …)
A Server (Apache, nginx, …)
The Request and the Response are HTTP messages
Let’s play with HTTP and HTTP headers
GET / HTTP/1.1
Host: http.trainings.sensiolabs.com
« The request-header fields allow the client to pass additional
information about the request, and about the client itself, to the
server.These fields act as request modifiers, with semantics
equivalent to the parameters on a programming language method
invocation. »
« Each HTTP header field consists of a case-insensitive field name
followed by a colon (":"), optional whitespace, and the field value »
HTTP/1.1 200 OK
Date: Wed, 15 Oct 2005 07:07:07 GMT
Server: Apache
Content-Length: 14
Content-Type: text/html
Hello World!
Live HTTP headers Firefox extension
http://livehttpheaders.mozdev.org/
$ curl -i http://http.trainings.sensiolabs.com/
HTTP/1.1 200 OK
Date: Wed, 15 Oct 2005 07:07:07 GMT
Server: Apache
Content-Length: 14
Content-Type: text/html
Hello World!
$ telnet localhost 80
Trying ::1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
Host: http.trainings.sensiolabs.com
HTTP/1.1 200 OK
Date: Wed, 15 Oct 2005 07:07:07 GMT
Server: Apache
Content-Length: 14
Content-Type: text/html
Hello World!
Connection closed by foreign host.
HTTP Headers with PHP
header('Content-Type: text/plain');
header('content-type: text/plain');
Caching in the HTTP Specification
 p1: Messaging: Low-level message parsing and connection management
 p2: Semantics: Methods, status codes and headers
 p3: Payload: Dealing with content, message-specific headers
 p4: Conditional Requests: e.g., If-Modified-Since
 p5: Range Requests: Getting partial content
 p6: Caching: Browser and intermediary caches
 p7: Authentication: HTTP authentication framework
HTTP Expiration
HTTPValidation
Fresh vs Stale
HTTP Headers for Expiration
Cache-Control
Expires
HTTP Headers forValidation
Last-Modified / If-Modified-Since
ETag / If-None-Match
HTTP Cache headers
only work with“safe”HTTP methods
(like GET & HEAD)
Never change the state of the server
when serving a GET request
(we are talking about the application’s state of course,
you can log, cache, …)
HTTP Expiration
Expires
« The "Expires" header field
gives the date/time after which
the response is considered stale. »
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
HTTP/1.1 200 OK
Expires: Thu,	
  01	
  Dec	
  …	
  
Hello
GET /foo
HTTP/1.1 200 OK
Expires: Thu,	
  01	
  Dec	
  
…	
  
Hello
HTTP/1.1 200 OK
Expires: Thu,	
  01	
  Dec	
  …	
  
Hello
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo
HTTP/1.1 200 OK
Expires: Thu,	
  01	
  Dec	
  
…	
  
Hello
HTTP/1.1 200 OK
Expires: Thu,	
  01	
  Dec	
  …	
  
Hello
Before	
  expira4on	
  
Your	
  applica4on	
  is	
  not	
  called	
  
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
HTTP/1.1 200 OK
Expires: Thu,	
  01	
  Dec	
  …	
  
Hello
GET /foo
HTTP/1.1 200 OK
Expires: Thu,	
  01	
  Dec	
  
…	
  
Hello
HTTP/1.1 200 OK
Expires: Thu,	
  01	
  Dec	
  …	
  
Hello
A<er	
  expira4on	
  
Not	
  fresh	
  
Expires: Thu, 01 Dec 2010 16:00:00 GMT
Date in RFC1123 format, not RFC2822
(timezone always GMT, which is ~ UTC)
$expires = gmdate('D, j M Y H:i:s T', time() + 5);
header('Expires: '.$expires);
$date = new DateTime(null, new DateTimeZone('UTC'));
$date->modify('+5 seconds');
$expires = $date->format('D, d M Y H:i:s').' GMT';
$date = gmdate('D, j M Y H:i:s T', time() + 5);
header('Expires: '.$expires);
$date->setTimezone(new DateTimeZone('UTC'));
/expires_with_expires_in_5s.php
WARNING
The clocks on theWeb server
AND the cache (aka browser)
MUST be synchronised
WARNING
« HTTP/1.1 servers SHOULD NOT
send Expires dates
more than one year in the future. »
Mostly useful to make static assets (images, css, js, …)
cacheable with an extremely long expiry time
But there is a better way!
Use Cache-Control instead
header('Cache-Control: max-age=5');
http.trainings.sensiolabs.com/expires_in_5s.php
Don’t use Expires
Use Cache-Control
…except if you want to set a date,
which should be pretty rare
HTTPValidation
“304 Not Modified”is your friend
HTTP Headers forValidation
Etag / If-None-Match
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo
HTTP/1.1 200 OK
ETag: abcdef	
  
…	
  
Hello
HTTP/1.1 200 OK
ETag: abcdef	
  
Hello
HTTP/1.1 200 OK
ETag: abcdef	
  
Hello
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
If-None-Match: abcdef
HTTP/1.1 304 Not Modified
GET /foo
HTTP/1.1 200 OK
ETag: abcdef	
  
…	
  
Hello
HTTP/1.1 200 OK
ETag: abcdef	
  
Hello
If	
  the	
  resource	
  has	
  not	
  changed	
  
SomeCache
GET /foo
HTTP/1.1 200 OK
ETag: abcdef	
  
…	
  
Hello
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
If-None-Match: abcdef
If	
  the	
  resource	
  has	
  changed	
  
GET /foo
HTTP/1.1 200 OK
ETag: 123456	
  
…	
  
Hello
HTTP/1.1 200 OK
ETag: 123456	
  
Hello
HTTP/1.1 200 OK
ETag: 123456	
  
Hello
// compute ETag value
$etag = '...';
if (
isset($_SERVER['HTTP_IF_NONE_MATCH'])
&& $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
) {
header('HTTP/1.1 304 Not Modified');
} else {
header('ETag: '.$etag);
echo 'Hello';
}
This is a simple but naïve implementation…
HTTP Headers forValidation
Last-Modified / If-Modified-Since
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo
HTTP/1.1 200 OK
Last-Modified: Thu,	
  	
  
…	
  
Hello
HTTP/1.1 200 OK
Last-Modified: Thu,	
  …	
  
Hello
HTTP/1.1 200 OK
Last-Modified: Thu,	
  …	
  
Hello
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
If-Modified-Since: Thu,
HTTP/1.1 304 Not Modified
GET /foo
HTTP/1.1 200 OK
Last-Modified: Thu,	
  	
  
…	
  
Hello
HTTP/1.1 200 OK
Last-Modified: Thu,	
  …	
  
Hello
If	
  the	
  resource	
  has	
  not	
  changed	
  
SomeCache
GET /foo
HTTP/1.1 200 OK
Last-Modified: Thu,
…	
  
Hello
GET /foo
HTTP/1.1 200 OK
Last-Modified: Sun,
…	
  
Hello
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
If-Modified-Since: Thu,
If	
  the	
  resource	
  has	
  changed	
  
HTTP/1.1 200 OK
Last-Modified: Sun,	
  …	
  
Hello
HTTP/1.1 200 OK
Last-Modified: Sun,	
  …	
  
Hello
Expiration &Validation
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo
HTTP/1.1 200 OK
Etag: abcdef
Cache-­‐Control:	
  max-­‐
age=10	
  
…	
  
Hello
HTTP/1.1 200 OK
ETag: abcdef	
  
Cache-­‐Control:	
  max-­‐age=10	
  
Hello
HTTP/1.1 200 OK
ETag: abcdef	
  
Cache-­‐Control:	
  max-­‐age=10	
  
Hello
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
Before	
  expira4on	
  
Your	
  applica4on	
  is	
  not	
  called	
  
GET /foo
HTTP/1.1 200 OK
Etag: abcdef
Cache-­‐Control:	
  max-­‐
age=10	
  
…	
  
Hello
HTTP/1.1 200 OK
ETag: abcdef	
  
Cache-­‐Control:	
  max-­‐age=10	
  
Hello
Browser
YourPHPapplication
SomeCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
If-None-Match: abcdef
HTTP/1.1 304 Not Modified
Cache-­‐Control:	
  max-­‐age=10	
  
A<er	
  expira4on	
  
but	
  resource	
  s4ll	
  valid	
  
GET /foo
HTTP/1.1 200 OK
Etag: abcdef
Cache-­‐Control:	
  max-­‐
age=10	
  
…	
  
Hello
HTTP/1.1 200 OK
ETag: abcdef	
  
Cache-­‐Control:	
  max-­‐age=10	
  
Hello
You can combine HTTP headers
the way you want
Expiration wins overValidation
Expiration allows you to scale
as less requests hit your server
(and client speed is better too)
Validation saves bandwidth
The goal is to never
generate the same response twice
PHP and Cache HTTP headers
session_start();
$_SESSION['foo'] = 'bar';
echo 'Hello';
http.trainings.sensiolabs.com/index.php
http.trainings.sensiolabs.com/cookie.php
Cache-Control: no-store, no-cache, must-revalidate,
post-check=0, pre-check=0
Kinds of caches
Browser Cache
On	
  the	
  server	
  side	
  
Browser
YourPHPapplication
BrowserCache
Within	
  a	
  Company	
  
On	
  the	
  server	
  side	
  
Browser
YourPHPapplication
BrowserBrowser
BrowserCacheBrowserCacheBrowserCache
Proxy Cache
On	
  the	
  server	
  side	
  
Within	
  a	
  Company	
  
Browser
YourPHPapplication
BrowserBrowser
BrowserCacheBrowserCacheBrowserCache
ProxyCache
On	
  the	
  server	
  side	
  
Within	
  a	
  Company	
  
Browser
YourPHPapplication
BrowserBrowser
BrowserCacheBrowserCacheBrowserCache
ProxyCache
Within	
  a	
  Company	
  
BrowserBrowserBrowser
BrowserCacheBrowserCacheBrowserCache
ProxyCache
Gateway Cache
On	
  the	
  server	
  side	
  
YourPHPapplication
Browser
BrowserCache
GatewayCache
Within	
  a	
  Company	
  
On	
  the	
  server	
  side	
  
Browser
YourPHPapplication
BrowserBrowser
BrowserCacheBrowserCacheBrowserCache
GatewayCache
On	
  the	
  server	
  side	
  
Within	
  a	
  Company	
  
Browser
YourPHPapplication
BrowserBrowser
BrowserCacheBrowserCacheBrowserCache
ProxyCache
BrowserBrowser
BrowserCacheBrowserCache
GatewayCache
Your PHP application
Gateway Cache
Reverse Proxy Cache
Surrogate Cache
HTTP Accelerator
Browser
Browser Cache
Proxy Cache
Browser Cache
Local cache for
when you hit“back”or
when images are reused throughout a website
Proxy Cache
A shared cache
Many people behind a single proxy
Installed by large corporations and ISPs
Reduce latency and network traffic
Gateway Cache
A shared cache on the server side
Installed by network administrators
Make websites more scalable, reliable and performing better
CDNs like Akaïma are gateway caches
Today, we will mainly talk about Gateway caches
Edge Caching
App
Browser
Browser
Gateway Cache
Gateway Cache
HTTP 1.1 allows caching anything by default
unless explicit Cache-Control header
In practice, most caches avoid anything with
Cache-Control
Cookie / Set-Cookie
WWW-Authenticate / Authorization
POST / PUT
302 / 307 status codes
Cache-Control: private
This is the default with PHP when you have a session
Browser cache will still work fine here
(public means shared caches, private means browser cache)
A gateway cache won't cache anything
"private" or carrying a cookie
In a real-world: tracking cookies (Google Analytics)
All proxies do caching based on the same HTTP headers
We need several clients to understand how it works
and how the cached is shared
HTTP Expiration
GatewayCache
Bob
YourPHPapplication
Bob’sCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo
HTTP/1.1 200 OK
C-C: max-age=600	
  
…	
  
Hello
HTTP/1.1 200 OK
C-C: max-age=600	
  
Hello
HTTP/1.1 200 OK
C-C: max-age=600	
  
Hello
GET /foo
HTTP/1.1 200 OK
C-C: max-age=600	
  
…	
  
Hello
HTTP/1.1 200 OK
C-C: max-age=600	
  
Hello
Bob’sCache
GatewayCache
Bob
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo
HTTP/1.1 200 OK
C-C: max-age=600	
  
…	
  
Hello
HTTP/1.1 200 OK
C-C: max-age=600	
  
Hello
GET /foo
HTTP/1.1 200 OK
C-C: max-age=600	
  
…	
  
Hello
BrowserCache
Alice
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
HTTP/1.1 200 OK
C-C: max-age=600	
  
Hello
HTTP/1.1 200 OK
C-C: max-age=600	
  
Hello
GatewayCache
GET /foo
HTTP/1.1 200 OK
C-C: max-age=600	
  
…	
  
Hello
GET /foo
HTTP/1.1 200 OK
C-C: max-age=600	
  
…	
  
Hello
HTTPValidation
Bob’sCache
GatewayCache
Bob
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo
HTTP/1.1 200 OK
Etag: abcde	
  
…	
  
Hello
HTTP/1.1 200 OK
Etag: abcde	
  
Hello
HTTP/1.1 200 OK
Etag: abcde	
  
Hello
GET /foo
HTTP/1.1 200 OK
Etag: abcde	
  
…	
  
Hello
HTTP/1.1 200 OK
Etag: abcde	
  
Hello
Bob’sCache
GatewayCache
Bob
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
If-None-Match: ab
GET /foo HTTP/1.1
Host: foo.org
If-None-Match: ab
GET /foo
HTTP/1.1 200 OK
Etag: ab	
  
…	
  
Hello
HTTP/1.1 200 OK
Etag: ab	
  
Hello
GET /foo
HTTP/1.1 200 OK
Etag: ab	
  
…	
  
Hello
304 Not Modified304 Not Modified
GatewayCache
YourPHPapplication
BrowserCache
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
If-None-Match: ab
GET /foo
HTTP/1.1 200 OK
Etag: ab	
  
…	
  
Hello
HTTP/1.1 200 OK
Etag: ab	
  
Hello
304 Not Modified
Alice
HTTP/1.1 200 OK
Etag: ab	
  
Hello
GET /foo
HTTP/1.1 200 OK
Etag: ab	
  
…	
  
Hello
Gateway caches
Varnish (only does that and tries to do it well)
Squid (just one way to use it)
mod_cache (Apache)
Gateway caches are a great way
to make your website performs better
But what if you cannot cache whole pages?
What if a page has "more" dynamic parts?
Lorem	
  ipsum	
  dolor	
  sit	
  
amet,	
  consectetur	
  
adipiscing	
  elit.	
  In	
  vel	
  nulla	
  
arcu,	
  vitae	
  cursus	
  nunc.	
  
Integer	
  semper	
  turpis	
  et	
  
enim	
  porRtor	
  iaculis.	
  
Nulla	
  facilisi.	
  Lorem	
  ipsum	
  
dolor	
  sit	
  amet,	
  
consectetur	
  adipiscing	
  elit.	
  
Mauris	
  vehicula	
  
ves4bulum	
  dictum.	
  
Aenean	
  non	
  velit	
  tortor.	
  
Nullam	
  adipiscing	
  
malesuada	
  aliquam.	
  
Mauris	
  dignissim,	
  urna	
  
quis	
  iaculis	
  tempus,	
  justo	
  
libero	
  porRtor	
  est,	
  nec	
  
eleifend	
  est	
  elit	
  vitae	
  ante.	
  
Curabitur	
  interdum	
  luctus	
  
metus.	
  
Lorem	
  ipsum	
  dolor	
  sit	
  amet,	
  consectetur	
  
adipiscing	
  elit.	
  In	
  vel	
  nulla	
  arcu,	
  vitae	
  
cursus	
  nunc.	
  Integer	
  semper	
  turpis	
  et	
  enim	
  
porRtor	
  iaculis.	
  Nulla	
  facilisi.	
  Lorem	
  ipsum	
  
dolor	
  sit	
  amet,	
  consectetur	
  adipiscing	
  elit.	
  
Mauris	
  vehicula	
  ves4bulum	
  dictum.	
  
Aenean	
  non	
  velit	
  tortor.	
  Nullam	
  adipiscing	
  
malesuada	
  aliquam.	
  Mauris	
  dignissim,	
  urna	
  
quis	
  iaculis	
  tempus,	
  justo	
  libero	
  porRtor	
  
est,	
  nec	
  eleifend	
  est	
  elit	
  vitae	
  ante.	
  
Curabitur	
  interdum	
  luctus	
  metus,	
  in	
  
pulvinar	
  lectus	
  rutrum	
  sit	
  amet.	
  Duis	
  
gravida,	
  metus	
  in	
  dictum	
  eleifend,	
  dolor	
  
risus	
  4ncidunt	
  ligula,	
  non	
  volutpat	
  nulla	
  
sapien	
  in	
  elit.	
  Nulla	
  rutrum	
  erat	
  id	
  neque	
  
suscipit	
  eu	
  ultricies	
  odio	
  sollicitudin.	
  
Aliquam	
  a	
  mi	
  vel	
  eros	
  placerat	
  hendrerit.	
  
Phasellus	
  porRtor,	
  augue	
  sit	
  amet	
  
vulputate	
  venena4s,	
  dui	
  leo	
  commodo	
  
odio,	
  a	
  euismod	
  turpis	
  ligula	
  in	
  elit.	
  	
  
cacheable for 10 seconds
cacheable for 5 seconds
ESI… or Edge Side Includes
http://www.w3.org/TR/esi-lang
Lorem	
  ipsum	
  dolor	
  sit	
  
amet,	
  consectetur	
  
adipiscing	
  elit.	
  In	
  vel	
  nulla	
  
arcu,	
  vitae	
  cursus	
  nunc.	
  
Integer	
  semper	
  turpis	
  et	
  
enim	
  porRtor	
  iaculis.	
  
Nulla	
  facilisi.	
  Lorem	
  ipsum	
  
dolor	
  sit	
  amet,	
  
consectetur	
  adipiscing	
  elit.	
  
Mauris	
  vehicula	
  
ves4bulum	
  dictum.	
  
Aenean	
  non	
  velit	
  tortor.	
  
Nullam	
  adipiscing	
  
malesuada	
  aliquam.	
  
Mauris	
  dignissim,	
  urna	
  
quis	
  iaculis	
  tempus,	
  justo	
  
libero	
  porRtor	
  est,	
  nec	
  
eleifend	
  est	
  elit	
  vitae	
  ante.	
  
Curabitur	
  interdum	
  luctus	
  
metus.	
  
Lorem	
  ipsum	
  dolor	
  sit	
  amet,	
  consectetur	
  
adipiscing	
  elit.	
  In	
  vel	
  nulla	
  arcu,	
  vitae	
  
cursus	
  nunc.	
  Integer	
  semper	
  turpis	
  et	
  enim	
  
porRtor	
  iaculis.	
  Nulla	
  facilisi.	
  Lorem	
  ipsum	
  
dolor	
  sit	
  amet,	
  consectetur	
  adipiscing	
  elit.	
  
Mauris	
  vehicula	
  ves4bulum	
  dictum.	
  
Aenean	
  non	
  velit	
  tortor.	
  Nullam	
  adipiscing	
  
malesuada	
  aliquam.	
  Mauris	
  dignissim,	
  urna	
  
quis	
  iaculis	
  tempus,	
  justo	
  libero	
  porRtor	
  
est,	
  nec	
  eleifend	
  est	
  elit	
  vitae	
  ante.	
  
Curabitur	
  interdum	
  luctus	
  metus,	
  in	
  
pulvinar	
  lectus	
  rutrum	
  sit	
  amet.	
  Duis	
  
gravida,	
  metus	
  in	
  dictum	
  eleifend,	
  dolor	
  
risus	
  4ncidunt	
  ligula,	
  non	
  volutpat	
  nulla	
  
sapien	
  in	
  elit.	
  Nulla	
  rutrum	
  erat	
  id	
  neque	
  
suscipit	
  eu	
  ultricies	
  odio	
  sollicitudin.	
  
Aliquam	
  a	
  mi	
  vel	
  eros	
  placerat	
  hendrerit.	
  
Phasellus	
  porRtor,	
  augue	
  sit	
  amet	
  
vulputate	
  venena4s,	
  dui	
  leo	
  commodo	
  
odio,	
  a	
  euismod	
  turpis	
  ligula	
  in	
  elit.	
  	
  
Lorem	
  ipsum	
  dolor	
  sit	
  amet,	
  consectetur	
  
adipiscing	
  elit.	
  In	
  vel	
  nulla	
  arcu,	
  vitae	
  
cursus	
  nunc.	
  Integer	
  semper	
  turpis	
  et	
  enim	
  
porRtor	
  iaculis.	
  Nulla	
  facilisi.	
  Lorem	
  ipsum	
  
dolor	
  sit	
  amet,	
  consectetur	
  adipiscing	
  elit.	
  
Mauris	
  vehicula	
  ves4bulum	
  dictum.	
  
Aenean	
  non	
  velit	
  tortor.	
  Nullam	
  adipiscing	
  
malesuada	
  aliquam.	
  Mauris	
  dignissim,	
  urna	
  
quis	
  iaculis	
  tempus,	
  justo	
  libero	
  porRtor	
  
est,	
  nec	
  eleifend	
  est	
  elit	
  vitae	
  ante.	
  
Curabitur	
  interdum	
  luctus	
  metus,	
  in	
  
pulvinar	
  lectus	
  rutrum	
  sit	
  amet.	
  Duis	
  
gravida,	
  metus	
  in	
  dictum	
  eleifend,	
  dolor	
  
risus	
  4ncidunt	
  ligula,	
  non	
  volutpat	
  nulla	
  
sapien	
  in	
  elit.	
  Nulla	
  rutrum	
  erat	
  id	
  neque	
  
suscipit	
  eu	
  ultricies	
  odio	
  sollicitudin.	
  
Aliquam	
  a	
  mi	
  vel	
  eros	
  placerat	
  hendrerit.	
  
Phasellus	
  porRtor,	
  augue	
  sit	
  amet	
  
vulputate	
  venena4s,	
  dui	
  leo	
  commodo	
  
odio,	
  a	
  euismod	
  turpis	
  ligula	
  in	
  elit.	
  	
  
<esi:include src="..." />
<esi:include src="http://..." />
BrowserCache
GatewayCache
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /bar HTTP/1.1
Host: foo.org
Lorem	
  
ipsum	
  
dolor	
  	
  
<esi:include	
  
src="hYp.."	
  />	
  
HTTP/1.1 200 OK
Lorem	
  ipsum	
  
dolor	
  
HTTP/1.1 200 OK
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
BrowserCache
GatewayCache
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /bar HTTP/1.1
Host: foo.org
Lorem	
  
ipsum	
  
dolor	
  	
  
<esi:include	
  
src="hYp.."	
  />	
  
HTTP/1.1 200 OK
C-C: max-age=10
Lorem	
  ipsum	
  
dolor	
  
HTTP/1.1 200 OK
C-C: max-age=5
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
GET /foo
C-C: max-age=10	
  
Lor <esi:include />
GET /bar
C-C: max-age=5	
  
Lorem
GET /foo
C-C: max-age=10	
  
Lor Lorem
BrowserCache
GatewayCache
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
GET /foo
C-C: max-age=10	
  
Lor <esi:include />
GET /bar
C-C: max-age=5	
  
Lorem
GET /foo
C-C: max-age=10	
  
Lor Lorem
2	
  seconds	
  later…	
  
BrowserCache
GatewayCache
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
GET /foo
C-C: max-age=10	
  
Lor <esi:include />
GET /bar
C-C: max-age=5	
  
Lorem
GET /foo
C-C: max-age=10	
  
Lor Lorem
7	
  seconds	
  later…	
  
WRONG	
  
Cache-Control: max-age=10
Cache-Control: s-maxage=10
BrowserCache
GatewayCache
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /bar HTTP/1.1
Host: foo.org
Lorem	
  
ipsum	
  
dolor	
  	
  
<esi:include	
  
src="hYp.."	
  />	
  
HTTP/1.1 200 OK
C-C: s-maxage=10
Lorem	
  ipsum	
  
dolor	
  
HTTP/1.1 200 OK
C-C: s-maxage=5
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
GET /foo
C-C: s-maxage=10	
  
Lor <esi:include />
GET /bar
C-C: s-maxage=5	
  
Lorem
BrowserCache
GatewayCache
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
GET /foo
C-C: s-maxage=10	
  
Lor <esi:include />
GET /bar
C-C: s-maxage=5	
  
Lorem
2	
  seconds	
  later…	
  
BrowserCache
GatewayCache
Browser
YourPHPapplication
GET /foo HTTP/1.1
Host: foo.org
GET /foo HTTP/1.1
Host: foo.org
GET /bar HTTP/1.1
Host: foo.org
Lorem	
  ipsum	
  
dolor	
  
HTTP/1.1 200 OK
C-C: s-maxage=5
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
Lorem	
  ipsum	
  
dolor	
  sit	
  
amet,	
  	
  
Lorem	
  
ipsum	
  
dolor	
  
HTTP/1.1 200 OK
GET /foo
C-C: s-maxage=10	
  
Lor <esi:include />
GET /bar
C-C: s-maxage=5	
  
Lorem
7	
  seconds	
  later…	
  
Lorem	
  ipsum	
  dolor	
  sit	
  
amet,	
  consectetur	
  
adipiscing	
  elit.	
  In	
  vel	
  nulla	
  
arcu,	
  vitae	
  cursus	
  nunc.	
  
Integer	
  semper	
  turpis	
  et	
  
enim	
  porRtor	
  iaculis.	
  
Nulla	
  facilisi.	
  Lorem	
  ipsum	
  
dolor	
  sit	
  amet,	
  
consectetur	
  adipiscing	
  elit.	
  
Mauris	
  vehicula	
  
ves4bulum	
  dictum.	
  
Aenean	
  non	
  velit	
  tortor.	
  
Nullam	
  adipiscing	
  
malesuada	
  aliquam.	
  
Mauris	
  dignissim,	
  urna	
  
quis	
  iaculis	
  tempus,	
  justo	
  
libero	
  porRtor	
  est,	
  nec	
  
eleifend	
  est	
  elit	
  vitae	
  ante.	
  
Curabitur	
  interdum	
  luctus	
  
metus.	
  
Lorem	
  ipsum	
  dolor	
  sit	
  amet,	
  
consectetur	
  adipiscing	
  elit.	
  In	
  vel	
  nulla	
  
arcu,	
  vitae	
  cursus	
  nunc.	
  Integer	
  semper	
  
turpis	
  et	
  enim	
  porRtor	
  iaculis.	
  Nulla	
  
facilisi.	
  Lorem	
  ipsum	
  dolor	
  sit	
  amet,	
  
consectetur	
  adipiscing	
  elit.	
  Mauris	
  
vehicula	
  ves4bulum	
  dictum.	
  Aenean	
  
non	
  velit	
  tortor.	
  Nullam	
  adipiscing	
  
malesuada	
  aliquam.	
  Mauris	
  dignissim,	
  
urna	
  quis	
  iaculis	
  tempus,	
  justo	
  libero	
  
porRtor	
  est,	
  nec	
  eleifend	
  est	
  elit	
  vitae	
  
ante.	
  Curabitur	
  interdum	
  luctus	
  metus,	
  
in	
  pulvinar	
  lectus	
  rutrum	
  sit	
  amet.	
  Duis	
  
gravida,	
  metus	
  in	
  dictum	
  eleifend,	
  
dolor	
  risus	
  4ncidunt	
  ligula,	
  non	
  
volutpat	
  nulla	
  sapien	
  in	
  elit.	
  Nulla	
  
rutrum	
  erat	
  id	
  neque	
  suscipit	
  eu	
  
ultricies	
  odio	
  sollicitudin.	
  Aliquam	
  a	
  mi	
  
vel	
  eros	
  placerat	
  hendrerit.	
  Phasellus	
  
porRtor,	
  augue	
  sit	
  amet	
  vulputate	
  
venena4s,	
  dui	
  leo	
  commodo	
  odio,	
  a	
  
euismod	
  turpis	
  ligula	
  in	
  elit.	
  	
  
Lorem	
  ipsum	
  dolor	
  sit	
  
amet,	
  consectetur	
  
adipiscing	
  elit.	
  In	
  vel	
  
nulla	
  arcu,	
  vitae	
  cursus	
  
nunc.	
  Integer	
  semper	
  
turpis	
  et	
  enim	
  porRtor	
  
iaculis.	
  Nulla	
  facilisi.	
  
Lorem	
  ipsum	
  dolor	
  sit	
  
amet,	
  consectetur	
  
adipiscing	
  elit.	
  Mauris	
  
vehicula	
  ves4bulum	
  
dictum.	
  Aenean	
  non	
  
velit	
  tortor.	
  Nullam	
  
adipiscing	
  malesuada	
  
aliquam.	
  	
  
Lorem	
  ipsum	
  dolor	
  sit	
  
amet,	
  consectetur	
  
adipiscing	
  elit.	
  In	
  vel	
  
nulla	
  arcu,	
  vitae	
  
cursus	
  nunc.	
  	
  
main template
layout
base layout
included page
included page
included page
sub vcl_fetch {
esi;
}
varnishlog -i TxURL
varnishlog –o VCL_call hit | grep RxURL
Make your application work when ESI is enabled
and when it’s not
And only parse for ESIs when it’s needed
Surrogate-Capability
Surrogate-Control
http://www.w3.org/TR/edge-arch
Surrogate-Capability: abc="Surrogate/1.0 ESI/1.0"
Surrogate-Control: content="ESI/1.0"
RewriteEngine On
RewriteRule ^(.*)$ index.php [QSA,L]
sub vcl_recv {
set req.http.Surrogate-Capability = "abc=ESI/1.0";
}
sub vcl_fetch {
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
unset beresp.http.Surrogate-Control;
esi;
}
}
function has_surrogate_ESI_capability()
{
return isset($_SERVER['HTTP_SURROGATE_CAPABILITY'])
&& preg_match('#ESI/1.0#',
$_SERVER['HTTP_SURROGATE_CAPABILITY']);
}
function add_surrogate_control()
{
header('Surrogate-Control: content="ESI/1.0"');
}
function include($path)
{
if (has_surrogate_ESI_capability()) {
$path = get_absolute_path($path);
echo '<esi:include src="'.$path.'" />';
add_surrogate_control();
} else {
include dirname(__FILE__).'/'.$path;
}
}
Imagine the power when you combine
ESI, expiration, validation, max-age, s-maxage, …
Why would you want to reinvent the wheel?
and implement your own caching system?
Because you cannot afford to useVarnish?
Because you use a shared hosting company?
<?php
// With the Symfony2 HTTP accelerator instead of Varnish
// won't work if you use exit() for instance...
// won't work if you have "global" state
// This is just a skeleton to get you started
// Of course, this is native with Symfony2 ;)
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpKernelHttpKernelInterface;
use SymfonyComponentHttpKernelCacheCache;
use SymfonyComponentHttpKernelCacheEsi;
use SymfonyComponentHttpKernelCacheStore;
class AppKernel implements HttpKernelInterface
{
public function handle(Request $request = null, $type
= HttpKernelInterface::MASTER_REQUEST, $raw = false)
{
return new Response($content, 200, $headers);
}
public function getRequest() {}
}
$_SERVER['HTTP_SURROGATE_CAPABILITY'] = 'symfony2="ESI/1.0"';
// quick hack... not secure at all!
$base = str_replace('index.php', '', $request->getScriptName());
$script = str_replace($base, '', $request->getRequestUri());
ob_start();
include __DIR__.'/'.$script;
$content = ob_get_clean();
$headers = array();
foreach (headers_list() as $header) {
$elements = explode(':', $header, 2);
$headers[$elements[0]] = trim($elements[1]);
}
// do not deal with response others than 200
// implementation depends on your code
return new Response($content, 200, $headers);
$kernel = new AppKernel();
$store = new Store('/path/to/http_cache');
$esi = new Esi();
$cache = new Cache($kernel, $store, $esi);
$cache->handle()->send();
error_log($cache->getLog());
Goal
Be as dynamic as needed
Hit the application as less as possible
The power is even bigger than what you think…
<esi:include
src="http://...”
alt="http://...”
onerror="continue" />
stale-while-revalidate - rfc5861
stale-if-error - rfc5861
ThereareonlytwohardthingsinComputerScience:
cacheinvalidationandnamingthings. --PhilKarlton
http://martinfowler.com/bliki/TwoHardThings.html
PURGE:
varnishadm -T localhost:6082 purgeurl "^/images/ »
acl purge_acl {
"localhost";
"1.2.3.4";
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge_acl) {
error 405 "Not allowed.";
}
purge_url(req.url);
error 200 "Purged.";
}
return(lookup);
}
curl -X PURGE http://...
WARNING
Don't use GZIP with ESI
asVarnish won't be able to assemble the page

Más contenido relacionado

La actualidad más candente

Platform as a Service (PaaS) - A cloud service for Developers
Platform as a Service (PaaS) - A cloud service for Developers Platform as a Service (PaaS) - A cloud service for Developers
Platform as a Service (PaaS) - A cloud service for Developers Ravindra Dastikop
 
Security in cloud computing
Security in cloud computingSecurity in cloud computing
Security in cloud computingveena venugopal
 
Cyber security for an organization
Cyber security for an organizationCyber security for an organization
Cyber security for an organizationTejas Wasule
 
Introduction to IoT Architectures and Protocols
Introduction to IoT Architectures and ProtocolsIntroduction to IoT Architectures and Protocols
Introduction to IoT Architectures and ProtocolsAbdullah Alfadhly
 
cloud storage ppt
cloud storage pptcloud storage ppt
cloud storage pptNaga Dinesh
 
CONTENT DELIVERY NETWORK
CONTENT DELIVERY NETWORK CONTENT DELIVERY NETWORK
CONTENT DELIVERY NETWORK Saif Muttair
 
Fundamentals of IoT Security
Fundamentals of IoT SecurityFundamentals of IoT Security
Fundamentals of IoT SecuritySHAAMILIVARSAGV
 
What Is Cloud Computing? | Cloud Computing For Beginners | Cloud Computing Tr...
What Is Cloud Computing? | Cloud Computing For Beginners | Cloud Computing Tr...What Is Cloud Computing? | Cloud Computing For Beginners | Cloud Computing Tr...
What Is Cloud Computing? | Cloud Computing For Beginners | Cloud Computing Tr...Simplilearn
 
An introduction to cyber forensics and open source tools in cyber forensics
An introduction to cyber forensics and open source tools in cyber forensicsAn introduction to cyber forensics and open source tools in cyber forensics
An introduction to cyber forensics and open source tools in cyber forensicsZyxware Technologies
 
Node.js and The Internet of Things
Node.js and The Internet of ThingsNode.js and The Internet of Things
Node.js and The Internet of ThingsLosant
 
Virtualization Explained | What Is Virtualization Technology? | Virtualizatio...
Virtualization Explained | What Is Virtualization Technology? | Virtualizatio...Virtualization Explained | What Is Virtualization Technology? | Virtualizatio...
Virtualization Explained | What Is Virtualization Technology? | Virtualizatio...Simplilearn
 
Operating system vulnerability and control
Operating system vulnerability and control Operating system vulnerability and control
Operating system vulnerability and control أحلام انصارى
 

La actualidad más candente (20)

Cloud computing ppt
Cloud computing pptCloud computing ppt
Cloud computing ppt
 
Platform as a Service (PaaS) - A cloud service for Developers
Platform as a Service (PaaS) - A cloud service for Developers Platform as a Service (PaaS) - A cloud service for Developers
Platform as a Service (PaaS) - A cloud service for Developers
 
Security in cloud computing
Security in cloud computingSecurity in cloud computing
Security in cloud computing
 
Cyber security for an organization
Cyber security for an organizationCyber security for an organization
Cyber security for an organization
 
DoS or DDoS attack
DoS or DDoS attackDoS or DDoS attack
DoS or DDoS attack
 
Introduction to IoT Architectures and Protocols
Introduction to IoT Architectures and ProtocolsIntroduction to IoT Architectures and Protocols
Introduction to IoT Architectures and Protocols
 
cloud storage ppt
cloud storage pptcloud storage ppt
cloud storage ppt
 
Building converged plantwide ethernet architectures
Building converged plantwide ethernet architecturesBuilding converged plantwide ethernet architectures
Building converged plantwide ethernet architectures
 
CONTENT DELIVERY NETWORK
CONTENT DELIVERY NETWORK CONTENT DELIVERY NETWORK
CONTENT DELIVERY NETWORK
 
Fundamentals of IoT Security
Fundamentals of IoT SecurityFundamentals of IoT Security
Fundamentals of IoT Security
 
What Is Cloud Computing? | Cloud Computing For Beginners | Cloud Computing Tr...
What Is Cloud Computing? | Cloud Computing For Beginners | Cloud Computing Tr...What Is Cloud Computing? | Cloud Computing For Beginners | Cloud Computing Tr...
What Is Cloud Computing? | Cloud Computing For Beginners | Cloud Computing Tr...
 
An introduction to cyber forensics and open source tools in cyber forensics
An introduction to cyber forensics and open source tools in cyber forensicsAn introduction to cyber forensics and open source tools in cyber forensics
An introduction to cyber forensics and open source tools in cyber forensics
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Node.js and The Internet of Things
Node.js and The Internet of ThingsNode.js and The Internet of Things
Node.js and The Internet of Things
 
IoT Security
IoT SecurityIoT Security
IoT Security
 
Honeypots
HoneypotsHoneypots
Honeypots
 
Virtualization Explained | What Is Virtualization Technology? | Virtualizatio...
Virtualization Explained | What Is Virtualization Technology? | Virtualizatio...Virtualization Explained | What Is Virtualization Technology? | Virtualizatio...
Virtualization Explained | What Is Virtualization Technology? | Virtualizatio...
 
Operating system vulnerability and control
Operating system vulnerability and control Operating system vulnerability and control
Operating system vulnerability and control
 
Http Protocol
Http ProtocolHttp Protocol
Http Protocol
 
Fog Computing
Fog ComputingFog Computing
Fog Computing
 

Destacado

research article Professional #social media instrument to meet researcher and...
research article Professional #social media instrument to meet researcher and...research article Professional #social media instrument to meet researcher and...
research article Professional #social media instrument to meet researcher and...M. Luisetto Pharm.D.Spec. Pharmacology
 
PSFK presents Future Of Health
PSFK presents Future Of HealthPSFK presents Future Of Health
PSFK presents Future Of HealthPSFK
 
Strategies for promoting health
Strategies for promoting healthStrategies for promoting health
Strategies for promoting healthG Masso
 
Community Health Nursing (complete)
Community Health Nursing (complete)Community Health Nursing (complete)
Community Health Nursing (complete)MarkFredderickAbejo
 
Understanding Mental Health and Mental Illness
Understanding Mental Health and Mental IllnessUnderstanding Mental Health and Mental Illness
Understanding Mental Health and Mental IllnessTeenMentalHealth.org
 
Nutrition: Food, Nutrition and Health
Nutrition: Food, Nutrition and HealthNutrition: Food, Nutrition and Health
Nutrition: Food, Nutrition and HealthBates2ndQuarterLPN
 

Destacado (9)

Cache memory presentation
Cache memory presentationCache memory presentation
Cache memory presentation
 
Dec 2012 NLE TIPS MS (A)
Dec 2012 NLE TIPS MS (A)Dec 2012 NLE TIPS MS (A)
Dec 2012 NLE TIPS MS (A)
 
research article Professional #social media instrument to meet researcher and...
research article Professional #social media instrument to meet researcher and...research article Professional #social media instrument to meet researcher and...
research article Professional #social media instrument to meet researcher and...
 
PSFK presents Future Of Health
PSFK presents Future Of HealthPSFK presents Future Of Health
PSFK presents Future Of Health
 
Strategies for promoting health
Strategies for promoting healthStrategies for promoting health
Strategies for promoting health
 
Community Health Nursing (complete)
Community Health Nursing (complete)Community Health Nursing (complete)
Community Health Nursing (complete)
 
Understanding Mental Health and Mental Illness
Understanding Mental Health and Mental IllnessUnderstanding Mental Health and Mental Illness
Understanding Mental Health and Mental Illness
 
Nutrition: Food, Nutrition and Health
Nutrition: Food, Nutrition and HealthNutrition: Food, Nutrition and Health
Nutrition: Food, Nutrition and Health
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar a Caching on the Edge

Under the Covers with the Web
Under the Covers with the WebUnder the Covers with the Web
Under the Covers with the WebTrevor Lohrbeer
 
Web Site Optimization
Web Site OptimizationWeb Site Optimization
Web Site OptimizationSunil Patil
 
Web site optimization
Web site optimizationWeb site optimization
Web site optimizationSunil Patil
 
PHP Training: Module 1
PHP Training: Module 1PHP Training: Module 1
PHP Training: Module 1hussulinux
 
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."Dongwook Lee
 
Devoxx Maroc 2015 HTTP 1, HTTP 2 and folks
Devoxx Maroc  2015 HTTP 1, HTTP 2 and folksDevoxx Maroc  2015 HTTP 1, HTTP 2 and folks
Devoxx Maroc 2015 HTTP 1, HTTP 2 and folksNicolas Martignole
 
DEF CON 27- ALBINOWAX - http desync attacks
DEF CON 27- ALBINOWAX - http desync attacksDEF CON 27- ALBINOWAX - http desync attacks
DEF CON 27- ALBINOWAX - http desync attacksFelipe Prado
 
Caching the Uncacheable
Caching the UncacheableCaching the Uncacheable
Caching the Uncacheabledanrot
 
REST in peace @ IPC 2012 in Mainz
REST in peace @ IPC 2012 in MainzREST in peace @ IPC 2012 in Mainz
REST in peace @ IPC 2012 in MainzAlessandro Nadalin
 
Web II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksWeb II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksRandy Connolly
 
Hidden Gems in HTTP
Hidden Gems in HTTPHidden Gems in HTTP
Hidden Gems in HTTPBen Ramsey
 
Interactive web. O rly?
Interactive web. O rly?Interactive web. O rly?
Interactive web. O rly?timbc
 
Computer network (10)
Computer network (10)Computer network (10)
Computer network (10)NYversity
 
HTTP colon slash slash: the end of the road?
HTTP colon slash slash: the end of the road?HTTP colon slash slash: the end of the road?
HTTP colon slash slash: the end of the road?Alessandro Nadalin
 

Similar a Caching on the Edge (20)

Http caching basics
Http caching basicsHttp caching basics
Http caching basics
 
Under the Covers with the Web
Under the Covers with the WebUnder the Covers with the Web
Under the Covers with the Web
 
Web Site Optimization
Web Site OptimizationWeb Site Optimization
Web Site Optimization
 
Web site optimization
Web site optimizationWeb site optimization
Web site optimization
 
PHP Training: Module 1
PHP Training: Module 1PHP Training: Module 1
PHP Training: Module 1
 
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
 
5-WebServers.ppt
5-WebServers.ppt5-WebServers.ppt
5-WebServers.ppt
 
Devoxx Maroc 2015 HTTP 1, HTTP 2 and folks
Devoxx Maroc  2015 HTTP 1, HTTP 2 and folksDevoxx Maroc  2015 HTTP 1, HTTP 2 and folks
Devoxx Maroc 2015 HTTP 1, HTTP 2 and folks
 
DEF CON 27- ALBINOWAX - http desync attacks
DEF CON 27- ALBINOWAX - http desync attacksDEF CON 27- ALBINOWAX - http desync attacks
DEF CON 27- ALBINOWAX - http desync attacks
 
Caching the Uncacheable
Caching the UncacheableCaching the Uncacheable
Caching the Uncacheable
 
API Design Workshop
API Design WorkshopAPI Design Workshop
API Design Workshop
 
Varnish
VarnishVarnish
Varnish
 
REST in peace @ IPC 2012 in Mainz
REST in peace @ IPC 2012 in MainzREST in peace @ IPC 2012 in Mainz
REST in peace @ IPC 2012 in Mainz
 
Web II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksWeb II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET Works
 
Hidden Gems in HTTP
Hidden Gems in HTTPHidden Gems in HTTP
Hidden Gems in HTTP
 
HTTP
HTTPHTTP
HTTP
 
Interactive web. O rly?
Interactive web. O rly?Interactive web. O rly?
Interactive web. O rly?
 
Computer network (10)
Computer network (10)Computer network (10)
Computer network (10)
 
Http2 kotlin
Http2   kotlinHttp2   kotlin
Http2 kotlin
 
HTTP colon slash slash: the end of the road?
HTTP colon slash slash: the end of the road?HTTP colon slash slash: the end of the road?
HTTP colon slash slash: the end of the road?
 

Más de Fabien Potencier

Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Fabien Potencier
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Fabien Potencier
 
Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Fabien Potencier
 
The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010Fabien Potencier
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010Fabien Potencier
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201Fabien Potencier
 
Caching on the Edge with Symfony2
Caching on the Edge with Symfony2Caching on the Edge with Symfony2
Caching on the Edge with Symfony2Fabien Potencier
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Fabien Potencier
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 WorldFabien Potencier
 
Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Fabien Potencier
 
Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Fabien Potencier
 

Más de Fabien Potencier (20)

Look beyond PHP
Look beyond PHPLook beyond PHP
Look beyond PHP
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 
Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3
 
The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010
 
PhpBB meets Symfony2
PhpBB meets Symfony2PhpBB meets Symfony2
PhpBB meets Symfony2
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201
 
Caching on the Edge with Symfony2
Caching on the Edge with Symfony2Caching on the Edge with Symfony2
Caching on the Edge with Symfony2
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
 
Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Symfony Components
Symfony ComponentsSymfony Components
Symfony Components
 
PHP 5.3 in practice
PHP 5.3 in practicePHP 5.3 in practice
PHP 5.3 in practice
 
Symfony2 revealed
Symfony2 revealedSymfony2 revealed
Symfony2 revealed
 
Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3
 

Último

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 

Último (20)

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 

Caching on the Edge

  • 1. Speaker | Company Title // Presentation Name Caching on the Edge Fabien Potencier
  • 4. March 1999 RFC2616 – HTTP/1.1 http://www.ietf.org/rfc/rfc2616.txt
  • 5. HTTP Bis –Work in progress http://tools.ietf.org/wg/httpbis/ http://www.flickr.com/photos/visualpanic/233508614
  • 6.  p1: Messaging: Low-level message parsing and connection management  p2: Semantics: Methods, status codes and headers  p3: Payload: Dealing with content, message-specific headers  p4: Conditional Requests: e.g., If-Modified-Since  p5: Range Requests: Getting partial content  p6: Caching: Browser and intermediary caches  p7: Authentication: HTTP authentication framework
  • 8. The Client sends a Request to the Server The Server sends back a Response to the Client A Client (browser, bot,WS, curl, …) A Server (Apache, nginx, …) The Request and the Response are HTTP messages
  • 9. Let’s play with HTTP and HTTP headers
  • 10. GET / HTTP/1.1 Host: http.trainings.sensiolabs.com
  • 11. « The request-header fields allow the client to pass additional information about the request, and about the client itself, to the server.These fields act as request modifiers, with semantics equivalent to the parameters on a programming language method invocation. »
  • 12. « Each HTTP header field consists of a case-insensitive field name followed by a colon (":"), optional whitespace, and the field value »
  • 13. HTTP/1.1 200 OK Date: Wed, 15 Oct 2005 07:07:07 GMT Server: Apache Content-Length: 14 Content-Type: text/html Hello World!
  • 14. Live HTTP headers Firefox extension http://livehttpheaders.mozdev.org/
  • 15. $ curl -i http://http.trainings.sensiolabs.com/ HTTP/1.1 200 OK Date: Wed, 15 Oct 2005 07:07:07 GMT Server: Apache Content-Length: 14 Content-Type: text/html Hello World!
  • 16. $ telnet localhost 80 Trying ::1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.1 Host: http.trainings.sensiolabs.com HTTP/1.1 200 OK Date: Wed, 15 Oct 2005 07:07:07 GMT Server: Apache Content-Length: 14 Content-Type: text/html Hello World! Connection closed by foreign host.
  • 19. Caching in the HTTP Specification
  • 20.  p1: Messaging: Low-level message parsing and connection management  p2: Semantics: Methods, status codes and headers  p3: Payload: Dealing with content, message-specific headers  p4: Conditional Requests: e.g., If-Modified-Since  p5: Range Requests: Getting partial content  p6: Caching: Browser and intermediary caches  p7: Authentication: HTTP authentication framework
  • 22. HTTP Headers for Expiration Cache-Control Expires
  • 23. HTTP Headers forValidation Last-Modified / If-Modified-Since ETag / If-None-Match
  • 24. HTTP Cache headers only work with“safe”HTTP methods (like GET & HEAD)
  • 25. Never change the state of the server when serving a GET request (we are talking about the application’s state of course, you can log, cache, …)
  • 27. Expires « The "Expires" header field gives the date/time after which the response is considered stale. »
  • 28. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org HTTP/1.1 200 OK Expires: Thu,  01  Dec  …   Hello GET /foo HTTP/1.1 200 OK Expires: Thu,  01  Dec   …   Hello HTTP/1.1 200 OK Expires: Thu,  01  Dec  …   Hello
  • 29. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 200 OK Expires: Thu,  01  Dec   …   Hello HTTP/1.1 200 OK Expires: Thu,  01  Dec  …   Hello Before  expira4on   Your  applica4on  is  not  called  
  • 30. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org HTTP/1.1 200 OK Expires: Thu,  01  Dec  …   Hello GET /foo HTTP/1.1 200 OK Expires: Thu,  01  Dec   …   Hello HTTP/1.1 200 OK Expires: Thu,  01  Dec  …   Hello A<er  expira4on   Not  fresh  
  • 31. Expires: Thu, 01 Dec 2010 16:00:00 GMT Date in RFC1123 format, not RFC2822 (timezone always GMT, which is ~ UTC)
  • 32. $expires = gmdate('D, j M Y H:i:s T', time() + 5); header('Expires: '.$expires);
  • 33. $date = new DateTime(null, new DateTimeZone('UTC')); $date->modify('+5 seconds'); $expires = $date->format('D, d M Y H:i:s').' GMT'; $date = gmdate('D, j M Y H:i:s T', time() + 5); header('Expires: '.$expires);
  • 36. WARNING The clocks on theWeb server AND the cache (aka browser) MUST be synchronised
  • 37. WARNING « HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future. »
  • 38. Mostly useful to make static assets (images, css, js, …) cacheable with an extremely long expiry time But there is a better way!
  • 42. Don’t use Expires Use Cache-Control …except if you want to set a date, which should be pretty rare
  • 46. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 200 OK ETag: abcdef   …   Hello HTTP/1.1 200 OK ETag: abcdef   Hello HTTP/1.1 200 OK ETag: abcdef   Hello
  • 47. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org If-None-Match: abcdef HTTP/1.1 304 Not Modified GET /foo HTTP/1.1 200 OK ETag: abcdef   …   Hello HTTP/1.1 200 OK ETag: abcdef   Hello If  the  resource  has  not  changed  
  • 48. SomeCache GET /foo HTTP/1.1 200 OK ETag: abcdef   …   Hello Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org If-None-Match: abcdef If  the  resource  has  changed   GET /foo HTTP/1.1 200 OK ETag: 123456   …   Hello HTTP/1.1 200 OK ETag: 123456   Hello HTTP/1.1 200 OK ETag: 123456   Hello
  • 49. // compute ETag value $etag = '...'; if ( isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag ) { header('HTTP/1.1 304 Not Modified'); } else { header('ETag: '.$etag); echo 'Hello'; }
  • 50. This is a simple but naïve implementation…
  • 52. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 200 OK Last-Modified: Thu,     …   Hello HTTP/1.1 200 OK Last-Modified: Thu,  …   Hello HTTP/1.1 200 OK Last-Modified: Thu,  …   Hello
  • 53. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org If-Modified-Since: Thu, HTTP/1.1 304 Not Modified GET /foo HTTP/1.1 200 OK Last-Modified: Thu,     …   Hello HTTP/1.1 200 OK Last-Modified: Thu,  …   Hello If  the  resource  has  not  changed  
  • 54. SomeCache GET /foo HTTP/1.1 200 OK Last-Modified: Thu, …   Hello GET /foo HTTP/1.1 200 OK Last-Modified: Sun, …   Hello Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org If-Modified-Since: Thu, If  the  resource  has  changed   HTTP/1.1 200 OK Last-Modified: Sun,  …   Hello HTTP/1.1 200 OK Last-Modified: Sun,  …   Hello
  • 56. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 200 OK Etag: abcdef Cache-­‐Control:  max-­‐ age=10   …   Hello HTTP/1.1 200 OK ETag: abcdef   Cache-­‐Control:  max-­‐age=10   Hello HTTP/1.1 200 OK ETag: abcdef   Cache-­‐Control:  max-­‐age=10   Hello
  • 57. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org Before  expira4on   Your  applica4on  is  not  called   GET /foo HTTP/1.1 200 OK Etag: abcdef Cache-­‐Control:  max-­‐ age=10   …   Hello HTTP/1.1 200 OK ETag: abcdef   Cache-­‐Control:  max-­‐age=10   Hello
  • 58. Browser YourPHPapplication SomeCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org If-None-Match: abcdef HTTP/1.1 304 Not Modified Cache-­‐Control:  max-­‐age=10   A<er  expira4on   but  resource  s4ll  valid   GET /foo HTTP/1.1 200 OK Etag: abcdef Cache-­‐Control:  max-­‐ age=10   …   Hello HTTP/1.1 200 OK ETag: abcdef   Cache-­‐Control:  max-­‐age=10   Hello
  • 59. You can combine HTTP headers the way you want Expiration wins overValidation
  • 60. Expiration allows you to scale as less requests hit your server (and client speed is better too) Validation saves bandwidth
  • 61. The goal is to never generate the same response twice
  • 62. PHP and Cache HTTP headers
  • 65. Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
  • 68. On  the  server  side   Browser YourPHPapplication BrowserCache
  • 69. Within  a  Company   On  the  server  side   Browser YourPHPapplication BrowserBrowser BrowserCacheBrowserCacheBrowserCache
  • 71. On  the  server  side   Within  a  Company   Browser YourPHPapplication BrowserBrowser BrowserCacheBrowserCacheBrowserCache ProxyCache
  • 72. On  the  server  side   Within  a  Company   Browser YourPHPapplication BrowserBrowser BrowserCacheBrowserCacheBrowserCache ProxyCache Within  a  Company   BrowserBrowserBrowser BrowserCacheBrowserCacheBrowserCache ProxyCache
  • 74. On  the  server  side   YourPHPapplication Browser BrowserCache GatewayCache
  • 75. Within  a  Company   On  the  server  side   Browser YourPHPapplication BrowserBrowser BrowserCacheBrowserCacheBrowserCache GatewayCache
  • 76. On  the  server  side   Within  a  Company   Browser YourPHPapplication BrowserBrowser BrowserCacheBrowserCacheBrowserCache ProxyCache BrowserBrowser BrowserCacheBrowserCache GatewayCache
  • 77. Your PHP application Gateway Cache Reverse Proxy Cache Surrogate Cache HTTP Accelerator Browser Browser Cache Proxy Cache
  • 78. Browser Cache Local cache for when you hit“back”or when images are reused throughout a website
  • 79. Proxy Cache A shared cache Many people behind a single proxy Installed by large corporations and ISPs Reduce latency and network traffic
  • 80. Gateway Cache A shared cache on the server side Installed by network administrators Make websites more scalable, reliable and performing better CDNs like Akaïma are gateway caches
  • 81. Today, we will mainly talk about Gateway caches
  • 84. HTTP 1.1 allows caching anything by default unless explicit Cache-Control header
  • 85. In practice, most caches avoid anything with Cache-Control Cookie / Set-Cookie WWW-Authenticate / Authorization POST / PUT 302 / 307 status codes
  • 86. Cache-Control: private This is the default with PHP when you have a session Browser cache will still work fine here (public means shared caches, private means browser cache)
  • 87. A gateway cache won't cache anything "private" or carrying a cookie In a real-world: tracking cookies (Google Analytics)
  • 88. All proxies do caching based on the same HTTP headers
  • 89. We need several clients to understand how it works and how the cached is shared
  • 91. GatewayCache Bob YourPHPapplication Bob’sCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 200 OK C-C: max-age=600   …   Hello HTTP/1.1 200 OK C-C: max-age=600   Hello HTTP/1.1 200 OK C-C: max-age=600   Hello GET /foo HTTP/1.1 200 OK C-C: max-age=600   …   Hello HTTP/1.1 200 OK C-C: max-age=600   Hello
  • 92. Bob’sCache GatewayCache Bob YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 200 OK C-C: max-age=600   …   Hello HTTP/1.1 200 OK C-C: max-age=600   Hello GET /foo HTTP/1.1 200 OK C-C: max-age=600   …   Hello
  • 93. BrowserCache Alice YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org HTTP/1.1 200 OK C-C: max-age=600   Hello HTTP/1.1 200 OK C-C: max-age=600   Hello GatewayCache GET /foo HTTP/1.1 200 OK C-C: max-age=600   …   Hello GET /foo HTTP/1.1 200 OK C-C: max-age=600   …   Hello
  • 95. Bob’sCache GatewayCache Bob YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 200 OK Etag: abcde   …   Hello HTTP/1.1 200 OK Etag: abcde   Hello HTTP/1.1 200 OK Etag: abcde   Hello GET /foo HTTP/1.1 200 OK Etag: abcde   …   Hello HTTP/1.1 200 OK Etag: abcde   Hello
  • 96. Bob’sCache GatewayCache Bob YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org If-None-Match: ab GET /foo HTTP/1.1 Host: foo.org If-None-Match: ab GET /foo HTTP/1.1 200 OK Etag: ab   …   Hello HTTP/1.1 200 OK Etag: ab   Hello GET /foo HTTP/1.1 200 OK Etag: ab   …   Hello 304 Not Modified304 Not Modified
  • 97. GatewayCache YourPHPapplication BrowserCache GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org If-None-Match: ab GET /foo HTTP/1.1 200 OK Etag: ab   …   Hello HTTP/1.1 200 OK Etag: ab   Hello 304 Not Modified Alice HTTP/1.1 200 OK Etag: ab   Hello GET /foo HTTP/1.1 200 OK Etag: ab   …   Hello
  • 98. Gateway caches Varnish (only does that and tries to do it well) Squid (just one way to use it) mod_cache (Apache)
  • 99. Gateway caches are a great way to make your website performs better
  • 100. But what if you cannot cache whole pages? What if a page has "more" dynamic parts?
  • 101. Lorem  ipsum  dolor  sit   amet,  consectetur   adipiscing  elit.  In  vel  nulla   arcu,  vitae  cursus  nunc.   Integer  semper  turpis  et   enim  porRtor  iaculis.   Nulla  facilisi.  Lorem  ipsum   dolor  sit  amet,   consectetur  adipiscing  elit.   Mauris  vehicula   ves4bulum  dictum.   Aenean  non  velit  tortor.   Nullam  adipiscing   malesuada  aliquam.   Mauris  dignissim,  urna   quis  iaculis  tempus,  justo   libero  porRtor  est,  nec   eleifend  est  elit  vitae  ante.   Curabitur  interdum  luctus   metus.   Lorem  ipsum  dolor  sit  amet,  consectetur   adipiscing  elit.  In  vel  nulla  arcu,  vitae   cursus  nunc.  Integer  semper  turpis  et  enim   porRtor  iaculis.  Nulla  facilisi.  Lorem  ipsum   dolor  sit  amet,  consectetur  adipiscing  elit.   Mauris  vehicula  ves4bulum  dictum.   Aenean  non  velit  tortor.  Nullam  adipiscing   malesuada  aliquam.  Mauris  dignissim,  urna   quis  iaculis  tempus,  justo  libero  porRtor   est,  nec  eleifend  est  elit  vitae  ante.   Curabitur  interdum  luctus  metus,  in   pulvinar  lectus  rutrum  sit  amet.  Duis   gravida,  metus  in  dictum  eleifend,  dolor   risus  4ncidunt  ligula,  non  volutpat  nulla   sapien  in  elit.  Nulla  rutrum  erat  id  neque   suscipit  eu  ultricies  odio  sollicitudin.   Aliquam  a  mi  vel  eros  placerat  hendrerit.   Phasellus  porRtor,  augue  sit  amet   vulputate  venena4s,  dui  leo  commodo   odio,  a  euismod  turpis  ligula  in  elit.     cacheable for 10 seconds cacheable for 5 seconds
  • 102. ESI… or Edge Side Includes http://www.w3.org/TR/esi-lang
  • 103. Lorem  ipsum  dolor  sit   amet,  consectetur   adipiscing  elit.  In  vel  nulla   arcu,  vitae  cursus  nunc.   Integer  semper  turpis  et   enim  porRtor  iaculis.   Nulla  facilisi.  Lorem  ipsum   dolor  sit  amet,   consectetur  adipiscing  elit.   Mauris  vehicula   ves4bulum  dictum.   Aenean  non  velit  tortor.   Nullam  adipiscing   malesuada  aliquam.   Mauris  dignissim,  urna   quis  iaculis  tempus,  justo   libero  porRtor  est,  nec   eleifend  est  elit  vitae  ante.   Curabitur  interdum  luctus   metus.   Lorem  ipsum  dolor  sit  amet,  consectetur   adipiscing  elit.  In  vel  nulla  arcu,  vitae   cursus  nunc.  Integer  semper  turpis  et  enim   porRtor  iaculis.  Nulla  facilisi.  Lorem  ipsum   dolor  sit  amet,  consectetur  adipiscing  elit.   Mauris  vehicula  ves4bulum  dictum.   Aenean  non  velit  tortor.  Nullam  adipiscing   malesuada  aliquam.  Mauris  dignissim,  urna   quis  iaculis  tempus,  justo  libero  porRtor   est,  nec  eleifend  est  elit  vitae  ante.   Curabitur  interdum  luctus  metus,  in   pulvinar  lectus  rutrum  sit  amet.  Duis   gravida,  metus  in  dictum  eleifend,  dolor   risus  4ncidunt  ligula,  non  volutpat  nulla   sapien  in  elit.  Nulla  rutrum  erat  id  neque   suscipit  eu  ultricies  odio  sollicitudin.   Aliquam  a  mi  vel  eros  placerat  hendrerit.   Phasellus  porRtor,  augue  sit  amet   vulputate  venena4s,  dui  leo  commodo   odio,  a  euismod  turpis  ligula  in  elit.    
  • 104. Lorem  ipsum  dolor  sit  amet,  consectetur   adipiscing  elit.  In  vel  nulla  arcu,  vitae   cursus  nunc.  Integer  semper  turpis  et  enim   porRtor  iaculis.  Nulla  facilisi.  Lorem  ipsum   dolor  sit  amet,  consectetur  adipiscing  elit.   Mauris  vehicula  ves4bulum  dictum.   Aenean  non  velit  tortor.  Nullam  adipiscing   malesuada  aliquam.  Mauris  dignissim,  urna   quis  iaculis  tempus,  justo  libero  porRtor   est,  nec  eleifend  est  elit  vitae  ante.   Curabitur  interdum  luctus  metus,  in   pulvinar  lectus  rutrum  sit  amet.  Duis   gravida,  metus  in  dictum  eleifend,  dolor   risus  4ncidunt  ligula,  non  volutpat  nulla   sapien  in  elit.  Nulla  rutrum  erat  id  neque   suscipit  eu  ultricies  odio  sollicitudin.   Aliquam  a  mi  vel  eros  placerat  hendrerit.   Phasellus  porRtor,  augue  sit  amet   vulputate  venena4s,  dui  leo  commodo   odio,  a  euismod  turpis  ligula  in  elit.     <esi:include src="..." />
  • 106. BrowserCache GatewayCache Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /bar HTTP/1.1 Host: foo.org Lorem   ipsum   dolor     <esi:include   src="hYp.."  />   HTTP/1.1 200 OK Lorem  ipsum   dolor   HTTP/1.1 200 OK Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK
  • 107. BrowserCache GatewayCache Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /bar HTTP/1.1 Host: foo.org Lorem   ipsum   dolor     <esi:include   src="hYp.."  />   HTTP/1.1 200 OK C-C: max-age=10 Lorem  ipsum   dolor   HTTP/1.1 200 OK C-C: max-age=5 Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK GET /foo C-C: max-age=10   Lor <esi:include /> GET /bar C-C: max-age=5   Lorem GET /foo C-C: max-age=10   Lor Lorem
  • 108. BrowserCache GatewayCache Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK GET /foo C-C: max-age=10   Lor <esi:include /> GET /bar C-C: max-age=5   Lorem GET /foo C-C: max-age=10   Lor Lorem 2  seconds  later…  
  • 109. BrowserCache GatewayCache Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK GET /foo C-C: max-age=10   Lor <esi:include /> GET /bar C-C: max-age=5   Lorem GET /foo C-C: max-age=10   Lor Lorem 7  seconds  later…   WRONG  
  • 111. BrowserCache GatewayCache Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /bar HTTP/1.1 Host: foo.org Lorem   ipsum   dolor     <esi:include   src="hYp.."  />   HTTP/1.1 200 OK C-C: s-maxage=10 Lorem  ipsum   dolor   HTTP/1.1 200 OK C-C: s-maxage=5 Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK GET /foo C-C: s-maxage=10   Lor <esi:include /> GET /bar C-C: s-maxage=5   Lorem
  • 112. BrowserCache GatewayCache Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK GET /foo C-C: s-maxage=10   Lor <esi:include /> GET /bar C-C: s-maxage=5   Lorem 2  seconds  later…  
  • 113. BrowserCache GatewayCache Browser YourPHPapplication GET /foo HTTP/1.1 Host: foo.org GET /foo HTTP/1.1 Host: foo.org GET /bar HTTP/1.1 Host: foo.org Lorem  ipsum   dolor   HTTP/1.1 200 OK C-C: s-maxage=5 Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK Lorem  ipsum   dolor  sit   amet,     Lorem   ipsum   dolor   HTTP/1.1 200 OK GET /foo C-C: s-maxage=10   Lor <esi:include /> GET /bar C-C: s-maxage=5   Lorem 7  seconds  later…  
  • 114. Lorem  ipsum  dolor  sit   amet,  consectetur   adipiscing  elit.  In  vel  nulla   arcu,  vitae  cursus  nunc.   Integer  semper  turpis  et   enim  porRtor  iaculis.   Nulla  facilisi.  Lorem  ipsum   dolor  sit  amet,   consectetur  adipiscing  elit.   Mauris  vehicula   ves4bulum  dictum.   Aenean  non  velit  tortor.   Nullam  adipiscing   malesuada  aliquam.   Mauris  dignissim,  urna   quis  iaculis  tempus,  justo   libero  porRtor  est,  nec   eleifend  est  elit  vitae  ante.   Curabitur  interdum  luctus   metus.   Lorem  ipsum  dolor  sit  amet,   consectetur  adipiscing  elit.  In  vel  nulla   arcu,  vitae  cursus  nunc.  Integer  semper   turpis  et  enim  porRtor  iaculis.  Nulla   facilisi.  Lorem  ipsum  dolor  sit  amet,   consectetur  adipiscing  elit.  Mauris   vehicula  ves4bulum  dictum.  Aenean   non  velit  tortor.  Nullam  adipiscing   malesuada  aliquam.  Mauris  dignissim,   urna  quis  iaculis  tempus,  justo  libero   porRtor  est,  nec  eleifend  est  elit  vitae   ante.  Curabitur  interdum  luctus  metus,   in  pulvinar  lectus  rutrum  sit  amet.  Duis   gravida,  metus  in  dictum  eleifend,   dolor  risus  4ncidunt  ligula,  non   volutpat  nulla  sapien  in  elit.  Nulla   rutrum  erat  id  neque  suscipit  eu   ultricies  odio  sollicitudin.  Aliquam  a  mi   vel  eros  placerat  hendrerit.  Phasellus   porRtor,  augue  sit  amet  vulputate   venena4s,  dui  leo  commodo  odio,  a   euismod  turpis  ligula  in  elit.     Lorem  ipsum  dolor  sit   amet,  consectetur   adipiscing  elit.  In  vel   nulla  arcu,  vitae  cursus   nunc.  Integer  semper   turpis  et  enim  porRtor   iaculis.  Nulla  facilisi.   Lorem  ipsum  dolor  sit   amet,  consectetur   adipiscing  elit.  Mauris   vehicula  ves4bulum   dictum.  Aenean  non   velit  tortor.  Nullam   adipiscing  malesuada   aliquam.     Lorem  ipsum  dolor  sit   amet,  consectetur   adipiscing  elit.  In  vel   nulla  arcu,  vitae   cursus  nunc.     main template layout base layout included page included page included page
  • 116. varnishlog -i TxURL varnishlog –o VCL_call hit | grep RxURL
  • 117. Make your application work when ESI is enabled and when it’s not And only parse for ESIs when it’s needed
  • 122. sub vcl_recv { set req.http.Surrogate-Capability = "abc=ESI/1.0"; } sub vcl_fetch { if (beresp.http.Surrogate-Control ~ "ESI/1.0") { unset beresp.http.Surrogate-Control; esi; } }
  • 123. function has_surrogate_ESI_capability() { return isset($_SERVER['HTTP_SURROGATE_CAPABILITY']) && preg_match('#ESI/1.0#', $_SERVER['HTTP_SURROGATE_CAPABILITY']); }
  • 125. function include($path) { if (has_surrogate_ESI_capability()) { $path = get_absolute_path($path); echo '<esi:include src="'.$path.'" />'; add_surrogate_control(); } else { include dirname(__FILE__).'/'.$path; } }
  • 126. Imagine the power when you combine ESI, expiration, validation, max-age, s-maxage, …
  • 127. Why would you want to reinvent the wheel? and implement your own caching system?
  • 128. Because you cannot afford to useVarnish? Because you use a shared hosting company?
  • 129. <?php // With the Symfony2 HTTP accelerator instead of Varnish // won't work if you use exit() for instance... // won't work if you have "global" state // This is just a skeleton to get you started // Of course, this is native with Symfony2 ;)
  • 130. use SymfonyComponentHttpFoundationResponse; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpKernelHttpKernelInterface; use SymfonyComponentHttpKernelCacheCache; use SymfonyComponentHttpKernelCacheEsi; use SymfonyComponentHttpKernelCacheStore; class AppKernel implements HttpKernelInterface { public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false) { return new Response($content, 200, $headers); } public function getRequest() {} }
  • 131. $_SERVER['HTTP_SURROGATE_CAPABILITY'] = 'symfony2="ESI/1.0"'; // quick hack... not secure at all! $base = str_replace('index.php', '', $request->getScriptName()); $script = str_replace($base, '', $request->getRequestUri()); ob_start(); include __DIR__.'/'.$script; $content = ob_get_clean(); $headers = array(); foreach (headers_list() as $header) { $elements = explode(':', $header, 2); $headers[$elements[0]] = trim($elements[1]); } // do not deal with response others than 200 // implementation depends on your code return new Response($content, 200, $headers);
  • 132. $kernel = new AppKernel(); $store = new Store('/path/to/http_cache'); $esi = new Esi(); $cache = new Cache($kernel, $store, $esi); $cache->handle()->send(); error_log($cache->getLog());
  • 133. Goal Be as dynamic as needed Hit the application as less as possible
  • 134. The power is even bigger than what you think…
  • 139. PURGE: varnishadm -T localhost:6082 purgeurl "^/images/ »
  • 140. acl purge_acl { "localhost"; "1.2.3.4"; } sub vcl_recv { if (req.request == "PURGE") { if (!client.ip ~ purge_acl) { error 405 "Not allowed."; } purge_url(req.url); error 200 "Purged."; } return(lookup); } curl -X PURGE http://...
  • 141. WARNING Don't use GZIP with ESI asVarnish won't be able to assemble the page