Skip to content

Lesson 3 of 9 · 25m

Deployment Shape and Observability Before Performance Tuning

Build a boring, repeatable Rails runtime with migrations, process supervision, logs, metrics, request IDs, and deploy rollback expectations.

Before you tune Rails, make it observable and deployable. I have watched teams argue about Ruby object allocation while their deploys ran migrations before all app processes understood the schema, and their logs could not connect a slow request to the tenant that caused it. Performance without operational shape is astrology.

Know your process topology

A small but serious Rails deployment usually has distinct process roles:

web      Puma serving HTTP
worker   Solid Queue, Sidekiq, or another job processor
cron     recurring scheduler or systemd timers
release  one-off migration and deploy preparation task

Do not run unrelated long jobs inside the web process because it feels simpler. Web dynos or containers should remain responsive when report generation gets expensive.

Migrations are deployment events

Rails can run migrations during a release step:

bin/rails db:migrate

The important part is compatibility. Add columns first, deploy code that can read old and new shapes, backfill, then enforce constraints in a later deploy. A migration that rewrites a giant table or takes an exclusive lock is not a release-step detail; it is a scheduled operation.

Structured logs beat decorative logs

Use request IDs and include tenant or account context where safe:

config.log_tags = [:request_id, ->(req) { "account=#{req.session[:account_id] || '-'}" }]
config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")

Do not log tokens, cookies, passwords, full payloads, or personal data casually. You need enough context to reconstruct behavior, not a second breach database.

Measure the four golden signals in Rails terms

Latency belongs by controller action and job class. Traffic belongs by route and process type. Errors belong by exception class and deploy revision. Saturation belongs by database pool, queue depth, memory, CPU, and connection counts. A dashboard named only puma is not application observability.

# config/initializers/lograge.rb, if you use lograge
Rails.application.configure do
  config.lograge.enabled = true
  config.lograge.custom_options = ->(event) { { params: event.payload[:params]&.except("controller", "action"), db: event.payload[:db_runtime], view: event.payload[:view_runtime] } }
end

Filter sensitive parameters before sending them anywhere.

Rollback is application design

If rollback means restoring a backup, it is not rollback; it is disaster recovery. Prefer forward-compatible migrations, feature flags for risky behavior, and deploy artifacts you can redeploy quickly. Record the Git revision in logs and error reports so every observation is anchored to a code version.

Field notes

  • Puma worker count is a memory and concurrency decision, not a magic performance dial.
  • Database pool size multiplied across processes can exceed Postgres connection capacity long before CPU does.
  • Health checks should detect process liveness separately from expensive dependency readiness.
  • Add observability to the job system too; a perfect web dashboard can hide a six-hour queue backlog.

Was this lesson helpful?

Sign in to rate