Your app got slow as users grew: where the time usually goes

Why apps slow down as data grows, and how to find the cause by measuring: slow queries, missing indexes, N+1 queries, big payloads and heavy requests.

The short answer

Most of the time goes to the database: queries that scanned a thousand rows now scan a million, one page runs a hundred small queries, or a missing index forces full-table reads. Measure first. Find the slowest pages, find the slowest queries behind them, fix the worst one, and measure again. Buying a bigger server comes last.

On this page
  1. How do you measure first?
  2. Where does the time usually go?
  3. When does caching help?
  4. When do you need a bigger server?
  5. How do you stop it from happening again?
  6. What does a good fix look like?
  7. Working with us

The app was fast with a hundred customers. At ten thousand it drags. Nothing in the code changed, which is the clue: the code is the same and the data is bigger. Performance work is mostly detective work, and the first rule is to measure before you change anything.

How do you measure first?

Do not guess. Set up three things:

  1. Request timing. Log how long each endpoint takes, with the route and not just the URL. Look at the median and the slowest five percent. Averages hide the pages users complain about.
  2. Slow-query logging in the database. List the queries that take the most total time, which is the number of calls times the duration.
  3. A target. For example: “the dashboard loads in under two seconds for the biggest account”. Without a target you do not know when to stop.

Then pick the single slowest item, fix it, and measure again with the same method. Report before and after. This habit beats any list of tricks.

Where does the time usually go?

1. Missing or wrong indexes

A query that filters or sorts on a column with no index reads the whole table. It is invisible at a thousand rows and painful at a million. Ask the database to explain a slow query: a sequential scan on a big table in a hot path is the sign.

Fix: add an index that matches the query’s filter and sort. Create it without blocking writes where your database allows it. Do not index everything, because each index slows writes and uses space.

2. The N+1 problem

A page loads a list of 100 items, then runs one more query for each item to fetch something related. That is 101 queries where two would do. It is the most common cause of a page that got slow as lists grew.

Fix: fetch the related data in one query (a join, or one query with a list of ids), and look at the count of queries per request in development.

3. Fetching too much

  • Selecting every column when the page needs three.
  • Returning every row instead of a page of results. Use pagination, and prefer cursor-based pagination on large tables over deep offsets.
  • Sending huge JSON payloads to the browser.
  • Counting rows exactly on every load of a large table.

4. Heavy work inside the request

Reports, exports, image processing, emails and calls to third parties should not run while the user waits. Move them to a background job and show progress. Make sure the jobs are safe with two copies running; see designing a SaaS that can move servers.

5. Locks and long transactions

A long transaction, or a migration that holds a lock, makes other queries wait. The page is slow because it is queued, not because its own query is slow.

6. Connection limits

Each request opens a database connection; the database has a limit. Past it, requests queue. A connection pool with sensible limits fixes it, and so does closing connections that are held while waiting on external calls.

7. The front end

Not all slowness is the server. Large JavaScript bundles, unoptimized images, and pages that wait for several API calls one after another all slow the experience. Measure in the browser as well, including on a mid-range phone and a slow connection.

When does caching help?

After you have fixed the queries, not before. A cache on top of a slow query hides the problem and adds a new one: stale data. Cache things that are expensive and change rarely, set an expiry, and keep important state out of in-memory caches that a second server cannot see. A cache is fine where losing it only makes data briefly stale.

When do you need a bigger server?

When measurements show the machine is the limit: CPU or memory consistently high, and the queries already efficient. More hardware for an unindexed query only delays the problem and raises the bill. If one server really is the ceiling, see the options for a database that is too big for one server.

How do you stop it from happening again?

  • Alert on slow endpoints, not just errors.
  • Test with a realistic amount of data in staging. Many performance bugs only appear with a big account.
  • Look at the query count and time of key pages when you review changes.
  • Run the same measurement every month and keep the numbers.

Measure before speeding anything up. We apply this to our own builds too: when our CI felt slow, we split each job into waiting, setup and running time, fixed the slowest item and reported before and after; see our CI and test lessons.

What does a good fix look like?

A real example of the shape: a list page takes six seconds for the largest account. Request timing shows the endpoint; the query log shows one query running for most of that time; the explain output shows a full table scan; an index on the filter and sort columns brings it down; the page is measured again and the number goes in the ticket. The fix is small. The work was in knowing where to look, which is why we start with measurement and not with a list of optimizations.

Working with us

If your app slowed down and you do not know why, we can measure it, find the few causes that account for most of the time and fix them in order, without a rewrite. Send us a description of what is slow via contact, or see our web application work.

Keep reading

Tell us what you want to build.

A product, a website, a search problem or a system that should work better. We reply with a plain answer and the first step.

A few sentences are enough (at least 20 characters).

We use your details only to answer your inquiry. See our privacy policy.