Blog · Backend · · 3 min read
Serving 13.6 million taxa from read-only SQLite
Ptree serves eleven taxonomic backbones totaling 13.6 million taxon records, along with rolled-up properties, photos, sounds, biotic interactions, range maps, and full-text search, directly from local SQLite files. There is no external database server. One Node cluster handles HTTP traffic using Node's built-in node:sqlite (DatabaseSync), keeping memory predictable and cold lookups down to a few milliseconds.
Immutable mode skips locks and filesystem checks
Every served SQLite store is built offline into a staged generation and swapped atomically into place. Serving processes never write to those databases, so the API opens every store as an immutable URI:
new DatabaseSync(`file:${path}?immutable=1`, { readOnly: true });
Passing ?immutable=1 tells SQLite that no other process can modify the file while it is open. SQLite skips shared-memory WAL setup, acquires no file locks, and stops calling stat() before every read to check whether another writer updated the change counter. Under concurrent load across eight worker processes, immutable read-only databases behave like memory-mapped lookup tables backed directly by the Linux page cache.
Eight HTTP workers plus one bounded heavy pool
Most requests touch only a handful of indexed rows. Fetching a taxon card, reading a page of 50 children, switching backbones through the precomputed navigation table, or looking up a sound all finish in a millisecond or two on the HTTP worker's own event loop. A few operations, however, run queries that take hundreds of milliseconds or aggregate across large rollups. Running those synchronous DatabaseSync calls on an HTTP worker would block every other request that shares that worker.
The API server separates the two workloads with node:cluster:
- Eight HTTP worker processes accept connections and answer fast synchronous SQLite routes directly.
- The cluster primary owns one shared heavy pool backed by dedicated
worker_threads.
When an HTTP worker receives a heavy route, it forwards the request over Node IPC (heavyReq / heavyRes) to the primary. Centralizing the heavy pool in the primary gives Ptree three advantages that an eight-way per-worker pool cannot:
- The pool tracks every running request in an
inflightmap indexed by route. If twelve visitors or a bot burst hit the same expensive endpoint at once across different HTTP workers, the pool runs it once and sends the result back to all twelve callers. - The queue caps pending work at 32 requests per heavy thread and drops abandoned requests after 280 seconds, so disconnected clients cannot pile up unbounded work.
- After each job, a heavy worker checks its memory usage against 75% of its heap ceiling. When a thread crosses that line, the pool retires it cleanly and spawns a fresh replacement so SQLite page caches and temporary buffers do not grow without bound.
Caching only slow responses per generation
Fast queries do not need a cache, and caching everything only duplicates pages that the Linux page cache already holds in RAM. The page cache writes a response into a small generation-stamped SQLite file (WITHOUT ROWID) only when generating that response took more than 50 ms.
Cache keys include a composite token built from the API's interfaceVersion, the current childOrder(), and the exact size and modification time of the backing store files. Rebuilding a data generation or changing how children sort invalidates stale entries automatically without manual cache purges. Cold slow endpoints drop from hundreds of milliseconds to sub-millisecond reads on the second visit.
Because the database files sit on local NVMe beside the API process and every store is read-only and generation-stamped, there are no connection pools to size, no ORM layers to tune, and no network hops between application code and storage.