HTTP's Best-Kept Secret: Caching
Upcoming SlideShare
Loading in...5
×
 

HTTP's Best-Kept Secret: Caching

on

  • 25,062 views

 

Statistics

Views

Total Views
25,062
Views on SlideShare
23,653
Embed Views
1,409

Actions

Likes
37
Downloads
474
Comments
1

22 Embeds 1,409

http://en.oreilly.com 817
http://www.bsdmap.com 454
http://www.slideshare.net 82
http://dancroak.com 10
http://cache.baidu.com 9
http://www.scoop.it 5
http://wew.bsdmap.com 4
http://httpwww.bsdmap.com 4
http://207.46.192.232 4
http://web.mail.bsdmap.com 3
http://w3ww.bsdmap.com 2
http://www.linkedin.com 2
https://twitter.com 2
http://yaoge123blogwww.bsdmap.com 2
http://translate.googleusercontent.com 2
http://static.slidesharecdn.com 1
http://ww.bsdmap.com 1
http://awww.bsdmap.com 1
http://webcache.googleusercontent.com 1
http://http.bsdmap.com 1
http://www.bsdmap.com. 1
http://www.zhuaxia.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • IT is a good PPT to explain web caching theory.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • Pure ruby HTTP cache implementation. <br /> This talk is not really about Rack::Cache. <br />
  • Heroku understands HTTP caching. <br />
  • What we're talking about when we say HTTP caching. <br /> There's so many different caching systems. <br />
  • Page caching, action caching, fragment caching, SQL caching, memcached. <br />
  • *This* is what we're talking about <br /> Wire level <br /> Declarative. <br /> Don't worry if this doesn't look familiar. <br />
  • <br />
  • All caches adhere to the same basic rules for the most part. <br />
  • Or browser cache. <br /> People are most familiar with. When we think about HTTP caching, this is what comes to mind. <br /> Bandwidth/Traffic Reduction. <br /> Number of Clients served by the Cache. <br /> I don’t want to talk about Client caches. <br />
  • Many users behind a single cache <br />
  • <br />
  • <br />
  • Also Known As “Reverse Proxy Cache” <br />
  • <br />
  • The reasons have changed over time. <br />
  • <br />
  • First server, client/browser, and web page <br /> Things are good for, like, a year. <br /> Ramble about research guys trading papers and linking to each other. <br />
  • Explosive <br /> Netscape goes public in 1995 <br /> <br />
  • State of the art <br /> Roughly 2.3KB/s <br /> Today, yahoo.com homepage is 388K - 2m48s <br />
  • Other things: CGI just starting out. (Guestbooks, hit counters, search) <br /> JavaScript - didn’t exist. <br /> So what was the most important issue to solve? <br />
  • <br />
  • Expires <br /> Last-Modified <br />
  • Cache-Control <br /> ETag <br />
  • Much more worried about load on backends. <br /> Do less work. <br /> <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • I use ETag and If-None-Match. Last-Modified and If-Modified-Since. <br />
  • I use ETag and If-None-Match. Last-Modified and If-Modified-Since. <br />
  • I use ETag and If-None-Match. Last-Modified and If-Modified-Since. <br />
  • I use ETag and If-None-Match. Last-Modified and If-Modified-Since. <br />
  • <br />
  • <br />
  • Halts <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • Requires Rails 2.3+ for Rack/middleware support. <br />
  • <br />
  • <br />
  • <br />

