Best practices for running Rails in production
Use an error reporting service like Rollbar.
Use Slowpoke for request and database timeouts. 💎 disclaimer: one of my gems
Use Rack Attack to throttle and block requests.
Use an auditing library like Audited.
Use Lograge.
gem 'lograge'
Add the following to config/environments/production.rb
.
config.lograge.enabled = true
config.lograge.custom_options = lambda do |event|
options = event.payload.slice(:request_id, :user_id, :visit_id)
options[:params] = event.payload[:params].except("controller", "action")
# if you use Searchkick
if event.payload[:searchkick_runtime].to_f > 0
options[:search] = event.payload[:searchkick_runtime]
end
options
end
Add the following to app/controllers/application_controller.rb
.
def append_info_to_payload(payload)
super
payload[:request_id] = request.uuid
payload[:user_id] = current_user.id if current_user
payload[:visit_id] = ahoy.visit_id # if you use Ahoy
end
Use an uptime monitoring service like Pingdom or Uptime Robot.
Monitor web servers, background jobs, and scheduled tasks.
Use a performance monitoring service like New Relic or AppSignal.
Be sure to monitor:
- requests by action - total time, count
- queue time -
X-Request-Start
header
- jobs by type - total time, count
- requests by type - total time, count
- CPU usage
- space
- requests by type - total time, count
Use Notable to track notable requests and background jobs. 💎 disclaimer: one of my gems
- errors
- slow requests, jobs, and timeouts
- 404s
- validation failures
- CSRF failures
- unpermitted parameters
- blocked and throttled requests
Use a high performance web server like Unicorn.
gem 'unicorn'
Use SSL to protect your users. Add the following to config/environments/production.rb
.
config.force_ssl = true
Fix double logging in the Rails console. Create config/initializers/log_once.rb
with:
ActiveSupport::Logger.class_eval do
def self.broadcast(logger)
Module.new do
end
end
end
- Redis timeout
- Elasticsearch timeout
- Background jobs
- Scheduled jobs
- cant_wait gem for database timeouts