[Bug] usage.json unbounded growth causes O(n) disk I/O per request, triggering MacOS OOMKill of the VM
What happened?
Every API call proxied through routerly triggers a full read-parse-rewrite
of the entire usage.json file (appendUsageRecord() in
packages/service/dist/config/loader.js), not just an append of the new
record. The file has no size cap, rotation, or archiving, so it grows
without bound. In our case it reached ~39 MB after normal usage over a
couple of weeks, meaning every single request costs a full 39 MB read +
39 MB JSON.stringify + full write + rename.
Running under Docker with /data bind-mounted from the host (OrbStack on
macOS), this write amplification was severe enough to spike host disk I/O
to the point where macOS/OrbStack killed the container runtime VM
("high I/O" resource termination). We only found this after ruling out the
other two containers we run alongside it (no sustained disk activity there)
and tracing routerly's /data/data/usage.json growth + the read-modify-write
code path directly.
Notably, the code already has an inline comment acknowledging this as a known tradeoff:
// ponytail: whole-file read-modify-write per usage append under this global
// lock is O(n) per record; if write throughput ever demands it the upgrade is
// append-only usage writes (NDJSON append), not a bigger retry budget.
This report is real-world confirmation that write throughput does demand it — this isn't just a latency concern, it can crash the host runtime.
This makes routerly almost unusable in production because can trigger macOS OOMKill anytime.
Steps to reproduce
- Run routerly in Docker with
ROUTERLY_HOME//databind-mounted to a host directory (not a named volume). - Proxy a sustained volume of chat/completion requests through it over
time (days/weeks of normal usage is enough for
usage.jsonto reach tens of MB). - Watch
$ROUTERLY_HOME/data/usage.jsongrow with every request — no rotation or truncation ever occurs. - Once the file is non-trivially sized, watch host-level disk I/O
(
docker statsBLOCK I/O, or hostiotop/Activity Monitor) during request traffic: each request causes a full-file write burst proportional to total historic usage record count, not to the single new record.
Expected behaviour
Recording a usage event should be an O(1) append (e.g. NDJSON line append), independent of how much historic usage data already exists. There should also be some retention/rotation policy so the file doesn't grow unbounded forever.
Actual behaviour
appendUsageRecord() reads the entire usage.json, parses it, pushes one
record, re-serializes the entire array, writes it to a temp file, and
renames over the original — for every single request:
// packages/service/dist/config/loader.js
export async function appendUsageRecord(record) {
if (process.env['ROUTERLY_SKIP_TRACKING']) return;
const existing = await readConfig('usage'); // reads + parses whole file
existing.push(record);
await writeConfig('usage', existing); // stringifies + writes whole file
}
export async function writeConfig(key, data) {
// ...
// Atomic publish: full content to temp, then rename over the target.
await writeFile(tmpPath, JSON.stringify(data, null, 2), 'utf-8');
await rename(tmpPath, filePath);
}
There is no size cap, max-age, or rotation for usage.json anywhere in the
codebase (checked settings.json and grepped packages/service/dist for
retention|rotat|maxUsage|usageLimit — the only retention logic that
exists is for the unrelated notifications inbox, #91).
Cost grows non-linearly over the life of an instance: negligible on day 1, but a long-running deployment with meaningful traffic eventually turns every request into a multi-MB-plus disk write, which is amplified further by bind-mount/virtiofs overhead on Docker Desktop/OrbStack on macOS, severe enough in our case to get the container runtime killed for excessive I/O.
Environment
- Routerly version: root
package.json0.2.0,@routerly/service0.3.0 - Installation type: Docker (
com.docker.compose.service=routerly) - OS: macOS Sequoia (OrbStack 2.2.1 as the container runtime)
- Node version: N/A (Docker image, Node 25.9.0 inside container)
Logs or error output
No in-app error — the container itself was healthy. The signal was purely
host-level: OrbStack/macOS terminated the runtime citing high disk I/O,
and docker stats correlated block I/O with usage.json size and request
volume once we isolated the container.
usage.json size at time of investigation: 39,626,571 bytes (~39 MB)
File modify pattern: full-file rewrite (temp + rename) on every appendUsageRecord() call
Additional context
Suggested fix:
- Switch usage records to append-only NDJSON (one JSON object per line via
fs.appendFile) instead of a single JSON array rewritten wholesale — turns the hot path into an O(1) append, matching the existing inline "ponytail" comment's own suggested direction. - Add a retention/rotation policy for the usage log (max age or max size,
with archive-and-truncate), similar to what already exists for the
notifications inbox (
#91). - If the array format must stay for backward compatibility (dashboard reads it directly), consider a background compaction job instead of a synchronous whole-file rewrite on the request hot path.
Workaround applied on our side: backed up and truncated usage.json to
[] to relieve immediate I/O pressure. Not a real fix — usage history was
lost and the same O(n)-per-request cost will return as the file re-grows.
Confirmed. appendUsageRecord() in packages/service/src/config/loader.ts does a full read-parse-rewrite of usage.json per request via the generic writeConfig() (whole-array JSON.stringify + temp + rename), so cost scales O(n) with historic record count — matches the existing inline ponytail comment at loader.ts:143 flagging this exact tradeoff.
Fix direction: append-only NDJSON writes for the hot path + retention/rotation policy (precedent: notifications inbox retention from #91), keeping UsageRecord[] shape on read for dashboard/CLI consumers.
Thanks for the detailed report. Usage data is now stored in an append-only format, so each request no longer rewrites the whole file and disk I/O stays constant as history grows. Existing data is migrated automatically. This has been available since v0.5.0, so I'm closing this issue. Please reopen it if you still see the problem on a recent version.