Skip to content

Compute the slow breakdown where nothing is waiting for it - #150

Closed
ralyodio wants to merge 2 commits into
mainfrom
crawlstats-speed
Closed

Compute the slow breakdown where nothing is waiting for it#150
ralyodio wants to merge 2 commits into
mainfrom
crawlstats-speed

Conversation

@ralyodio

Copy link
Copy Markdown
Contributor

Follow-on to #149. That took /api/crawlstats from 118s to 20.2s — and then it sat at exactly 20.2s on every request, because the remaining time was categoryStats hitting the timeout, failing, caching nothing, and being asked again by the next reader.

Why it can't be made cheap

Decomposed against production:

piece time
feeds per category 6,340ms index-only
growth by day+category 1,046ms index-only
status × category 23,942ms
sum(item_count) per category 35,440ms
crawled-in-last-day per category 40,020ms

The slow three read columns no index covers, so they fetch every one of 476,715 rows. An index covering them would have to carry status and last_success_at, both rewritten on every crawl — buying a fast chart with a slower crawler, when writes are the one thing this system has none of to spare.

The 30s ceiling was ours

It's TURSO_REQUEST_TIMEOUT_MS, not a limit of the database. Given a longer deadline the whole statement completes in 58.9 seconds.

The fix

The query is unchanged and simply stops being on the request path. The poller recomputes it every five minutes on a connection with a patient deadline (connect({ timeoutMs }), new and per-connection) and primes the same Redis key the web service reads. A reader finds it already there.

Safe to run beside the crawler: it's a read, and reads don't queue behind the single writer everything else contends for. One long read every five minutes costs the crawler nothing.

primeCache goes through the same envelope remember writes, so the warmer and the reader can't drift on the format. Without REDIS_URL it's a no-op — the local and test case, where the poller simply doesn't warm.

The warm is logged as stats-warm-skipped rather than …-error on failure, deliberately: toEntry puts anything ending in "error" on the operational-alarm panel, and a missed warm isn't an alarm — the cache just keeps serving what it had.

Full workspace suite green (11 packages, 0 failures); web build clean; poller parses.

🤖 Generated with Claude Code

ralyodio and others added 2 commits August 21, 2026 15:30
/api/crawlstats answered in 118 seconds. It fans out eleven reads and returns
when the slowest does; timed against production:

    categoryStats            30,005ms  (timed out)
    jobBacklogs              11,255ms
    failingFeeds(20)          5,172ms
    crawlStats                4,975ms
    the other seven          under 600ms each

No rewrite fixes `categoryStats`. It is a group-by over 476,715 rows, and on
the same connection a bare `count(*)` of that table is 6.9s while `select
category, count(*) … group by category` does not finish inside the client's 30s
deadline. Dropping its conditional aggregates -- the fix that worked for
`crawlStats` in PR #96 -- changes nothing, because the cost is visiting every
row for a column no index covers.

The per-process cache that was already here could not save it either, for a
reason worth naming: `categoryStats` does not run slowly, it *fails*, and a
cache that only stores successes stores nothing. Every request paid the full
timeout, for ever. Redis plus serve-stale-on-failure inverts that -- one
success, any time, serves every later reader -- and it survives the deploys that
emptied the old cache. It also adds no writes to Turso, whose write path is the
binding constraint on everything else here.

The part that needed care is that a status page must never report a stalled
crawler as healthy. The rule that keeps it honest is to cache facts and derive
anything measured against now: `idleMinutes` is `now - lastSuccessAt` computed
inside the query, so caching the object freezes it, and a dead crawler would go
on reporting the same cheerful number. `liveStats` caches the timestamp and
redoes the subtraction, so the number climbs while the crawler is down.
`queueHistory` does the same with its hour labels, caching the sparse rows and
filling the window on the way out.

A cache that can hang is not a cache, so the lookups are bounded too, and every
failure path -- no REDIS_URL, a refused connection, a socket that accepts
commands and never answers -- falls through to the read it replaced.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
/api/crawlstats came down from 118s to 20.2s, and then sat there exactly, on
every request. The remaining 20 seconds were `categoryStats` hitting the
timeout, failing, caching nothing, and being asked again by the next reader.

Decomposed against production, the reason it cannot be made cheap:

    feeds per category            6,340ms   index-only
    growth by day+category        1,046ms   index-only
    status x category            23,942ms
    sum(item_count) per category 35,440ms
    crawled-in-last-day          40,020ms

The slow three read columns no index covers, so they fetch every one of 476,715
rows. An index covering them would have to carry `status` and `last_success_at`,
both rewritten on every crawl -- buying a fast chart with a slower crawler, when
writes are the one thing this system has none of to spare.

The other half is that the 30-second ceiling was ours. It is
`TURSO_REQUEST_TIMEOUT_MS`, not a limit of the database, and given a longer
deadline the whole statement completes in 58.9 seconds.

So the query is unchanged and simply stops being on the request path. The poller
recomputes it every five minutes on a connection with a patient deadline and
primes the same Redis key the web service reads, so a reader finds it already
there. It is a read, and reads do not queue behind the single writer, so this
costs the crawler nothing.

`primeCache` goes through the same envelope `remember` writes, so the warmer and
the reader cannot drift on the format, and it is a no-op without REDIS_URL --
which is the local and test case, where the poller simply does not warm.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@ralyodio

Copy link
Copy Markdown
Contributor Author

Superseded by a clean rebase onto main after #149 was squash-merged.

@ralyodio ralyodio closed this Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant