← Binderdex dev log

Anatomy of a pool starvation: the crawler never touched search

#web#data#tcg

A ring of twelve occupied slots surrounds a clock, and outside a closed bar an amber dot waits at the head of a fading queue

On the night of October 4, a crawler walked the binder-idea pages on Binderdex in one fast burst. Every request was answered and nothing errored. The first thing users saw was nowhere near those pages: card search in the mobile app started failing.

Binderdex is a Pokémon card collection and price app. Its binder-idea pages are a public gallery of ready-made, themed 3x3 binder pages that collectors browse and search engines crawl. Each page shows a panel of related idea pages, and building that panel meant querying the catalog of ideas.

Each of those renders ran the same three catalog scans, which are queries that read through that catalog, and the related-lists panel had no cache in front of it, so all three hit Postgres every single time. The database saturated and the connection pool started timing out.

A connection pool is a queue with a clock

A pool hands out a fixed number of database connections. When all of them are busy, the next query waits in line, and if the wait outlasts a deadline, that query fails. Three slow scans per render don't look dangerous until a burst multiplies them. Then every query in the system waits behind the crawler.

That's why the incident title in my error tracker pointed the wrong way. It named a subject lookup, a query for the theme behind an idea page, which was only the first query to give up waiting. The scans ahead of it in line were the real cost.

THE BURST night of October 4 Crawler burst on binder-idea pages Every render: 3 catalog scans each, no cache Connection pool: every slot waiting Card search in the mobile app database saturated A BYSTANDER nowhere near those pages waits behind the crawler search starts failing
The crawler never touched search. Both were waiting on the same pool.

Key the cache on what the data depends on

The fix is boring on purpose. Related lists depend only on the page's category and archetype, two labels for what kind of idea page it is, so they now sit behind a cache keyed on exactly those two values, revalidated every hour.

const related = unstable_cache(
  () => listRelatedSubjects(category, archetype),
  ["related-subjects", category, archetype],
  { revalidate: 3600 },
);

Pages that share a category and archetype now share one cached result. A crawl pays for each pair once an hour instead of once per page, and a crawler stops being an incident.

The index that couldn't say no

The same night's error queue held a second lesson, about column order. The card page was slow to show graded prices, the market prices for cards graded by a professional company. One query had a p95 of 1.2 seconds, meaning the slowest 5% of its calls took longer than that.

Prices live in a partitioned table, which is one logical table stored as many smaller ones. The best available index was on (card_id, recorded_at). The query also filters by source, and Postgres couldn't use that index for the source filter. So every partition ran a backward scan through time and threw its rows away on the filter.

One source has only had data since mid-May. For a hot card, that meant sixteen older partitions scanned at roughly a thousand rows each, returning nothing.

The new composite index is declared in the schema and built per partition.

-- column order: card_id (equality), source (equality), recorded_at (sort)
CREATE INDEX graded_prices_card_source_recorded
  ON graded_prices_p2026_05 (card_id, source, recorded_at DESC);

Column order does the work: equality on the card, equality on the source, then the sort order. An index can only narrow a search by the columns it holds, in the order it holds them. With source in second place, a partition with no matching rows can answer immediately instead of scanning to find that out.

Receipts

I don't trust a feeling on this kind of change, so I measured it twice. On scratch partitions seeded with 300,000 noise rows, the old plan removed about a thousand rows per stale partition in 1.29 ms. The new plan returns zero rows per stale partition in 0.34 ms.

In production, EXPLAIN (ANALYZE, BUFFERS) on a hot card with roughly 25,000 price rows touched 16,291 buffers and reported 259 to 1,409 rows removed by filter on every stale partition. That run was cold-cache, so its 6.1 seconds is inflated. The shape of the plan is the evidence.

Three things to steal

This is the second database post in a row, and the catalog pipeline one is its sibling. If you run a small pool behind a render path, take these with you.

  • Key caches on real dependencies. The related lists never varied within a category and archetype, so computing them per page paid for a difference that didn't exist.
  • On partitioned tables, a missing index column is a per-partition tax. Column order decides which partitions can answer "no rows" and skip the scan.
  • The alert names the innocent. The query in the error title was the first waiter for a connection. Read the plan before you believe the title.

This is the build log for Binderdex (www.binderdex.com) · All entries · Weekly essays