Rails is built for shipping fast. The same conventions that make it productive also make it easy to add an N+1 query or a slow callback that nobody notices until real traffic arrives. This guide covers how to set up a Rails app for a meaningful load test, how to handle the authenticity token, and the bottlenecks that show up most often.
Test the production setup
Run the test against the environment you actually deploy, not a development server:
RAILS_ENV=production, with eager loading on and code reloading off;- assets precompiled and served by a CDN or Nginx, not by Rails;
- Puma configured the way production is: same threads, same workers;
- logging at a production level. Debug logging on every request costs real time;
- profilers like rack-mini-profiler turned off.
A staging copy of production with realistic data is ideal. An index you don't need with 1,000 rows becomes essential with 10 million.
Understand Puma threads and workers
Puma has two settings that decide how many requests you can serve at once:
- workers: separate processes, usually one per CPU core;
- threads: concurrent requests inside each worker.
With 2 workers and 5 threads, the server handles 10 requests at a time. Because of Ruby's global VM lock, threads help most when requests wait on the database or network, and less when they burn CPU. If requests take 200 ms each, 10 slots give you roughly 50 requests per second. Raising threads beyond that may just move the queue to the database.
Handle the authenticity token and sessions
Rails protects forms with an authenticity token. A realistic test does what a browser does:
GET /users/sign_in, and capture theauthenticity_tokenvalue from the form (or thecsrf-tokenmeta tag);POSTthe email, password and token, keeping the session cookie;- continue with the same cookies. For JavaScript requests, send the token in the
X-CSRF-Tokenheader.
For API-only apps with token or JWT auth, sign in once per virtual user, capture the token and send it on every request.
Use a dataset of test accounts so each virtual user is different. Remember that Devise hashes passwords with bcrypt, which is CPU-heavy by design. A login storm can saturate CPUs on its own.
The usual bottlenecks
ActiveRecord pool smaller than threads. Each Puma thread needs a database connection. If pool in database.yml is 5 and Puma runs 10 threads, threads wait for a connection and you see ActiveRecord::ConnectionTimeoutError. Keep pool at least equal to RAILS_MAX_THREADS. Then count the total: servers × workers × threads must fit inside PostgreSQL's max_connections, or you need PgBouncer.
N+1 queries. A view that loops over 50 posts and shows post.author.name runs 51 queries. Fix with includes(:author) (or preload and eager_load when you need control). The Bullet gem flags N+1 queries in development, and strict_loading can raise an error when lazy loading happens.
Missing indexes. Columns used in where, order and joins need an index. Check slow queries with .explain or your database's slow query log.
Slow callbacks and synchronous work. after_save callbacks that send emails, call webhooks or rebuild search indexes run inside the request. Move them to Sidekiq (or another job backend) with deliver_later and perform_later. Then run the load test with Sidekiq workers running, and watch the queue. A fast request that leaves 10,000 jobs behind is still a problem.
Caching. Fragment caching and Rails.cache with Redis or Memcached can take a lot of load off the database. File store cache does not share across servers.
Read the results per step
Look at each step of the journey separately. If only the dashboard slows down while sign-in stays flat, that view is the problem, not the server. Compare p95 per step with a baseline run and re-run the same test after each fix. For help reading a slow result, see find the bottleneck in a slow API.
Rails with Hoofbeat
Describe the journey in HoofbeatChat ("sign in to my Rails app, open the dashboard, create a project"), or import a HAR recording of yourself doing it (how). Add a capture for authenticity_token on the sign-in page and reuse it in the POST. Hoofbeat keeps cookies per virtual user and reads accounts from a dataset. Connect Prometheus to chart CPU, database connections and Sidekiq queue size next to latency. Once the test is stable, add it to CI so a new N+1 query fails the build instead of reaching production.