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

Like this? Share it with your network

Share

HTTP's Best-Kept Secret: Caching

on

  • 25,637 views

 

Statistics

Views

Total Views
25,637
Views on SlideShare
24,225
Embed Views
1,412

Actions

Likes
38
Downloads
478
Comments
1

23 Embeds 1,412

http://en.oreilly.com 819
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://207.46.192.232 4
http://wew.bsdmap.com 4
http://httpwww.bsdmap.com 4
http://web.mail.bsdmap.com 3
http://yaoge123blogwww.bsdmap.com 2
http://translate.googleusercontent.com 2
http://www.linkedin.com 2
https://twitter.com 2
http://w3ww.bsdmap.com 2
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
http://ww.bsdmap.com 1
http://static.slidesharecdn.com 1
https://www.linkedin.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 Presentation Transcript

  • 1. Caching HTTP’s Best Kept Secret
  • 2. Ryan Tomayko http://tomayko.com/
  • 3. Sinatra http://www.sinatrarb.com
  • 4. Rack http://rack.rubyforge.org
  • 5. Rack::Cache http://tomayko.com/src/rack-cache
  • 6. Heroku http://heroku.com
  • 7. HTTP Caching?
  • 8. NOT Rails Caching
  • 9. 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
  • 10. 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
  • 11. Types of Caches
  • 12. Client Cache Client Client Client Cache Cache Cache foo.com
  • 13. Shared Proxy Cache Shared Proxy Cache foo.com
  • 14. Shared Proxy Cache Cache Cache foo.com
  • 15. Shared Proxy Cache
  • 16. Gateway Cache foo.com Gateway Cache Backend
  • 17. Caches Everywhere Client Client Cache Cache Shared Cache foo.com Gateway Cache Backend
  • 18. Why Cache?
  • 19. November 1990
  • 20. November 1990 Web Population: 1
  • 21. February 1996 Web Population: 20M
  • 22. February 1996 Modem Speed: 28.8kbps
  • 23. February 1996
  • 24. February 1996
  • 25. Bandwidth.
  • 26. February 1996 RFC 1945 - HTTP/1.0
  • 27. March 1999 RFC 2616 - HTTP/1.1
  • 28. Today
  • 29. Expiration
  • 30. Gateway Cache Alice Backend
  • 31. Gateway Cache Alice Backend GET /foo Host: foo.com
  • 32. Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com
  • 33. Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK Cache-Control:public,max-age=60
  • 34. 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
  • 35. Gateway Cache Bob (30 seconds later) Backend
  • 36. Gateway Cache Bob (30 seconds later) Backend GET /foo Host: foo.com
  • 37. Gateway Cache Bob (30 seconds later) Backend GET /foo Host: foo.com 200OK Cache-Control:public,max-age=60 Age:30
  • 38. Gateway Cache Carol (60 seconds later) Backend
  • 39. Gateway Cache Carol (60 seconds later) Backend GET /foo Host: foo.com
  • 40. Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com
  • 41. Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK Cache-Control:public,max-age=60
  • 42. 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
  • 43. expires_in class FooController < Application def show expires_in 60.seconds, :public => true @foo = Foo.find(params[:id]) render :action => 'show' end end
  • 44. Sinatra get '/foo' do headers['Cache-Control'] = 'public, max-age=60' @foo = Foo.find_and_stu erb :foo end
  • 45. Validation (Conditional GET)
  • 46. Gateway Cache Alice Backend
  • 47. Gateway Cache Alice Backend GET /foo Host: foo.com
  • 48. Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com
  • 49. Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK ETag:quot;abcdef012345quot;
  • 50. Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK 200OK ETag:quot;abcdef012345quot; ETag:quot;abcdef012345quot;
  • 51. Gateway Cache Bob Backend
  • 52. Gateway Cache Bob Backend GET /foo Host: foo.com
  • 53. Gateway Cache Bob Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345
  • 54. Gateway Cache Bob Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345 304NotModified
  • 55. Gateway Cache Bob Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345 304NotModified 200OK ETag:abcdef012345
  • 56. 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
  • 57. 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
  • 58. Sinatra: etag get '/foo' do @foo = Foo.find(params[:id]) etag @foo.etag erb :foo end
  • 59. Expiration + Validation
  • 60. Gateway Cache Alice Backend
  • 61. Gateway Cache Alice Backend GET /foo Host: foo.com
  • 62. Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com
  • 63. Gateway Cache Alice Backend GET /foo GET /foo Host: foo.com Host: foo.com 200OK Cache-Control:public,max-age=60 ETag:abcdef012345
  • 64. 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
  • 65. Gateway Cache Bob (30 seconds later) Backend
  • 66. Gateway Cache Bob (30 seconds later) Backend GET /foo Host: foo.com
  • 67. Gateway Cache Bob (30 seconds later) Backend GET /foo Host: foo.com 200OK Cache-Control:public,max-age=60 ETag:abcdef012345 Age:30
  • 68. Gateway Cache Carol (60 seconds later) Backend
  • 69. Gateway Cache Carol (60 seconds later) Backend GET /foo Host: foo.com
  • 70. Gateway Cache Carol (60 seconds later) Backend GET /foo GET /foo Host: foo.com Host: foo.com If-None-Match: abcdef012345
  • 71. 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
  • 72. 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
  • 73. Never Generate The Same Response Twice
  • 74. 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
  • 75. High Performance Caches/Accelerators •Squid http://www.squid-cache.org/ • Varnish http://varnish.projects.linpro.no/
  • 76. Heroku
  • 77. Thanks!