← Back to PureKit

Exporting 120M Audit Rows: Cutting Cgroup Memory from 19.5 GB to 223 MB

#Linux Page Cache#cgroup#SQLite#fadvise
Exporting 120M Audit Rows: Cutting Cgroup Memory from 19.5 GB to 223 MB

The Problem

Exporting multi-day protocol audit logs—120 million rows spread across four 9.9 GB SQLite shards—kept failing or hitting cgroup memory watchdogs. The legacy code even enforced a hard 5-million-row ceiling, yet at just 4.99 million rows its cgroup usage_in_bytes surged to 19.55 GB (including ~10.5 GB of Linux page cache), tripping the 20 GB slice cap.

Why Memory Exploded

Three design flaws compounded page-cache pressure inside the cgroup:

  • OFFSET pagination repeatedly scanned massive SQLite tables.
  • Entire daily SQLite files were copied (cp) into a temporary directory before reading.
  • All CSV files were written out and compressed in one giant zip -r pass at the end.

Streaming & Page-Cache Engineering

  1. In-place streaming + chunk-zip-delete: Read SQLite shards directly in place, emit 500k-row CSV chunks, immediately append to a Zip64 archive with zip -1, and delete each chunk before starting the next—removing the 5M row limit completely.
  2. 2.1× throughput boost with zero extra RAM: Switching to byte-level streaming IO + zip -1 cut per-500k-row shard time from 15.4s down to 7.2s (projecting ~29 min for 120M rows), while holding peak cgroup usage flat at 222.9 MB (RSS ~103 MB)—a 98.8% reduction from 19.55 GB.
  3. Taming external download page cache on ext4: When fetching 12.8 GB remote DB shards via a CLI downloader, calling posix_fadvise(POSIX_FADV_DONTNEED) alone failed to reclaim dirty pages (cache still climbed to 6.9 GB). Pausing the download process group briefly every 0.2s (SIGSTOP → fsync + fadvise → SIGCONT) kept page cache strictly bounded throughout the download.

Enjoyed this note or seen a similar bug?

Feel free to drop a line—always glad to exchange ideas with fellow engineers.

Say hi · hi@purekit.net