Performance across the stack
Find why a Taskboard page is slow by reading the network waterfall, then fix the layer at fault: N+1 queries, missing indexes, payloads or request chains.
Observability across layersShaping queries for screensCalling the API from the client
Performance across the stack
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Measure before you optimise: the network waterfall in the browser shows whether time went to the server, the payload, or requests waiting on each other.
- A slow page is usually one layer's fault, and the fix in the wrong layer changes nothing the user can feel.
- An N+1 pattern turns one screen into hundreds of queries, and replacing it with a single join took 101 queries down to 1 in a test.
- An index on a filtered foreign key changes the query plan from a sequential scan to an index scan, which you should confirm with explain rather than assume.
- Request waterfalls, where the client waits for one response before asking for the next, add latency no server tuning can remove.
Start by looking, not guessing
A user says the Taskboard project page feels slow. The temptation is to open the code and start improving whatever looks inefficient. That is how you spend a week making a function twice as fast when it was 2% of the delay.
A full-stack page is slow because of one of a few things, and each lives in a different layer:
| Where the time goes | Layer | Typical cause |
|---|---|---|
| Waiting before the first byte | API and database | Slow query, N+1, missing index, cold start |
| Downloading | Network | Big JSON, uncompressed or oversized images, large bundle |
| Requests queued behind each other | Client | A waterfall: fetch A, then B using A, then C |
| After the data arrives | Browser | Heavy rendering, long tasks |
The core problem is working out which row you are in. The tool is the network waterfall in your browser’s developer tools: a bar per request, split into phases. A long “waiting for server response” bar means the server is the problem. A long download bar means payload. Bars that start only when another ends mean a waterfall in your client code. This is the topic of the Hussein Nasser video below, and it is the same skill whatever the framework.
Case 1: the server is slow because of N+1
Taskboard’s project page shows each task with its comment count. The naive version loads the tasks, then asks for the count task by task. I measured it on PGlite with 100 tasks:
const ts = (await q("select id from tasks where id <= 100")).rows;
for (const r of ts) await q("select count(*) from comments where task_id=$1", [r.id]);
// n+1 queries: 101, 7.0 ms
versus one query that does the join and the count together:
select t.id, count(c.id) from tasks t
left join comments c on c.task_id = t.id
where t.id <= 100 group by t.id;
-- 1 query, 0.6 ms
The absolute times are tiny because the database is in the same process, with no network. Against a real database each query costs a network round trip as well, so the gap grows with distance: 101 round trips at even 2 ms each is 200 ms before any work is done. The point is the shape. One approach issues a number of queries that grows with the list, the other does not.
This pattern is explained properly in the N+1 query problem and eager loading, and the screen-driven view of it is in shaping queries for screens. In a waterfall it looks like a long server-wait bar, and in your logs from the previous lesson it appears as many identical queries under one request id.
Case 2: one query is slow because of a missing index
After the join, the comments query filters by task_id. With 50,000 comment rows and no index, explain showed:
Seq Scan on comments (cost=0.00..677.50 rows=163 width=40)
Filter: (task_id = 42)
After create index on comments(task_id):
Bitmap Heap Scan on comments (cost=6.23..286.27 rows=250 width=40)
Recheck Cond: (task_id = 42)
The plan changed from reading every row to using the index, and the estimated cost fell by more than half. (I used PGlite 0.5.17; the estimates are the planner’s guesses, not timings. Always check with a realistic amount of data, because on a tiny table Postgres may rightly prefer a scan.) The habit: for any query that is slow in production, run explain (or explain analyze to include real timings) and read what it does. A foreign key column you filter or join on is the first place to look for a missing index. Postgres does not create indexes on foreign keys for you.
Case 3: the page waits on itself
Suppose the API is fast, every query is indexed, and the page is still slow. Open the waterfall. If you see:
GET /api/me ──────
GET /api/projects ────── (starts after /api/me ends)
GET /api/projects/1/tasks ──────
then the client is the problem: each request is started only after the previous one returns. Fixes, from least to most work: start independent requests together, use the router’s loaders so data fetching begins on navigation instead of after the component renders, or add one endpoint that returns what the screen needs (see designing endpoints for the UI). No amount of query tuning removes a waterfall, because the latency is the sum of the trips.
Case 4: too many bytes
If the download bar is the long one, look at what you send:
- JSON with fields the screen never uses. Select fewer columns, or paginate. See pagination, filtering and search end to end.
- Responses without compression. Turn on gzip or Brotli at the proxy or host.
- Large images. Resize them and serve modern formats, with width and height attributes.
- A large JavaScript bundle. Code splitting, covered in code splitting and lazy loading, moves rarely used code out of the first load.
For what the browser measures about all this, see web performance and Core Web Vitals.
Caching is the fix you add last
Once the work itself is cheap, caching avoids repeating it: HTTP caching headers for static assets and rarely changing responses, a cache in front of an expensive query. It is powerful and also the source of the “I changed it and nothing happened” bug, so reach for it after the waterfall and the queries are clean. Mechanisms and trade-offs are in caching in application code.
A working order
- Reproduce the slow page and open the waterfall.
- Name the layer from the table above.
- In that layer, find the single largest cost: one slow request, one repeated query, one big payload.
- Fix it and measure again. If the number did not move, you fixed the wrong thing, so revert and look again.
Keep the before and after numbers somewhere. They are your proof that the change helped, and they are what you will wish you had in six months.
What the video covers
Hussein Nasser’s “DevTools Waterfall Deep Dive - Diagnose Your Backend and Improve the Frontend User Experience” runs about 29 minutes. It is six years old, and by its title it uses the browser DevTools waterfall to diagnose backend and frontend problems, exactly this lesson’s first move. The DevTools interface has changed since, so expect different labels and layout, though the phases of a request are the same. I could not read a transcript. DebugBear’s shorter waterfall video is linked as a newer second view.
Where this fits
You can now diagnose where a page loses time. The last group takes the app past its first users, starting with money, which brings in an outside service that calls you back: payments and webhooks end to end.
Check yourself
What does a long green 'waiting' (time to first byte) bar in a waterfall point to?
Time spent on the server before it started responding, so the work is in the API or the database rather than in the network or the browser.
Why can a fast API still give a slow page?
Because the client may make requests one after another, each waiting for the last, or download a large bundle or payload, so the user waits on the sum of the steps.
How do you check that an index is actually used?
Run explain on the query before and after creating it and look for the plan to change from a sequential scan to an index or bitmap scan.
What is an N+1 query problem?
Loading a list with one query and then running one more query for each item, such as comment counts per task, so the query count grows with the list.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.