113
HTTP cache Alessandro Nadalin March, 29th 2011

HTTP cache @ PUG Rome 03-29-2011

Embed Size (px)

DESCRIPTION

A short presentation about HTTP caching at the Rome PHP User Group.

Citation preview

Page 1: HTTP cache @ PUG Rome 03-29-2011

HTTP cache

Alessandro Nadalin

March, 29th 2011

Page 2: HTTP cache @ PUG Rome 03-29-2011

The Webis

inconsistent

Page 3: HTTP cache @ PUG Rome 03-29-2011

get over it

Page 4: HTTP cache @ PUG Rome 03-29-2011

Why?

Page 5: HTTP cache @ PUG Rome 03-29-2011

Because it scales to billions of users

Page 6: HTTP cache @ PUG Rome 03-29-2011

Because it scales to billions of users

WWW data traffic won't be possible without being

inconsistent

Page 7: HTTP cache @ PUG Rome 03-29-2011

How does it scales?

Page 8: HTTP cache @ PUG Rome 03-29-2011

Cache

Page 9: HTTP cache @ PUG Rome 03-29-2011

What is caching?

Page 10: HTTP cache @ PUG Rome 03-29-2011

Storing data so that future requests for that data can be served faster

( general purpose )

Page 11: HTTP cache @ PUG Rome 03-29-2011

Process of storing copies of your most accessed resources near to a/some client/s

( the WWW )

Page 12: HTTP cache @ PUG Rome 03-29-2011

Caching is not saving a webpage, or a fragment of it, on the disk, to serve it to the next client

Page 13: HTTP cache @ PUG Rome 03-29-2011

Caching is not saving a webpage, or a fragment of it, on the disk, to serve it to the next client

This is a caching mechanism

Page 14: HTTP cache @ PUG Rome 03-29-2011

Disk-persisted or in-memory

Page 15: HTTP cache @ PUG Rome 03-29-2011

Can be distributed

Page 16: HTTP cache @ PUG Rome 03-29-2011

Can be distributed with a NoSQL database

Page 17: HTTP cache @ PUG Rome 03-29-2011

Cache implementation can be really funny

Page 18: HTTP cache @ PUG Rome 03-29-2011

Don't reinvent the wheel

Page 19: HTTP cache @ PUG Rome 03-29-2011

Goals

Page 20: HTTP cache @ PUG Rome 03-29-2011

Goals

evolve

Page 21: HTTP cache @ PUG Rome 03-29-2011

Goals

evolveloose coupling

Page 22: HTTP cache @ PUG Rome 03-29-2011

Goals

Work lessevolve

loose coupling

Page 23: HTTP cache @ PUG Rome 03-29-2011

Evolve Because you want your platform to extensible

Page 24: HTTP cache @ PUG Rome 03-29-2011

Evolve Because you want your platform to extensible

Loose coupling Because you want it to be easy to integrate with, evolve, plug and mantain

Page 25: HTTP cache @ PUG Rome 03-29-2011

Evolve Because you want your platform to extensible

Loose coupling Because you want it to be easy to integrate with, evolve, plug and mantain

Work less

Because every LoC is bug-prone and our man-day is a hard to scale cost

Page 26: HTTP cache @ PUG Rome 03-29-2011

enters our Hero #1

Page 27: HTTP cache @ PUG Rome 03-29-2011
Page 28: HTTP cache @ PUG Rome 03-29-2011
Page 29: HTTP cache @ PUG Rome 03-29-2011

enters our Hero #2

Page 30: HTTP cache @ PUG Rome 03-29-2011
Page 31: HTTP cache @ PUG Rome 03-29-2011

http://www.lullabot.com/articles/a-beginners-guide-to-caching-data

Page 32: HTTP cache @ PUG Rome 03-29-2011

Goals

Work lessevolve

loose coupling

Page 33: HTTP cache @ PUG Rome 03-29-2011

Goals

Work lessevolve

loose coupling

Page 34: HTTP cache @ PUG Rome 03-29-2011

everything is done for us!

:)

but....

Page 35: HTTP cache @ PUG Rome 03-29-2011

tmp files, cache tables, procedural crap...

mmmmh....

gotta be something better

Page 36: HTTP cache @ PUG Rome 03-29-2011

