Best practices for running Rails in production.
Disclaimer: 💎 = one of my gems
Everyone writing code must be responsible for security. Best practices
Use an analytics service like Google Analytics or Mixpanel.
And possibly an open source library like Ahoy. 💎
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")
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 auditing library like Audited.
- There are two important metrics to track for web servers
- Use an error reporting service like Rollbar
- 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
- 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. 💎
- errors
- slow requests, jobs, and timeouts
- 404s
- validation failures
- CSRF failures
- unpermitted parameters
- blocked and throttled requests
Use Slowpoke for request and database timeouts. 💎
- Use a high performance web server like Unicorn
- Use Rack::Deflater for compression
- Add Oj to speed up JSON parsing
- Use Memcached for caching
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
Have suggestions? Let me know