Best practices for running Rails in production
Switch branches/tags
Nothing to show
Clone or download
Failed to load latest commit information. Restore focus to production Aug 7, 2018 Restore focus to production Aug 7, 2018 Restore focus to production Aug 7, 2018

Production Rails

🚀 Best practices for running Rails in production

This guide covers different concepts you should be familiar with. Recommendations come from personal experience and work at Instacart. A number of open source projects are ones I’ve created. For a comprehensive list of gems, check out Awesome Ruby.


Everyone writing code must be responsible for security. Best practices


Use an error reporting service like Rollbar.

Use Safely to rescue and report exceptions in non-critical code.


Use a centralized logging service like LogDNA.

Use Lograge to reduce volume. Configure it to add request_id, user_id, and params.

# config/environments/production.rb
config.lograge.enabled = true
config.lograge.custom_options = lambda do |event|
  options = event.payload.slice(:request_id, :user_id)
  options[:params] = event.payload[:params].except("controller", "action")

# app/controllers/application_controller.rb
def append_info_to_payload(payload)
  payload[:request_id] = request.uuid
  payload[:user_id] = if current_user


Use an auditing library like Audited.


Use Strong Migrations to catch unsafe migrations at dev time.

Web Requests

Use a high performance web server like Puma.

Use Rack::Deflater for compression.

Use a CDN like Amazon CloudFront to serve assets.

Use Slowpoke for request timeouts.

Background Jobs

Use a high performance background processing framework like Sidekiq with ActiveJob.

config.active_job.queue_adapter = :sidekiq

Use ActiveJob::TrafficControl to:

  • quickly disable jobs
  • throttle
  • limit concurrency


For transactional emails, use an email delivery service like SendGrid.

For marketing emails, use a service like MailChimp.

For styling, use a CSS inliner like Roadie.

class ApplicationMailer < ActionMailer::Base
  include Roadie::Rails::Automatic

Add UTM parameters to links.

Caching and Performance

Use Memcached and Dalli for caching.

config.cache_store = :dalli_store

Use a library like Memoist for memoizing.

memoize :time_consuming_method

Add Oj to speed up JSON parsing.



Use a performance monitoring service with transaction traces like New Relic or AppSignal.


Use an uptime monitoring service like Pingdom or Uptime Robot.


The database is a common bottleneck for Rails apps and deserves some special monitoring attention. There are some dedicated tools for this:

  • If you use Postgres, PgHero can help identify issues
  • Use Marginalia to track the origin of SQL queries

Notable Events

Use Notable to track notable requests and background jobs.

  • errors
  • slow requests, jobs, and timeouts
  • 404s
  • validation failures
  • CSRF failures
  • unpermitted parameters
  • blocked and throttled requests

Performance KPIs

Web Requests

  • 5xx errors and latency
  • requests by action - total time, count
  • queue time - X-Request-Start header

Background Jobs and Rake Tasks

  • jobs by type - total time, count

Data Stores - Database, Elasticsearch, Redis

  • requests by type - total time, count
  • CPU usage
  • space

External Services

  • requests by type - total time, count


Add timeouts.

One very important place is ActiveRecord. Add to config/database.yml and adjust as needed.


  connect_timeout: 1
  checkout_timeout: 5
    statement_timeout: 5000 # ms

MySQL and MariaDB

  connect_timeout: 1
  read_timeout: 1
  write_timeout: 1
  checkout_timeout: 5
    max_execution_time: 5000 # ms, for MySQL 5.7.8 or higher
    max_statement_time: 5 # sec, for MariaDB 10.1.1 or higher


Use an analytics service like Google Analytics or Mixpanel.

And possibly an open source library like Ahoy.

New Features

Use a feature flipper library like Rollout to easily enable and disable new features without pushing code.


Have suggestions? Help make this guide better for everyone.

Also check out Development Rails and Scaling Rails.