Caching your rails application

55
Rails Need short bursts of speed?

description

Bernard from Openminds talks about caching your Rails appication.

Transcript of Caching your rails application

Page 1: Caching your rails application

RailsNeed short bursts of speed?

Page 2: Caching your rails application

I’m a thief

• http://railslab.newrelic.com/

Page 3: Caching your rails application

Perception

Page 4: Caching your rails application

Communication: Two parts

• Server speed

• Browser speed

Page 5: Caching your rails application

Browser speed

• Firebug / Safari developer

• YSlow

• Different parts of a site

Page 6: Caching your rails application

Webpage parts

• Page itself (<html> ... )

• Additional files (CSS, JavaScript)

• Images

• ... generating dynamic content

Page 7: Caching your rails application

Siteparts

• 10 requests:10 times slower as one request

• stylesheet_tag :defaults, ‘screen’, :cache => ‘all’

Page 8: Caching your rails application
Page 9: Caching your rails application

images

• asset hosts

• Content Delivery Network

Page 10: Caching your rails application

Page Caching

Page 11: Caching your rails application

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

implementation

Page 13: Caching your rails application

result

Page 14: Caching your rails application

expire_page

Page 15: Caching your rails application

expires-result

Page 16: Caching your rails application

conclusion: page cache

• very simple

• limited scope to apply

• cache invalidation! Important!

Page 17: Caching your rails application

Cache Expiry

Page 18: Caching your rails application

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

after_filter• PostsController (clear_posts_cache)

• CommentsController (clear_posts_cache)

Page 20: Caching your rails application

sweeper

• observer on controller and model

• if saved, call clear_posts_cache

• if deleted, call clear_posts_cache

Page 21: Caching your rails application

how

Page 22: Caching your rails application

Page caching - part 2

• Ajax Callbacks for dynamic data

Page 23: Caching your rails application

login/logout

• pain!

Page 24: Caching your rails application

Action Caching

Page 25: Caching your rails application

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

caches_action

Page 27: Caching your rails application

action cache

• Filter is executed before sending the reply

• Output is the same for everyone!

Page 28: Caching your rails application

Welcome <username>

• content is cached

• layout is rendered

Page 29: Caching your rails application

Warning! Attention!

• fetch needed data with a before_filter

• action is not executed!

Page 30: Caching your rails application

Conditional AC

• caching only on certain conditions

Page 31: Caching your rails application

Action Caching

• when?

• execute code on each hit (authentication)

• result remains the same

• very easy to implement

Page 32: Caching your rails application

Fragment Caching

Page 33: Caching your rails application

Fragment caching

• Cache part of a page

• Logical blocks are candidates

Page 34: Caching your rails application

Example

Page 35: Caching your rails application

Fragment Caching

Page 36: Caching your rails application

No useless actions

Page 37: Caching your rails application

Fragment Cache expiry

• expire_fragment

Page 38: Caching your rails application

PC/AC Storage

Page 39: Caching your rails application

Page cache storage

• on disk

Page 40: Caching your rails application

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

DYI

Page 42: Caching your rails application

DYI caching

• Caching hard parts in the code

• Only spend cycles when really needed

Page 43: Caching your rails application
Page 44: Caching your rails application

Client-side caching

• The cloud is there, use it

Page 45: Caching your rails application

Three tags

• max-age

• etag

• last_modified

Page 46: Caching your rails application

max-age

Page 47: Caching your rails application

etag

• rails has this embedded

• MD5(body) => etag

• 304 Not Modified

Page 48: Caching your rails application

etag

• Beware, the complete parsing is done!

• Less bandwidth / traffic

• Client load time faster

Page 49: Caching your rails application

etag

Page 50: Caching your rails application

last_modified

Page 51: Caching your rails application

Scary technology?

• memcached

• reverse proxy’s

Page 52: Caching your rails application

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

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

Rack::Cache

• gem install rack-cache

• rails 2.3 needed!

Page 55: Caching your rails application

Need for speed?

• Database (n+1, joins / includes)

• Disk IO

• Implementing caching

• Browser caching

• External components (CDN, memcached, ...)