Skip to main content
AboutWorkExperienceLogHireResume
  1. Home
  2. /
  3. Log
  4. /
  5. Removing the primary key fixed memory, then made streaming writes quadratic

5–8 Nov 2026/Praxis

Removing the primary key fixed memory, then made streaming writes quadratic

DuckDB’s primary-key index cannot spill, which stopped the Praxis replay well short of a million customers. A landing table with compaction fixed that at scale, then slowed the nightly benchmark to 575 events a second.

575 → 1,542 ev/snightly chaos benchmark without the index, before and after the compaction threshold (floor 1,218)

Written up 22 Nov 2026

Symptom

The Phase 14 scale run stopped before one million customers. Each warehouse shard ran out of memory at 4.9 to 6.1 million rows with a 512 MB DuckDB memory limit.

Root cause

The event table had a primary key on event_id, and DuckDB holds a primary key as an ART index in memory, about 180 MB per million rows, which it cannot spill. One million customers produce about 97 million events, which puts the index alone near 17 GB. It existed for one reason: exactly-once rows under at-least-once delivery, where duplicates, redeliveries, crashes and replays are all expected.

Fix

I wrote the options down before choosing (ADR 0028). Sharding harder only moves the wall, since memory per shard is unchanged. A Bloom filter or a set of seen ids in Python still grows with the number of events, and loses exactness on restart unless it is persisted. Partitioning the table by day makes each index smaller, but DuckDB keeps every attached index in memory, so the total is the same. Parquet files deduplicated at read time would charge every reader, from dbt to forecasting, on every query.

The chosen design drops the primary key. A write deduplicates its batch, appends it to a landing table and commits, which is what allows the message to be acked. When enough landed rows are waiting, one transaction moves into raw.sim_events the first-landed row for each event_id that is not already there on the same event_date, then empties the landing table. The anti-join only looks at the dates being landed.

That restriction is exact only because of an invariant. The simulator derives event_id from content and occurred_at never changes on redelivery, so a duplicate always carries the same event_date. The invariant is checked directly (duplicate_event_ids() is reported and gated at zero), and a Hypothesis test asserts that the streaming and batch loaders produce the same table for every compaction threshold.

Verification

The scale steps and their pass criteria were registered on 5 November, before any of them ran, inside a Linux cgroup that emulates my 8 GB M1: 4 GB for the harness with swap disabled, 1 GB for Postgres. This is an emulation, not an M1 measurement.

Emulated M1 budget, synthetic data (simulator seed 1, fault seed 701, 28 days)
CustomersEventsShard RSS maxDuplicate ids
100K9,737,517631 MB0
250K24,343,371648 MB0
500K48,732,359662 MB0

Every step passed all 12 checks. Source: Praxis docs/evidence/final-scale.md. The 500K step took 3.1 hours and its Postgres container touched 14.5 MB of swap, disclosed as a deviation. The 1M step was not run.

Follow-up

Three days later the first nightly performance run after the change failed. The 1K-customer chaos benchmark ran at 575 events a second against a floor of 1,218, with the simulator side unchanged.

The library default was compact_every = 0, compact on every write, so that a write is visible as soon as it returns. The CLI streaming writers used that default, and with micro-batches of about six rows they compacted after every one. Each compaction anti-joins against raw.sim_events, which now has no index and grows with the stream, so total cost grew with the square of the stream length. The scale ladder never saw this because its configuration already set the threshold to 50,000 rows.

An A/B with only the threshold changed gave 1,572.9 events a second and the same oracle match. The streaming writers now use 50,000, the value already registered for the ladder rather than one tuned on this benchmark, and call flush() before reading counts. A test pins per-threshold compaction, and an existing test that compares warehouse rows with published events fails without the flush. The re-run passed at 1,541.9 events a second.

Status

Closed. The cost is visibility: a streaming writer’s rows are durable in the landing table at once but reach raw.sim_events only at the next compaction or flush. Throughput is about 14% below the Phase 16 measurement of 1,800.9 events a second, and the baseline was not re-recorded to absorb the difference.

Sources

  • ADR 0028, including Amendment 1 of 8 Nov 2026
  • docs/evidence/final-scale.md
  • Commit 5602da1: scale ladder and compaction fix

Read the Praxis case study

OlderA 1% crash rate dead-lettered 1,305 healthy messages
NewerA forecast that scored near-perfect because it could see the answer
HomeProjectsAboutExperienceLogHireContactResumeGitHubLinkedIn
© 2026 Bhargav Kr Nath