Which page is slow, for which users, and since when? If nobody on the team can answer all three, the application isn’t ready for Ruby on Rails performance optimization, because every fix will be a guess. Most “slow Rails app” tickets turn out to be one of three different problems, and each one needs a different fix.
The three are the database, the background job queue and the infrastructure around the app. This guide shows the signs of each, a cheap way to confirm it, and what to change first. The Rails details come from the Rails 8.1 guides and the PostgreSQL documentation.
Pin down what “slow” means first
Pick one endpoint and one time window, and write down the number. A median request time hides the users having a bad day, so record a high percentile such as the 95th as well. Note whether the slowness appeared after a deploy, after a data import or gradually as the tables grew.
Then split the request time. Rails publishes a process_action.action_controller event whose payload includes view_runtime and db_runtime, both in milliseconds, as the instrumentation guide documents. If database time dominates, read the next section. If neither number accounts for most of the total, the time is going somewhere else: an outbound API call, a lock, or the request waiting before Rails ever sees it.
Database: pages that get slower as the data grows
A database problem usually shows up as request time that tracks the number of rows or records on the page. A dashboard that was fine with 50 customers and crawls with 5,000 is the classic case.
The most common cause in Rails code is the N+1 pattern, where a loop loads an association once per record. The Active Record querying guide covers three fixes with different behavior. preload loads each association with its own query, eager_load uses a LEFT OUTER JOIN and allows conditions on the association, and includes picks between them. Choosing wrongly can swap many small queries for one huge join, so check the result rather than assuming.
To find the queries that matter, enable the pg_stat_statements module. It records execution counts and timings per normalized statement, but it needs a shared_preload_libraries entry and a server restart, so plan a window. Sort by total execution time, not by the slowest single call. A query that takes 5 milliseconds and runs fifty thousand times an hour costs more than one that takes two seconds and runs twice.
Then look at the plan of the worst offender with EXPLAIN ANALYZE. The PostgreSQL manual warns that this actually executes the statement, so wrap anything that writes data in a transaction and roll it back. A missing index and a plan that scans a whole table are the usual findings. Fix those before reaching for caching, which can hide a query problem without removing it.
Background jobs: fast pages, late results
When the web requests look healthy but users complain that emails, exports or webhooks arrive late, look at the queue. The symptom is delay, not slow pages.
Measure queue latency, which is how long the oldest waiting job has been there. The Sidekiq wiki recommends monitoring latency instead of backlog size, because backlog size can raise false alarms while a job that has waited a long time is what users actually feel. Its example alert fires when jobs wait more than 30 seconds, and you should choose your own threshold from what each job type promises users.
Check the adapter before tuning anything. The Active Job guide describes the async adapter as an in-process queue that keeps jobs in memory, and says outstanding jobs are lost if the process crashes or the machine restarts. Since Rails 8.0 the default queue backend is Solid Queue. If a production app still runs jobs in-process, late jobs may be the milder symptom. Lost jobs are worse.
If latency is high on a healthy adapter, the usual causes are one slow job type blocking fast ones and too few workers for the load. Separating queues by urgency, so a large report export can’t hold up a password reset email, is often the first change. Job code has its own database time too: the perform.active_job event reports db_runtime, so the database checks above apply to jobs as well.
Infrastructure: everything is slow at once
If trivial endpoints slow down together, and database time stays low, suspect the layer under Rails. Signs include slowness that tracks traffic peaks or deploys, one host that behaves worse than its neighbors, and requests that wait before the application starts working on them.
Confirm it by comparing the same endpoint across hosts and over time, and by checking CPU, memory and swap on the app servers during the slow period. Check whether the number of web processes and threads matches the load, and how long requests wait in the load balancer or web server queue. That wait doesn’t appear in Rails’ own timings, which is why people miss it.
Be careful with the instinct to add servers. If the database is the bottleneck, more app servers open more connections to it and can make things worse. Treat extra capacity as an experiment with a before and after measurement, not as the first answer.
When the time isn’t in any of the three
Sometimes the split shows database and job numbers that look fine while the request is still slow. Look at outbound calls first. A payment gateway, a shipping rate lookup or a webhook sent inside the request can add seconds that no query log will show. If the call doesn’t need to finish before the user sees a response, move it into a job. If it does, set an explicit timeout and decide what the page should do when the call fails.
Lock waits are the other quiet culprit. Two requests updating the same rows can queue behind each other, and the symptom is a slowdown that appears only under concurrent use. Your database logs and its lock views will show it when you look at the slow window rather than at an average.
A made-up example of how the pieces separate
This is hypothetical, invented for illustration, and describes no real project. A SaaS product gets complaints that its monthly report page “times out”. The request log shows most of the time in the database. pg_stat_statements points to one query called thousands of times per page view, which turns out to be an N+1 on line items. An includes fix removes it.
The same team also hears that invoice emails arrive an hour late. Queue latency shows the cause: report exports and email jobs share one queue, and exports win. Moving them to separate queues fixes it. Two complaints, two causes, and neither needed a bigger server.
What to capture before you change anything
- A named endpoint or job, with its high-percentile time and a date range.
- The split between database, view and other time for that endpoint.
- The ten most expensive statements by total execution time.
- Queue latency per queue, and the adapter in use.
- CPU, memory and request queue time on the app hosts during the slow window.
Change one thing at a time and re-measure. A release that adds an index, moves a queue and resizes a server tells you nothing about which of them worked.
Where outside help fits
An existing application with unclear history is the case where a second pair of eyes saves time. RailsCarma’s Ruby on Rails consulting page describes reviewing existing Rails applications for issues, and its application support and maintenance service covers ongoing support under SLAs once the fixes are in.
Measure first, then spend
A slow application feels like one problem, but it usually hides two or three. Split the time, confirm each suspect with a number, and fix the biggest one. Speak with RailsCarma’s team to review your application’s performance bottlenecks and define a practical fix list.