{"id":13393140,"url":"https://github.com/ankane/production_rails","last_synced_at":"2025-11-17T14:22:03.932Z","repository":{"id":20281260,"uuid":"23554554","full_name":"ankane/production_rails","owner":"ankane","description":"Best practices for running Rails in production","archived":false,"fork":false,"pushed_at":"2025-07-25T06:15:53.000Z","size":75,"stargazers_count":2281,"open_issues_count":0,"forks_count":139,"subscribers_count":81,"default_branch":"master","last_synced_at":"2025-07-25T10:42:20.991Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc-by-4.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ankane.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-09-01T20:13:26.000Z","updated_at":"2025-07-25T06:15:57.000Z","dependencies_parsed_at":"2024-01-03T02:24:21.584Z","dependency_job_id":"ad138909-03a5-4fbf-b1d4-2ba7d0f7aba8","html_url":"https://github.com/ankane/production_rails","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ankane/production_rails","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fproduction_rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fproduction_rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fproduction_rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fproduction_rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ankane","download_url":"https://codeload.github.com/ankane/production_rails/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fproduction_rails/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":284895605,"owners_count":27080931,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-17T02:00:06.431Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:00:43.957Z","updated_at":"2025-11-17T14:22:03.927Z","avatar_url":"https://github.com/ankane.png","language":null,"funding_links":[],"categories":["Production","Others","生产","Styleguides","Resources","Uncategorized"],"sub_categories":["Security","安全","Ruby","Best Practices","Uncategorized"],"readme":"# Production Rails\n\n:rocket: Best practices for running Rails in production\n\nThis guide covers different concepts you should be familiar with. Recommendations come from personal experience and work at [Instacart](https://www.instacart.com/opensource). A number of open source projects are ones I’ve created. For a comprehensive list of gems, check out [Awesome Ruby](https://awesome-ruby.com).\n\n## Security\n\nEveryone writing code must be responsible for security. See [best practices](https://github.com/ankane/secure_rails) and [how to secure sensitive data](https://ankane.org/sensitive-data-rails).\n\n## Errors\n\nUse an error reporting service like [Rollbar](https://rollbar.com/).\n\nUse [Safely](https://github.com/ankane/safely) to rescue and report exceptions in non-critical code.\n\n## Logging\n\nUse a centralized logging service like [Mezmo](https://www.mezmo.com/).\n\nUse [Lograge](https://github.com/roidrage/lograge) to reduce volume. Configure it to add `request_id`, `user_id`, and `params`.\n\n```ruby\n# config/environments/production.rb\nconfig.lograge.enabled = true\nconfig.lograge.custom_options = lambda do |event|\n  options = event.payload.slice(:request_id, :user_id)\n  options[:params] = event.payload[:params].except(\"controller\", \"action\")\n  options\nend\n\n# app/controllers/application_controller.rb\ndef append_info_to_payload(payload)\n  super\n  payload[:request_id] = request.uuid\n  payload[:user_id] = current_user.id if current_user\nend\n```\n\n## Audits\n\nUse an auditing library like [Audited](https://github.com/collectiveidea/audited).\n\n## Migrations\n\nUse [Strong Migrations](https://github.com/ankane/strong_migrations) to catch unsafe migrations at dev time.\n\n## Web Requests\n\nUse compression. If not using a reverse proxy like nginx, use [Rack::Deflater](https://www.schneems.com/2017/11/08/80-smaller-rails-footprint-with-rack-deflate/).\n\nUse a [CDN](https://en.wikipedia.org/wiki/Content_delivery_network) like [Amazon CloudFront](https://aws.amazon.com/cloudfront/) to serve assets.\n\nUse [Slowpoke](https://github.com/ankane/slowpoke) for request timeouts.\n\n## Background Jobs\n\nUse a high performance background processing framework like [Sidekiq](https://github.com/mperham/sidekiq) with Active Job.\n\n```ruby\nconfig.active_job.queue_adapter = :sidekiq\n```\n\nUse [ActiveJob::TrafficControl](https://github.com/nickelser/activejob-traffic_control) to:\n\n- quickly disable jobs\n- throttle\n- limit concurrency\n\n```ruby\nBadJob.disable!\n```\n\n## Email\n\nUse an email delivery service. You can use one that supports both transactional and marketing email like [Postmark](https://postmarkapp.com/), or separate ones for each type.\n\nUse [Ahoy Email](https://github.com/ankane/ahoy_email) for message history.\n\n```ruby\nAhoyEmail.default_options[:message] = true\n```\n\nUse a CSS inliner like [Roadie](https://github.com/Mange/roadie-rails) for styling.\n\n```ruby\nclass ApplicationMailer \u003c ActionMailer::Base\n  include Roadie::Rails::Automatic\nend\n```\n\n## Caching and Performance\n\nUse [Memcached](https://memcached.org/) and [Dalli](https://github.com/petergoldstein/dalli) for caching.\n\n```ruby\nconfig.cache_store = :mem_cache_store\n```\n\nUse a library like [MemoWise](https://github.com/panorama-ed/memo_wise) for memoizing.\n\n```ruby\nmemo_wise :time_consuming_method\n```\n\n## Monitoring\n\n### Tracing\n\nUse a performance monitoring service with transaction traces like [New Relic](https://newrelic.com/) or [AppSignal](https://appsignal.com/).\n\n### Uptime\n\nUse an uptime monitoring service like [Pingdom](https://www.pingdom.com/) or [Uptime Robot](https://uptimerobot.com/).\n\n### Database\n\nThe database is a common bottleneck for Rails apps and deserves some special monitoring attention. There are some dedicated tools for this:\n\n- If you use Postgres, [PgHero](https://github.com/ankane/pghero) can help identify issues\n- Use [Active Record Query Logs](https://api.rubyonrails.org/classes/ActiveRecord/QueryLogs.html) to track the origin of SQL queries\n\n### Notable Events\n\nUse [Notable](https://github.com/ankane/notable) to track notable requests and background jobs.\n\n- errors\n- slow requests, jobs, and timeouts\n- 404s\n- validation failures\n- CSRF failures\n- unpermitted parameters\n- blocked and throttled requests\n\n## Timeouts\n\n[Add timeouts](https://github.com/ankane/the-ultimate-guide-to-ruby-timeouts).\n\nOne very important place is Active Record. Add to `config/database.yml` and adjust as needed.\n\n#### PostgreSQL\n\n```yml\nproduction:\n  connect_timeout: 2\n  checkout_timeout: 5\n  variables:\n    statement_timeout: 5000 # ms\n```\n\n#### MySQL and MariaDB\n\n```yml\nproduction:\n  connect_timeout: 1\n  read_timeout: 1\n  write_timeout: 1\n  checkout_timeout: 5\n  variables:\n    max_execution_time: 5000 # ms\n    max_statement_time: 5 # sec\n```\n\n## Analytics\n\nUse an analytics tool to measure important events. Consider a first-party library like [Ahoy](https://github.com/ankane/ahoy), or use a third-party service like [Amplitude](https://amplitude.com/) or [Mixpanel](https://mixpanel.com/).\n\n## New Features\n\nUse a feature flipper library like [Rollout](https://github.com/FetLife/rollout) to easily enable and disable new features without pushing code.\n\nTo A/B test features, use a library like [Field Test](https://github.com/ankane/field_test).\n\n## Lastly...\n\nHave suggestions? [Help make this guide better for everyone](https://github.com/ankane/rails-best-practices/issues/new).\n\nAlso check out [Development Rails](Development.md) and [Scaling Rails](Scaling.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fankane%2Fproduction_rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fankane%2Fproduction_rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fankane%2Fproduction_rails/lists"}