Rails
Need short bursts of speed?
I’m a thief



•   http://railslab.newrelic.com/
Perception
Communication:
          Two parts


•   Server speed
•   Browser speed
Browser speed


•   Firebug / Safari developer
•   YSlow
•   Different parts of a site
Webpage parts

•   Page itself (<html> ... )
•   Additional files (CSS, JavaScript)
•   Images
•   ... generating dynamic content
Siteparts


•   10 requests:10 times slower as one request
•   stylesheet_tag :defaults, ‘screen’, :cache => ‘all’
images


•   asset hosts
•   Content Delivery Network
Page Caching
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)...
implementation
result
expire_page
expires-result
conclusion: page cache


•   very simple
•   limited scope to apply
•   cache invalidation! Important!
Cache Expiry
Why not expire_page in
     the action?

•   Not DRY (update, create, destroy,...)
•   after_filter possible, but other controllers
    might need to expire the cache
after_filter
•   PostsController (clear_posts_cache)
•   CommentsController (clear_posts_cache)
sweeper


•   observer on controller and model
    •   if saved, call clear_posts_cache
    •   if deleted, call clear_posts_cache
how
Page caching - part 2



•   Ajax Callbacks for dynamic data
login/logout



•   pain!
Action Caching
Action cache

•   Cached output stored
•   Filters are processed before the cached
    version is returned to the browser
•   Example: authenticatie
caches_action
action cache


•   Filter is executed before sending the reply
•   Output is the same for everyone!
Welcome <username>

•   content is cached
•   layout is rendered
Warning! Attention!
•   fetch needed data with a before_filter
•   action is not executed!
Conditional AC

•   caching only on certain conditions
Action Caching

•   when?
    •   execute code on each hit (authentication)
    •   result remains the same
•   very easy to implement
Fragment Caching
Fragment caching


•   Cache part of a page
•   Logical blocks are candidates
Example
Fragment Caching
No useless actions
Fragment Cache expiry



•   expire_fragment
PC/AC Storage
Page cache storage
•   on disk
Storage: action /
          fragment cache
•   memory (default) or syncronised memory
•   file on disk
•   drb
•   memcache (normal or compressed)
•   custom_store (not hard to impelement!)
DYI
DYI caching


•   Caching hard parts in the code
•   Only spend cycles when really needed
Client-side caching



•   The cloud is there, use it
Three tags


•   max-age
•   etag
•   last_modified
max-age
etag


•   rails has this embedded
•   MD5(body) => etag
•   304 Not Modified
etag


•   Beware, the complete parsing is done!
•   Less bandwidth / traffic
•   Client load time faster
etag
last_modified
Scary technology?


•   memcached
•   reverse proxy’s
Memcached

•   Daemon
•   Is a big hash in memory
•   Rails has default routines for this
•   Hoster / setup should support this
Reverse Proxy’s


•   a proxy, but on the server side
•   controle over expiry: max_age + etag
•   expiry remains a pain
Rack::Cache

•   gem install rack-cache
•   rails 2.3 needed!
Need for speed?

•   Database (n+1, joins / includes)
•   Disk IO
•   Implementing caching
•   Browser caching
•   External components (CDN, memcached, ...)

Caching your rails application