Frameworks

Page 37: HTTP cache @ PUG Rome 03-29-2011
Page 38: HTTP cache @ PUG Rome 03-29-2011

Cache is used for compiling routes, autoloading, ...

Page 39: HTTP cache @ PUG Rome 03-29-2011

Cache is used for compiling routes, autoloading, ...

...but also for storing the view

Page 40: HTTP cache @ PUG Rome 03-29-2011

Goals

Work lessevolve

loose coupling

Page 41: HTTP cache @ PUG Rome 03-29-2011

Goals

Work lessevolve

loose coupling

Page 42: HTTP cache @ PUG Rome 03-29-2011

at least because we use a framework

Page 43: HTTP cache @ PUG Rome 03-29-2011

but, probably

Page 44: HTTP cache @ PUG Rome 03-29-2011

PHP sucks

Page 45: HTTP cache @ PUG Rome 03-29-2011

Let's call a hero, from the East

Page 46: HTTP cache @ PUG Rome 03-29-2011

on Rails

Page 47: HTTP cache @ PUG Rome 03-29-2011
Page 48: HTTP cache @ PUG Rome 03-29-2011

Goals

Work lessevolve

loose coupling

Page 49: HTTP cache @ PUG Rome 03-29-2011

gosh :-(

Page 50: HTTP cache @ PUG Rome 03-29-2011

hey, what aboutthe latest

products?

Page 51: HTTP cache @ PUG Rome 03-29-2011

both Symfony2 andRuby's Rack embrace

Page 52: HTTP cache @ PUG Rome 03-29-2011

HTTP

Page 53: HTTP cache @ PUG Rome 03-29-2011

(here comes the nice part)

Page 54: HTTP cache @ PUG Rome 03-29-2011

Local

Shared/proxy

Shared/reverse proxy

Page 55: HTTP cache @ PUG Rome 03-29-2011

Local

Shared/proxy

Shared/reverse proxy

Page 56: HTTP cache @ PUG Rome 03-29-2011
Page 57: HTTP cache @ PUG Rome 03-29-2011

Local

Shared/proxy

Shared/reverse proxy

Page 58: HTTP cache @ PUG Rome 03-29-2011
Page 59: HTTP cache @ PUG Rome 03-29-2011

Local

Shared/proxy

Shared/reverse proxy

Page 60: HTTP cache @ PUG Rome 03-29-2011
Page 61: HTTP cache @ PUG Rome 03-29-2011

on the server side

Page 62: HTTP cache @ PUG Rome 03-29-2011

Proxy Reverse proxy

http://www.codeproject.com/KB/aspnet/ExploringCaching.aspx

Page 63: HTTP cache @ PUG Rome 03-29-2011

There is something cool about caching softwares.

Page 64: HTTP cache @ PUG Rome 03-29-2011

Free as free beer

Page 65: HTTP cache @ PUG Rome 03-29-2011

...but the server should take advantage of them

( the boring part were you need to write code )

Page 66: HTTP cache @ PUG Rome 03-29-2011

and here are a few ways to do so, using

Page 67: HTTP cache @ PUG Rome 03-29-2011

and here are a few ways to do so, using

expiration

Page 68: HTTP cache @ PUG Rome 03-29-2011

and here are a few ways to do so, using

expiration

validation

Page 69: HTTP cache @ PUG Rome 03-29-2011

and here are a few ways to do so, using

expiration

validation

invalidation

Page 70: HTTP cache @ PUG Rome 03-29-2011

and here are a few ways to do so, using

expiration

validation

invalidationwe will se why

Page 71: HTTP cache @ PUG Rome 03-29-2011

Expiration

Page 72: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comExpires: 0

Page 73: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comExpires: 0

Page 74: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comExpires: Tue, 15 Nov 1994 01:00 GMT

Page 75: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comCache-Control: max-age=60, public

Page 76: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comCache-Control: max-age=60, public

Page 77: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comCache-Control: max-age=60, public

Cacheable for 60 seconds

Page 78: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comCache-Control: max-age=60, public

Cacheable by both local and shared caches

Page 79: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comCache-Control: stale-if-error=600, stale-while-revalidate=600

Page 80: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comCache-Control: stale-if-error=600, stale-while-revalidate=600

fault-tolerant

Page 81: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comCache-Control: stale-if-error=600, stale-while-revalidate=600

available during downtime

Page 82: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comCache-Control: stale-if-error=600, stale-while-revalidate=600

available during revalidation

Page 83: HTTP cache @ PUG Rome 03-29-2011

Validation

Page 84: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comEtag: 1234

Page 85: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comEtag: 1234

an identifier for your response

Page 86: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comIf-None-Match: 1234

the browsers asks you if it has been modified

Conditional requests

Page 87: HTTP cache @ PUG Rome 03-29-2011

Relax

Page 88: HTTP cache @ PUG Rome 03-29-2011

Calculating an Etag is cheaper than generating a full MVC response

Page 89: HTTP cache @ PUG Rome 03-29-2011

HTTP/1.1 304 Not Modified

Page 90: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comLast-Modified: Tue, 15 Jan 2011 12:00:00 GMT

Page 91: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comLast-Modified: Tue, 15 Jan 2011 12:00:00 GMT

tell the client about the latest change

Page 92: HTTP cache @ PUG Rome 03-29-2011

GET / HTTP/1.1Host: www.example.comIf-Modified-Since: Tue, 15 Jan 2011 12:00:00 GMT

the client asks you if it has been modified since the last time

Conditional requests

Page 93: HTTP cache @ PUG Rome 03-29-2011

Relax

Page 94: HTTP cache @ PUG Rome 03-29-2011

Calculating a date is cheaper than retrieving an entire object

Page 95: HTTP cache @ PUG Rome 03-29-2011

HTTP/1.1 304 Not Modified

Page 96: HTTP cache @ PUG Rome 03-29-2011

Invalidation

Page 97: HTTP cache @ PUG Rome 03-29-2011

The web is not meant for invalidating data.

Server should not be able to keep clients' state, otherwise they wont scale well.

That's why long-polling and endless connections haven't had big success dealing with caching.

Page 98: HTTP cache @ PUG Rome 03-29-2011

but hey, you say

Page 99: HTTP cache @ PUG Rome 03-29-2011

HTTP's cache fails when dealing with really dynamic pages, because consumers will always have to hit the origin server, although a part of the page would be cacheable ( header and

footer, for example )

