Performance First Rails – Lessons From Production

| Shey Sewani | Toronto

This is a work in progress. These are the lessons I’ve picked up from running rails in production.

My Rails Performance Lessons

1. Always measure the thing you want to improve.

  • Test against production. It’s the only environment that matters.
  • Finding the right worker count is trial and error. Experiment.

2. Start with the database. It’s always the database.

  • Use CTEs to rewrite queries with subqueries.
  • Unindexed queries hurt the system’s overall performance. Fix this first.
  • Tune your database settings away from the defaults.
  • Create partial indexes for frequently queried accounts.
  • Use pluck and send less data across the wire.
  • Denormalize the most requested data (joins are expensive).
  • Use different settings for different workloads.

3. Cache aggressively: the less work your DB does, the faster everything gets.

4. Controlled parallelism is great.

  • Separate Redis for workers vs. cache vs. sessions.
  • You really only need three queues: fast, slow, and hi-mem.
  • You can add a vip queue, but don’t get carried away.
  • Run different workloads on different hosts.

5. Make the most of Ruby.

6. Do not use your database as a queue.