man@mndev:~/portfolio/notes/backend
← portfolio

Why our GraphQL BFF was slow before the first byte

Time to first byte depends on the slowest thing a resolver waits for. How to find it, fix it, and keep it fixed.

Backend··1 min read

A backend-for-frontend (BFF) built on GraphQL is great for product teams: one query, exactly the fields the page needs. It also makes one thing easy to miss. Nothing reaches the browser until the slowest resolver on the critical path finishes. On one high-traffic page flow, that showed up as a high TTFB.

Measure per resolver, not per request

The overall request time tells you that it’s slow, not where. I added timing around resolvers and upstream calls. Apollo-style tracing, OpenTelemetry spans or a simple Server-Timing header all work. Then I read the trace as a waterfall. The pattern to look for is steps that run one after another but don’t depend on each other.

The classic: accidental sequential awaits

// Before: total time = a + b + c
const listing = await getListing(id);
const pricing = await getPricing(id);
const dealer  = await getDealer(id);

// After: total time ≈ max(a, b, c)
const [listing, pricing, dealer] = await Promise.all([
  getListing(id), getPricing(id), getDealer(id),
]);

Two related issues show up just as often:

Keep it fixed with a regression test

Performance fixes quietly regress when someone adds “just one more call”. I test the shape of the work, not the milliseconds:

it('resolves the page without serial upstream calls', async () => {
  const calls = trackUpstreamCalls();
  await execute(PAGE_QUERY, { id: 'abc' });
  expect(calls.maxConcurrentDepth()).toBeLessThanOrEqual(2);
  expect(calls.count('getDealer')).toBe(1);
});

Wall-clock assertions are flaky in CI. How many calls, and in how many sequential rounds is stable, and it’s what actually drives TTFB.

Takeaways

// related