–/Customer Intelligence Platform
The full pipeline froze the machine because two things could not spill
The first full-data run pushed resident memory to about 8.5 GB on a 10 GB machine and froze it: a memory limit above physical RAM, mode() aggregates that do not spill, and a Categorical cast of 23 million session IDs.
~8.5 GB → ~3 GBpeak resident memory of the full pipeline, before and after; no swap after
Written up
Context
The Customer Intelligence Platform had been developed against a sample. In September I ran it on the full Kaggle dataset for the first time: 109.95 million events from October and November 2019, 23,016,650 sessions and 5.32 million users, on a machine with 10 GB of RAM. The scripts still carried settings from the original 16 GB machine, SET memory_limit='10GB' or '12GB' and four threads, hard-coded per script.
The project’s memory story rested on a printed estimate. The old Parquet optimisation script ended with a per-column table, user_session (Categorical): ~200 MB (est) and TOTAL ESTIMATED: ~3.7 GB in memory. Those lines were string constants, not measurements.
Symptom
Sessionization, a GROUP BY user_session over the full event table, drove resident memory to about 8.5 GB, exhausted swap and hard-froze the operating system. Separately, the Polars pass that produces the Parquet input ran out of memory on machines much below 16 GB.
Root cause
Three causes, each responsible for part of it:
- The memory limit was above physical RAM. DuckDB spills when it reaches
memory_limit; with the limit at 10 GB on a 10 GB machine, the operating system ran out first. - The session query computed
mode(category_code), and the user feature query computedmode()of weekday and time of day.mode()is a holistic aggregate: each group keeps a frequency table of its values, and DuckDB does not spill that state, somemory_limitdoes not bound it. Across 23 million session groups, with four threads each building partial hash tables, it grew until the machine stopped. - The optimisation pass cast every string column to
pl.Categorical,user_sessionincluded. Categorical encoding of a near-unique column saves nothing and needs a dictionary with one entry per distinct value, here 23 million UUIDs. At 36 bytes each that is about 830 MB of string data before any hash-table overhead (an estimate, not a measurement), already four times the printed figure.
Fix
One module, src/utils/duckdb_env.py, now configures the connection for every pipeline script: a 3 GB memory limit, two threads, a spill directory and preserve_insertion_order=false, each overridable by environment variable on a larger machine. Two threads instead of four halves the number of partial aggregate tables alive at once, and the spill directory gives the limit somewhere to put them.
The mode() calls went two different ways. Nothing downstream read the session-level top_category, so it was deleted rather than rewritten. The user-level preferences became a count per user and weekday, and per user and part of day, followed by arg_max(bucket, count) for each user. Both are ordinary aggregates that spill.
The two Polars passes became one streaming DuckDB COPY … TO from read_csv into Parquet. The projection narrows the product and user IDs to 32-bit integers and price to FLOAT, leaves user_session as a plain string, and lets the Parquet writer dictionary-encode the low-cardinality columns per row group, where dictionary encoding pays.
Verification
The full pipeline now completes with a measured peak of about 3 GB of resident memory and no swap, and the Parquet output is 1.82 GB against 13.7 GB of CSV. The project page and README now quote these measured figures; the 3.7 GB estimate and the 97% reduction built on it are gone.
Status
Closed. The 3 GB default was chosen to fit this machine with headroom, not tuned, and the environment variables raise it elsewhere. The ~40 GB the project quotes for a naive pandas load is still an extrapolation from 389 bytes a row on a 3-million-row sample, and is labelled as one.