AKJ
All posts
#performance#node.js#rest

Cutting API Latency by 40%: A Practical Refactoring Story

Apr 3, 20253 min read

Latency problems are sneaky. They rarely announce themselves — they accumulate quietly until someone notices the p95 response time has crept from 120ms to 340ms.

Finding the Bottleneck

We profiled three weeks of production traces and found 60% of time was spent in sequential database calls that could have been parallelised. Classic N+1 query problem dressed up in middleware.

The Refactor

The fix was conceptually simple: identify all independent data fetches in each request lifecycle and run them with Promise.all. The hard part was untangling implicit dependencies that assumed a specific execution order.

// Before
const user = await getUser(id);
const prefs = await getPrefs(user.id);
const feed = await getFeed(user.id);

// After
const [user, prefs, feed] = await Promise.all([
  getUser(id),
  getPrefs(id),
  getFeed(id),
]);

Results

P50 dropped from 210ms to 90ms. P95 from 340ms to 140ms. Zero breaking changes to the public API surface.