Page 100: HTTP cache @ PUG Rome 03-29-2011

Nope

Nope

Page 101: HTTP cache @ PUG Rome 03-29-2011

ESI was built for thathttp://www.w3.org/TR/esi-lang

Page 102: HTTP cache @ PUG Rome 03-29-2011

and hey, Varnish is a reverse proxy implementing what you need of the ESI specification

take 2, pay for 1

Page 103: HTTP cache @ PUG Rome 03-29-2011

So what does HTTP cache is meant to solve?

Page 104: HTTP cache @ PUG Rome 03-29-2011

Less work

Page 105: HTTP cache @ PUG Rome 03-29-2011

because the hard work is delegated to the browser/proxy

http://www.flickr.com/photos/snakphotography/5004775320/sizes/o/in/photostream/

Page 106: HTTP cache @ PUG Rome 03-29-2011

evolve

Page 107: HTTP cache @ PUG Rome 03-29-2011

because cache is abstracted from the application

Page 108: HTTP cache @ PUG Rome 03-29-2011

loose coupling

Page 109: HTTP cache @ PUG Rome 03-29-2011

because caching is bound to the protocol, HTTP, not to your implementation ( Sf, RoR, Django )

Page 110: HTTP cache @ PUG Rome 03-29-2011

The presentations lacks on technical stuff

So here are a few resource for who wants documentation:

http://tomayko.com/writings/things-caches-dohttp://www.slideshare.net/fabpot/caching-on-the-edgehttp://www.odino.org/301/rest-better-http-cachehttp://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

Page 111: HTTP cache @ PUG Rome 03-29-2011

Deep version of the same talk,about Sf2 cache internals and ESI

Page 112: HTTP cache @ PUG Rome 03-29-2011

May, 12/14 2011 in Verona

Early bid ends on April, 6th

Page 113: HTTP cache @ PUG Rome 03-29-2011

Thanks!

Alessandro Nadalin@_odino_ odino.org