Finding bottlenecks

Topic 2 of 8 in Week 2: Scaling, traffic and estimation9 min readBeginnerFree course

Overview

A system is only as fast as its slowest part. If the database can handle 5,000 queries a second, it doesn’t matter that your app servers could handle 50,000. Finding that limiting component, the bottleneck, is the first step in any scaling problem, at work and in interviews.

What a bottleneck looks like

Finding the bottleneck. A request path goes through a load balancer at 18 percent utilisation, app servers at 42 percent, a cache at 30 percent and a database at 96 percent, which is saturated so queries queue up. To find it, look for the resource near 100 percent (CPU, memory, disk I/O, connections or network) and follow latency: the component whose p99 grows first is the one to fix.

In this picture the database is the bottleneck. Adding app servers would not help; it would send even more queries to the database and make the queue longer.

Utilisation, saturation and errors

A practical way to check each resource is the USE method:

  • Utilisation: how busy is it? (CPU 90%, disk 100% busy, network at 8 of 10 Gbit/s)
  • Saturation: is work waiting? (run queue length, disk queue, threads waiting for a connection)
  • Errors: is it failing? (timeouts, dropped packets, out-of-memory kills)

Check it for every resource on every component: CPU, memory, disk I/O, network and pools such as database connections or worker threads. Pools are a common hidden limit: a database with idle CPU can still be the bottleneck if every app server has only 10 connections and all are in use.

Follow the latency

Utilisation tells you who is busy; latency tells you who is making users wait. Trace a slow request and see where the time goes:

Text
total 850 ms
  ├─ gateway            4 ms
  ├─ auth service      12 ms
  ├─ product service   30 ms
  │    └─ database    780 ms   ← here
  └─ render            24 ms

Distributed tracing tools draw exactly this. As a component approaches saturation its p99 latency rises first, often before averages move, so watch the tail.

Common bottlenecks and first fixes

Bottleneck Symptoms Typical first fixes
Database reads High CPU or disk reads, slow queries Indexes, caching, read replicas
Database writes Lock waits, slow commits Batch writes, queue them, shard
One hot row or key One counter or celebrity profile overloaded Split the counter, cache the hot key, spread load
App CPU High CPU on all app servers Add servers, optimise code, cache results
Network or bandwidth Saturated links, large payloads Compress, CDN, smaller responses
External service Waiting on a slow API Timeouts, caching, async calls
Connection or thread pools Requests queue with idle CPU Size pools correctly, reduce call time

Fix in the right order

  1. Measure to find the actual bottleneck, not the one you assume.
  2. Do less work: remove unnecessary queries, fix N+1 query patterns, add indexes. These are cheap and often give 10× gains.
  3. Avoid repeated work: cache results that are read often and change rarely.
  4. Spread the work: add servers, replicas or shards.
  5. Measure again. Removing one bottleneck reveals the next. After adding a cache, the database may be fine but the cache network may be saturated.

Bottlenecks in interviews

During the deep-dive and wrap-up phases, interviewers often ask “where will this break first?”. A good answer uses your estimates:

“At 35,000 reads per second at peak, the app servers are fine; we can add more. The database is the first bottleneck. With a 90% cache hit rate it sees about 3,500 reads per second, which a primary plus two read replicas can handle. If the hit rate falls, the replicas are the next thing to watch.”

Two common follow-ups:

  • “What if traffic grows 10×?” Walk through each component and say which one runs out first and how you’d scale it.
  • “What about hot keys?” Explain how a single popular item can overload one cache node or one shard even when total load is fine, and how to spread it (replicating hot keys, local caches, splitting counters).

Recap

The bottleneck is the resource that runs out first, and it caps the whole system. Find it by checking utilisation, saturation and errors on every resource, and by following where latency goes. Fix it cheaply first (less work, caching), then spread the load, and measure again because the bottleneck always moves.

Exercise

Diagnose the slowdown

An online shop slows down every evening. Dashboards show: app servers at 35% CPU, the Redis cache at 20% CPU with a 60% hit rate, the database at 90% CPU with disk reads at their limit, and the database connection pool on each app server often full. The slowest query is 'list products in a category, sorted by popularity'. What is the bottleneck, what evidence points to it, and what three changes would you try, in order?

Text
Bottleneck:

Evidence:

Changes to try, in order:
1.
2.
3.

Summary

  • A bottleneck is the resource that runs out first; it limits the throughput of the whole path.
  • Check utilisation, saturation (queues and waiting) and errors for each resource: CPU, memory, disk, network and connections.
  • Rising tail latency is often the first sign of saturation.
  • Fix the real bottleneck first; scaling other components can make things worse.
  • Every fix moves the bottleneck somewhere else, so measure again after each change.