Giter VIP home page Giter VIP logo

production_rails's Introduction

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.

Security

Everyone writing code must be responsible for security. See best practices and how to secure sensitive data.

Errors

Use an error reporting service like Rollbar.

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

Logging

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")
  options
end

# 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
end

Audits

Use an auditing library like Audited.

Migrations

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 Active Job.

config.active_job.queue_adapter = :sidekiq

Use ActiveJob::TrafficControl to:

  • quickly disable jobs
  • throttle
  • limit concurrency
BadJob.disable!

Email

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
end

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.

Monitoring

Tracing

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

Uptime

Use an uptime monitoring service like Pingdom or Uptime Robot.

Database

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

Timeouts

Add timeouts.

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

PostgreSQL

production:
  connect_timeout: 2
  checkout_timeout: 5
  variables:
    statement_timeout: 5000 # ms

MySQL and MariaDB

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

Analytics

Use an analytics tool to measure important events. Consider a first-party library like Ahoy, or use a third-party service like Amplitude or Mixpanel.

New Features

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

To A/B test features, use a library like Field Test.

Lastly...

Have suggestions? Help make this guide better for everyone.

Also check out Development Rails and Scaling Rails.

production_rails's People

Contributors

ankane avatar itsderek23 avatar jasiekmiko avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.