HTTP's Best-Kept Secret: Caching HTTP's Best-Kept Secret: Caching Presentation Transcript

  • Caching HTTP’s Best Kept Secret
  • Ryan Tomayko http://tomayko.com/
  • Sinatra http://www.sinatrarb.com
  • Rack http://rack.rubyforge.org
  • Rack::Cache http://tomayko.com/src/rack-cache
  • Heroku http://heroku.com
  • HTTP Caching?
  • NOT Rails Caching
  • GET /foo HTTP/1.1 Host: www.foo.com User-Agent: FooBrowser/1.0 Cache-Control: max-age=0 If-Modified-Since: Mon, 01 Jan 1979 ... If-None-Match: abcdef0123456789 Accept: * HTTP/1.1 200 OK Content-Type: text/html Content-Length: 24 Cache-Control: max-age=300 Last-Modified: Mon, 02 Jan 1979 ... ETag: abcdef0123456789 Vary: Accept
  • GET /foo HTTP/1.1 Host: www.foo.com User-Agent: FooBrowser/1.0 Cache-Control: max-age=0 If-Modified-Since: Mon, 01 Jan 1979 ... If-None-Match: abcdef0123456789 Accept: * HTTP/1.1 200 OK Content-Type: text/html Content-Length: 24 Cache-Control: public, max-age=300 Last-Modified: Mon, 02 Jan 1979 ... ETag: abcdef0123456789 Vary: Accept
  • Types of Caches
  • Client Cache Client Client Client Cache Cache Cache foo.com
  • Shared Proxy Cache Shared Proxy Cache foo.com
  • Shared Proxy Cache Cache Cache foo.com
  • Shared Proxy Cache
  • Gateway Cache foo.com Gateway Cache Backend
  • Caches Everywhere Client Client Cache Cache Shared Cache foo.com Gateway Cache Backend
  • Why Cache?
  • November 1990
  • November 1990 Web Population: 1
  • February 1996 Web Population: 20M
  • February 1996 Modem Speed: 28.8kbps
  • February 1996
  • February 1996
  • Bandwidth.
  • February 1996 RFC 1945 - HTTP/1.0
  • March 1999 RFC 2616 - HTTP/1.1
  • Today
  • Expiration
  • Gateway Cache Alice Backend
  • Gateway Cache Alice Backend GET /foo Host: foo.com
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK Cache-Control:public,max-age=60
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK 200OK Cache-Control:public,max-age=60 Cache-Control:public,max-age=60
  • Gateway Cache Bob (30 seconds later) Backend
  • Gateway Cache Bob (30 seconds later) Backend GET /foo Host: foo.com
  • Gateway Cache Bob (30 seconds later) Backend GET /foo Host: foo.com 200OK Cache-Control:public,max-age=60 Age:30
  • Gateway Cache Carol (60 seconds later) Backend
  • Gateway Cache Carol (60 seconds later) Backend GET /foo Host: foo.com
  • Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com
  • Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK Cache-Control:public,max-age=60
  • Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK 200OK Cache-Control:public,max-age=60 Cache-Control:public,max-age=60
  • expires_in class FooController < Application def show expires_in 60.seconds, :public => true @foo = Foo.find(params[:id]) render :action => 'show' end end
  • Sinatra get '/foo' do headers['Cache-Control'] = 'public, max-age=60' @foo = Foo.find_and_stu erb :foo end
  • Validation (Conditional GET)
  • Gateway Cache Alice Backend
  • Gateway Cache Alice Backend GET /foo Host: foo.com
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK ETag:quot;abcdef012345quot;
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK 200OK ETag:quot;abcdef012345quot; ETag:quot;abcdef012345quot;
  • Gateway Cache Bob Backend
  • Gateway Cache Bob Backend GET /foo Host: foo.com
  • Gateway Cache Bob Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345
  • Gateway Cache Bob Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345 304NotModified
  • Gateway Cache Bob Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345 304NotModified 200OK ETag:abcdef012345
  • Rails: fresh_when class FooController < Application def show @foo = Foo.find(params[:id]) fresh_when :etag => @foo, :last_modified => @foo.updated_at.utc end end
  • Rails: stale? class FooController < Application def show @foo = Foo.find(params[:id]) modified = @foo.updated_at.utc if stale?(:etag => @foo, :last_modified => modified) respond_to do |wants| # ... normal response processing end end end end
  • Sinatra: etag get '/foo' do @foo = Foo.find(params[:id]) etag @foo.etag erb :foo end
  • Expiration + Validation
  • Gateway Cache Alice Backend
  • Gateway Cache Alice Backend GET /foo Host: foo.com
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK Cache-Control:public,max-age=60 ETag:abcdef012345
  • Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK 200OK Cache-Control:public,max-age=60 Cache-Control:public,max-age=60 ETag:abcdef012345 ETag:abcdef012345
  • Gateway Cache Bob (30 seconds later) Backend
  • Gateway Cache Bob (30 seconds later) Backend GET /foo Host: foo.com
  • Gateway Cache Bob (30 seconds later) Backend GET /foo Host: foo.com 200OK Cache-Control:public,max-age=60 ETag:abcdef012345 Age:30
  • Gateway Cache Carol (60 seconds later) Backend
  • Gateway Cache Carol (60 seconds later) Backend GET /foo Host: foo.com
  • Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345
  • Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345 304NotModified Cache-Control:public,max-age=60
  • Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345 200OK 304NotModified Cache-Control:public,max-age=60 Cache-Control:public,max-age=60 ETag:abcdef012345
  • Never Generate The Same Response Twice
  • use Rack::Cache $ gem install rack-cache config.middleware.use Rack::Cache, :verbose => true, :metastore => quot;file:/var/cache/rack/metaquot;, :entitystore => quot;file:/var/cache/rack/bodyquot;, :allow_reload => false, :allow_revalidate => false
  • High Performance Caches/Accelerators •Squid http://www.squid-cache.org/ • Varnish http://varnish.projects.linpro.no/
  • Heroku
  • Thanks!