Caching your rails application
-
Upload
arrrrcamp -
Category
Technology
-
view
2.990 -
download
1
description
Transcript of Caching your rails application
![Page 1: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/1.jpg)
RailsNeed short bursts of speed?
![Page 2: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/2.jpg)
I’m a thief
• http://railslab.newrelic.com/
![Page 3: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/3.jpg)
Perception
![Page 4: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/4.jpg)
Communication: Two parts
• Server speed
• Browser speed
![Page 5: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/5.jpg)
Browser speed
• Firebug / Safari developer
• YSlow
• Different parts of a site
![Page 6: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/6.jpg)
Webpage parts
• Page itself (<html> ... )
• Additional files (CSS, JavaScript)
• Images
• ... generating dynamic content
![Page 7: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/7.jpg)
Siteparts
• 10 requests:10 times slower as one request
• stylesheet_tag :defaults, ‘screen’, :cache => ‘all’
![Page 8: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/8.jpg)
![Page 9: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/9.jpg)
images
• asset hosts
• Content Delivery Network
![Page 10: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/10.jpg)
Page Caching
![Page 11: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/11.jpg)
Why?
• Code parsing: 20 to 50 req/second
• Webserver: easily > 100 req/sec
• Webserver stresses less, less database queries
• Why not, it is easy (with rails)...
![Page 12: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/12.jpg)
implementation
![Page 13: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/13.jpg)
result
![Page 14: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/14.jpg)
expire_page
![Page 15: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/15.jpg)
expires-result
![Page 16: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/16.jpg)
conclusion: page cache
• very simple
• limited scope to apply
• cache invalidation! Important!
![Page 17: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/17.jpg)
Cache Expiry
![Page 18: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/18.jpg)
Why not expire_page in the action?
• Not DRY (update, create, destroy,...)
• after_filter possible, but other controllers might need to expire the cache
![Page 19: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/19.jpg)
after_filter• PostsController (clear_posts_cache)
• CommentsController (clear_posts_cache)
![Page 20: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/20.jpg)
sweeper
• observer on controller and model
• if saved, call clear_posts_cache
• if deleted, call clear_posts_cache
![Page 21: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/21.jpg)
how
![Page 22: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/22.jpg)
Page caching - part 2
• Ajax Callbacks for dynamic data
![Page 23: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/23.jpg)
login/logout
• pain!
![Page 24: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/24.jpg)
Action Caching
![Page 25: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/25.jpg)
Action cache
• Cached output stored
• Filters are processed before the cached version is returned to the browser
• Example: authenticatie
![Page 26: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/26.jpg)
caches_action
![Page 27: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/27.jpg)
action cache
• Filter is executed before sending the reply
• Output is the same for everyone!
![Page 28: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/28.jpg)
Welcome <username>
• content is cached
• layout is rendered
![Page 29: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/29.jpg)
Warning! Attention!
• fetch needed data with a before_filter
• action is not executed!
![Page 30: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/30.jpg)
Conditional AC
• caching only on certain conditions
![Page 31: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/31.jpg)
Action Caching
• when?
• execute code on each hit (authentication)
• result remains the same
• very easy to implement
![Page 32: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/32.jpg)
Fragment Caching
![Page 33: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/33.jpg)
Fragment caching
• Cache part of a page
• Logical blocks are candidates
![Page 34: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/34.jpg)
Example
![Page 35: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/35.jpg)
Fragment Caching
![Page 36: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/36.jpg)
No useless actions
![Page 37: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/37.jpg)
Fragment Cache expiry
• expire_fragment
![Page 38: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/38.jpg)
PC/AC Storage
![Page 39: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/39.jpg)
Page cache storage
• on disk
![Page 40: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/40.jpg)
Storage: action / fragment cache
• memory (default) or syncronised memory
• file on disk
• drb
• memcache (normal or compressed)
• custom_store (not hard to impelement!)
![Page 41: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/41.jpg)
DYI
![Page 42: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/42.jpg)
DYI caching
• Caching hard parts in the code
• Only spend cycles when really needed
![Page 43: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/43.jpg)
![Page 44: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/44.jpg)
Client-side caching
• The cloud is there, use it
![Page 45: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/45.jpg)
Three tags
• max-age
• etag
• last_modified
![Page 46: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/46.jpg)
max-age
![Page 47: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/47.jpg)
etag
• rails has this embedded
• MD5(body) => etag
• 304 Not Modified
![Page 48: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/48.jpg)
etag
• Beware, the complete parsing is done!
• Less bandwidth / traffic
• Client load time faster
![Page 49: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/49.jpg)
etag
![Page 50: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/50.jpg)
last_modified
![Page 51: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/51.jpg)
Scary technology?
• memcached
• reverse proxy’s
![Page 52: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/52.jpg)
Memcached
• Daemon
• Is a big hash in memory
• Rails has default routines for this
• Hoster / setup should support this
![Page 53: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/53.jpg)
Reverse Proxy’s
• a proxy, but on the server side
• controle over expiry: max_age + etag
• expiry remains a pain
![Page 54: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/54.jpg)
Rack::Cache
• gem install rack-cache
• rails 2.3 needed!
![Page 55: Caching your rails application](https://reader033.fdocuments.us/reader033/viewer/2022052821/554a3a9cb4c90582328b4b12/html5/thumbnails/55.jpg)
Need for speed?
• Database (n+1, joins / includes)
• Disk IO
• Implementing caching
• Browser caching
• External components (CDN, memcached, ...)