Can't even trust myself: raggio's 155-second cold start, now 4.6

■
Can't even trust myself: raggio's 155-second cold start, now 4.6
Table of Contents

Part 1 was the loop. This part is about the first thing the loop has fixed.

Can’t even trust myself these days

The first post says raggio needs 30.4 seconds from a cold start to its first search. That wasn’t a true cold start. Linux keeps recently read files in memory, the page cache, and a real cold start, like the first one after a reboot, finds none of raggio’s files there. When the agents evicted them and started a fresh 4 GiB container, the first search took about 80 seconds, and the first request for the collection’s info took 120 to 125 (155 in the final A/B). My published run had started with part of the data still cached, and I never wrote down how much.

So here’s the first lesson of the program: a benchmark number that doesn’t say what state the machine was in will eventually lie to you, and you’ll be the one who published it. I’ve added a note to the first post’s table.

Two minutes to say hello

Where did two minutes go? A collection loads on its first request, that’s the “only the collections people are actually querying pay rent” trick from the first post. The vector index itself loaded in under two seconds. Everything else went into full scans of the SQLite table that holds the chunks, all 3.57 GB of it:

  • 39 seconds to count the chunk and summary rows
  • 38 seconds to reconcile the index with the database. A crash between a database commit and an index sync can leave vectors in the index whose rows are gone, so every load compared all the ids on both sides
  • 39 seconds to count them all over again, for the statistics that GET /collections/{name} returns

(gotta appreciate the consistency tho) In a container, the page cache counts against the memory cap, and a 3.57 GB table plus a 1.6 GB index don’t fit in 4 GiB. So the table never stayed cached from one scan to the next, and every scan read it from disk all over again.

That explains why it was slow, but not why, for those two minutes, raggio didn’t answer anything else either. raggio is an asyncio service: a single thread, the event loop, runs every request handler, and it switches from one to the next whenever a handler waits on something, like the network. That’s very efficient, as long as no handler does heavy work without letting go of the thread. Bad luck, the collection load did exactly that: it ran synchronously on the event loop, so for two minutes the only thread that answers requests was busy scanning SQLite. Every other request waited in line, /healthz included, and any orchestrator would have happily killed the container for being dead.

Even worse, very GET /collections/{name} ran that statistics scan again, still on the event loop: one request for a collection’s info, and nobody else got an answer for about 41 seconds (52 in the final A/B). Damn.

Fig. 1 Cold start, second by second

The “before” lane uses the phase timings of one true-cold load on the original code (2.55M vectors, IVF index, 4 GiB container), 120.4 s in total. The “after” lane is plan C’s acceptance run, median of three true-cold starts. /healthz is drawn every 5 s to keep it readable; the real probes ran every 0.25 s, and before the fix the slowest one waited between 79 and 124 seconds, depending on the run.

Three tiny columns, one fat table

Those three scans wanted very little from the table: each row’s id, its type, and whether it’s indexed. But the table also holds every chunk’s text and metadata, so reading three tiny columns meant reading all 3.57 GB. The fix is trivial, and it would never have been an issue to begin with if I had developed the solution completely by myself: a covering index. An index on (doc_id, type, indexed) holds every column those queries need, in a structure that’s a fraction of the table’s size, so SQLite answers them from the index and never touches the table. Building a collection went from reading 8.8 GB in 80 seconds to reading 1.7 GB in 3.4, and most of those 1.7 GB are the vector index itself.
You can see that LLM have lots of training set data coming from developers, as it missed something so trivial as an index covering the heaviest queries, so DBA jobs are still fine for a while.
The id comparison between the index and the database also moved from Python sets to numpy, from up to 0.7 seconds to 0.02.

Fig. 2 Three tiny columns, one fat table

One row of the chunks table against the three columns the scans needed, then the bytes a true-cold start read before and after the covering index. Before: one true-cold load on the original code plus the first GET’s statistics scan. After: plan C’s acceptance run. Hover a segment for its seconds.

Now, the load now runs on a worker thread, the request that triggers it still has to wait, but it waits by awaiting the thread, and that hands the event loop back to everyone else. While a 2.55-million-vector collection loads, /healthz answers in 3 milliseconds instead of waiting up to two minutes. The statistics behind GET /collections/{name} moved to a thread too, and with the covering index under them they take half a second instead of 41.

The acceptance run on the DGX checked ten things in one go: the first answer after a true cold start in 4.8 seconds on IVF and 4.2 on flat (the gates were 6 and 5), /healthz never slower than 3 ms during a load, later GETs in about half a second, and seven more on disk reads, memory, the migration and the sampling. Ten out of ten, first try. In the final A/B against the original code, the cold start went from 155 seconds to 4.6, and the slowest GET after it from 52 to 0.5.

Reading 16.9 GB to pick a random sample

Building an IVF index, the optional index from the first post, starts with k-means, and k-means trains on a random sample of the vectors: 65,536 of them here. raggio picked them with the query everybody writes the first time:

SELECT v.vec FROM vecs v JOIN records r ON r.id = v.id
WHERE r.indexed = 1
ORDER BY RANDOM() LIMIT 65536

Think about what the database has to do with it: join every one of the 2.55 million vectors with its record, give each pair a random number, sort the lot, and keep the first 65,536. On a cold DGX that query read 16.9 GB from disk and took 110 seconds.
Again, DBAs are going to be fine for a while, as LLMs have a behavior way more akin to devs than to them.

The fix: when the ids are dense, which they are in a collection that’s mostly appended to, pick 65,536 random ids in Python and read just those rows by primary key. 829 MB read, 8 seconds cold, 0.4 warm. If the ids are too sparse for that to find enough rows, it falls back to the old query. The same plan also removed a full table scan and a join that ran before every build, checking which rows had no stored vector; now the build finds them from its own stream. A cold index build went from 556 seconds to 186: the sample from 152 seconds to 11, and the checks before it from 178 seconds to under 2.

The index that slowed one query

The covering index wasn’t free. It has to be built once, the first time the new version opens an existing collection: 49.6 seconds on a cold disk and 92 MB of extra space at 2.55 million rows. And it made one query slower. The page that lists chunks sorted by a metadata field went from 0.91 seconds to 2.73, because SQLite’s query planner saw a new index containing type and used it to find the chunk rows. In this collection almost every row is a chunk, so the index filtered out nearly nothing, and it added a jump back to the table for every row to read the metadata the query sorts on. Like in a bad dream, the LLM decided to pin the query a plain scan with an hint (NOT INDEXED), which seems to work good enough tbh.

Next Monday

Part 3, October 26: memory. The headroom guard that said yes right before the kernel killed the index build, and the malloc setting that fixed it and slowed filtered search by a third.

None of this needed anything I didn’t already know. A covering index and not blocking the event loop: I’d have told you both in a code review. I just never looked, because the number I had said everything was fine.

Related Articles