Your SlideShare is downloading. ×
0
Caching,
                 Memcached,
                 and Rails

                 Mike Perham
                 http://mike...
Me	


                  Maintainer of memcache-client, shipped with Rails

                  Hardcore performance nerd



...
Performance


                  Let me Tell you a Story...




Wednesday, March 25, 2009
Performance Tuning

                  Remove unnecessary Functionality

                  Tune necessary Functionality

  ...
Caching

                  Cache the results of expensive operations

                  Why is this memoization dumb?




...
Why Dumb?

                            SCALABILITY!
                            FRESHNESS!



Wednesday, March 25, 2009
Rails
                  Built in support for memcached since Rails 2.1

                  config/environments/production.rb...
Request
                            Varnish   Apache     Rails     DB




                                               M...
Caching Mechanisms

                  HTTP Proxy
                               FASTER
                  Page

           ...
HTTP Proxy


                  HTTP/URL caching with Varnish

                       Expires, Etag, Cache-Control headers
...
Page Caching	


                  ERB --> Static HTML

                  Uh oh, uses the filesystem!

                  Apa...
Action Caching


                  Runs filters (e.g. authorization)

                  Can use any cache strategy

       ...
Fragment Caching


                  Parts of the page are unique per user

                  Fragment cache the various U...
Object Caching

                  Built-in AR support for “smart” keys

                       Rails.cache.fetch(post.cach...
Expiration
                  “There are only two hard things in Computer
                  Science: naming and cache inval...
Expiration (Cont’d)
                  Time-based
                        Rails.cache.fetch(‘key’, :expires_in => 1.hour) {...
Memcached

                  Giant Hash of Fixed Size

                  Time-based expiration

                  LRU evic...
Facebook


                  100s of servers w/ 28TB of RAM

                  5 billion page views/day

                 ...
Performance




Wednesday, March 25, 2009
Mongrel + Nginx
                                         Varnish

                                         Nginx

        ...
Apache + Passenger
                                           Varnish

                                           HAProxy
...
Advanced


                  Spread your cache across a farm of memcached

                  Use the memcaches_page plugin...
Advice

                  Cache late in the development cycle, after tuning!

                  Memcached gives you time-b...
THANK YOU!
                            HTTP://MIKEPERHAM.COM
                             MPERHAM@GMAIL.COM

             ...
Upcoming SlideShare
Loading in...5
×

Caching, Memcached And Rails

27,543

Published on

An overview of the various caching mechanisms available in Rails 2.3.

Published in: Technology
2 Comments
28 Likes
Statistics
Notes
No Downloads
Views
Total Views
27,543
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
269
Comments
2
Likes
28
Embeds 0
No embeds

No notes for slide
























  • Transcript of "Caching, Memcached And Rails"

    1. 1. Caching, Memcached, and Rails Mike Perham http://mikeperham.com Wednesday, March 25, 2009
    2. 2. Me Maintainer of memcache-client, shipped with Rails Hardcore performance nerd Wednesday, March 25, 2009
    3. 3. Performance Let me Tell you a Story... Wednesday, March 25, 2009
    4. 4. Performance Tuning Remove unnecessary Functionality Tune necessary Functionality ONLY THEN, add caching last ALWAYS track before/after timings Wednesday, March 25, 2009
    5. 5. Caching Cache the results of expensive operations Why is this memoization dumb? Wednesday, March 25, 2009
    6. 6. Why Dumb? SCALABILITY! FRESHNESS! Wednesday, March 25, 2009
    7. 7. Rails Built in support for memcached since Rails 2.1 config/environments/production.rb: Wednesday, March 25, 2009
    8. 8. Request Varnish Apache Rails DB Memcached Standard Toolset for Rails 2.3 Further == Slower Wednesday, March 25, 2009
    9. 9. Caching Mechanisms HTTP Proxy FASTER Page Action Fragment SLOWER Object Wednesday, March 25, 2009
    10. 10. HTTP Proxy HTTP/URL caching with Varnish Expires, Etag, Cache-Control headers 5-10k req/sec! Wednesday, March 25, 2009
    11. 11. Page Caching ERB --> Static HTML Uh oh, uses the filesystem! Apache = 1-5k req/sec Wednesday, March 25, 2009
    12. 12. Action Caching Runs filters (e.g. authorization) Can use any cache strategy File, memory, memcached Wednesday, March 25, 2009
    13. 13. Fragment Caching Parts of the page are unique per user Fragment cache the various UI boxes Dynamically assemble HTML from the cache Wednesday, March 25, 2009
    14. 14. Object Caching Built-in AR support for “smart” keys Rails.cache.fetch(post.cache_key) { expensive_calc(post) } “#{table_name}/#{self.id}-#{self.updated_at.to_i}” Support for complex dependencies Wednesday, March 25, 2009
    15. 15. Expiration “There are only two hard things in Computer Science: naming and cache invalidation” Explicit expire_page :controller => ‘users’, :action => ‘index’ expire_action :controller => ‘users’, :action => ‘index’ Rails.cache.delete(‘key’) Observers Wednesday, March 25, 2009
    16. 16. Expiration (Cont’d) Time-based Rails.cache.fetch(‘key’, :expires_in => 1.hour) { ... } expires_in(2.hours) # sets page’s HTTP max-age attribute Versioning Rails.cache.fetch(user.cache_key) { ... } Wednesday, March 25, 2009
    17. 17. Memcached Giant Hash of Fixed Size Time-based expiration LRU eviction Operations: GET, SET, APPEND, INCR, CAS Limited to 1MB per value, 250 chars per key Wednesday, March 25, 2009
    18. 18. Facebook 100s of servers w/ 28TB of RAM 5 billion page views/day 400k req/sec from memcached Wednesday, March 25, 2009
    19. 19. Performance Wednesday, March 25, 2009
    20. 20. Mongrel + Nginx Varnish Nginx WEB1 M1 M2 M1 M2 Memcached Memcached APP1 APP2 Wednesday, March 25, 2009
    21. 21. Apache + Passenger Varnish HAProxy WEB1 Memcached Memcached Apache Apache Rails Rails Rails Rails APP1 APP2 Wednesday, March 25, 2009
    22. 22. Advanced Spread your cache across a farm of memcached Use the memcaches_page plugin Use AJAX for user-specific widgets Wednesday, March 25, 2009
    23. 23. Advice Cache late in the development cycle, after tuning! Memcached gives you time-based expiration, versioning and multi-machine support. Cache as early in the request as possible! Varnish Apache Rails DB Wednesday, March 25, 2009
    24. 24. THANK YOU! HTTP://MIKEPERHAM.COM MPERHAM@GMAIL.COM QUESTIONS? Wednesday, March 25, 2009
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×