Blog

The N+1 Problem Isn't Just a Database Thing

September 6, 2026Xerat023 min read
Share
The N+1 Problem Isn't Just a Database Thing

Most engineers meet the N+1 problem as a database story: fetch a list of 20 orders, then loop over them making a separate query for each order's customer, and you've just run 21 queries where one join would've done it. It's a genuinely common ORM footgun, well-documented, and reasonably easy to catch with query logging.

The same pattern shows up at the API layer, between services, and it's much easier to miss — because there's no query log to notice it in, just a page that feels slower than it should.

What it looks like over HTTP

A frontend fetches a list of 20 items from one endpoint, then — to render each item fully — makes a second API call per item to fetch its details, its author, its thumbnail, whatever the list endpoint didn't already include. Twenty items means twenty-one network round trips instead of one or two. Unlike a database query, each of these round trips carries real network latency — even at a fast 50ms per call, twenty sequential calls is a full second of pure waiting, before any of the actual work happens.

This is easy to write by accident, because it often starts out completely reasonable: a detail view was built to fetch its own data on its own page, and later, a list view reused that same per-item fetch inside a loop instead of the list endpoint being extended to include what the list actually needs.

Why it's harder to spot than the database version

A slow database query shows up in a query log with a timestamp and a duration, repeated suspiciously 20 times in a row — a fairly loud signal once you know to look. An API-level N+1 shows up as "this page takes 1.2 seconds to load," diffused across the browser's network tab as twenty individual, individually-fast requests that only look bad in aggregate. Nobody's alerted; nothing crashed; it's just slower than it should be, in a way that's easy to attribute to "the API is just kind of slow" rather than "we're making twenty calls where two would do."

The fixes, roughly in order of effort

  • Batch endpoints: GET /users?ids=1,2,3,...,20 instead of twenty separate GET /users/{id} calls, when the client controls which items it needs up front.
  • Include/expand parameters: GET /orders?expand=customer so the list endpoint itself returns the nested data the frontend actually needs, avoiding the second round trip entirely.
  • A backend-for-frontend or aggregation layer that does the fan-out server-side, where the N calls still happen but from a machine with much lower latency to the services being called, rather than from a user's browser over their real network connection.

Catching it before it ships

The mundane, effective way to catch this early: count the actual number of requests a feature makes for a realistic-sized list, against a mock where each individual response is deliberately configured with real, non-zero latency via a route delay. A feature that "feels fine" against a mock returning instantly can still be a 20-request N+1 problem waiting to be exposed the moment it's tested against something with actual round-trip latency — which is exactly why testing purely against a mock that always answers in 0ms can hide this class of bug as thoroughly as testing against nothing at all.

X

Xerat02

Building MockBase.