Scaling Rails
The other series I finally got around to watching last night was Scaling Rails. It was pretty pretty good, but at the same time highlighted some of the, for lack of a better term, concerning patterns that surround Rails. The two big ones I keep noticing are
- You need to run Edge in order to get the new stuff. It amazes me how many people run Edge in production. That seems like begging for pain.
- Scaling Rails is all about caching and where to put it. It’s not about more efficient design or architecture.
As I said before, the series was good, especially if you haven’t dug into how to scale your application before. Of the 13 episodes, you can skip 1, 4 and 13 as they are informercials for the product that sponsored the series. All the other episodes are sub-10 minutes in length and really well done from a production quality perspective.
- Episode Two: Page Caching
- Want to do this as much as humanly possible
- But don’t forget to expire them
- When to do it? When every user sees the same thing
- Episode Three: Expiration Strategy
- Sweepers
- Put them in their own directory
- Can observe both models and controllers
- ActionController::Caching::Sweeper
- Sweepers
- Episode Five: Advanced Page Caching
- Pagination – use routes
- Dynamic Data – use AJAX
- Episode Six – Action Caching
- Use when you need filters to run (like authentication)
- Good tip: when starting out, use :file_store as you can actually see the cached files on disk to see what it is doing
- tweaks
- don’t cache the layout (:layout => false)
- conditional caching (:if => Proc.new { … })
- Episode Seven – Fragment Caching
- When you have many pieces when you want to cache independently
- Episode Eight – Memcached
- Pronounced memcache dee
- Short explanation: {} in Memory
- Use in two different ways
- Object store
- Fragment cache store
- Fetch is kinda cool
- Avoiding cache expiration
- :expires_at
- use intelligent keys (by passing in ActiveRecord objects and letting Rails magic do its thing)
- Episode Nine – Interview with Taylor Weibley
- 3 tips for scaling
- Beware of fetching external data
- Optimize your database
- Don’t forget indexes
- Use :include
- Use counter caching
- QueryTrace plugin
- Drop to SQL if you really need control
- Denormalize columns
- Slaves and masters (Masochism plugin)
- Database sharding (Data Fabric gem)
- Design for scaling upfront
- 3 tips for scaling
- Episode Ten – Client-side Caching
- 3 headers
- max-age
- etag
- Nice explanation
- Faster for the client, still same cost for server
- In Rails 2.2 you can do custom keys
- last_modified
- stale?
- fresh_when
- etag and last_modified can be used together
- 3 headers
- Episode Eleven – Advanced HTTP Caching
- Proxy Cache – extra layer of caching on the client side
- Reverse Proxy Cache – extra layer of caching on the server side
- RPC expiration is controller using the Client-side Caching stuff
- or better yet, combine them
- max-age + etags
- max-age + last_modified
- gotta love when a recommended technique is available only if you are running Edge Rails. C’mon folks. Seriously?
- Episode Twelve – Jesse Newland
- Separate / isolate your rails stack
- Avoid hitting your database
- Use an intelligent reverse proxy (one that queues requests)
- Nginx + HAProxy + Thin
- Nginx + Fair Proxy Module
- Apache + Passenger
- Episode Thirteen – Jim Gochee
- Analyze your app in Production (load test against production)
- Optimize you database use
- Use HAProxy