Skip to main content
← Back to Learn

The Fetcher That Held Six Gigabytes: Rewriting a Data Pipeline for the Shape It Actually Needed

A market-data fetcher kept dying mid-run — not from rate limits, but from holding entire response spans in memory. The fix wasn't tuning. It was making the wrong shape unrepresentable.

Every data pipeline has a moment where you stop asking "why is this slow" and start asking "why is this shaped wrong." We had that moment this week with a market-data fetcher that kept dying halfway through multi-hour pulls — not on API errors, not on rate limits, but on its own memory footprint.

The autopsy

The original design buffered an entire fetch span — every row from every paginated API response — into a single in-memory list, then serialized the whole thing at once. On small spans this was invisible. On the spans that mattered, each session carried millions of quote records, and each record was a heap-allocated object. Peak resident memory crossed six gigabytes.

At that size the failure mode wasn't an out-of-memory kill. It was subtler: garbage-collected pauses stacked until the process looked frozen — wall-clock minutes spent collecting, throughput collapsed, and supervisors (or humans) killed it mid-write. Which produced the second bug: a partial write fused two spans into one malformed record, and the resume logic silently marked the unfinished span complete. Real data loss, no error raised.

The resume path had its own tax. To figure out which spans were already done, the tool re-parsed the entire output file — tens of gigabytes of JSON — on every restart. After any interruption, getting back to work cost a multi-gigabyte read and a memory spike of its own.

What the rewrite actually changed

We ported the hot path to Rust — not for the language itself, but because the correct shape was easier to express than the wrong one:

  • Rows stream, spans don't buffer. Each response page is parsed, written through a buffered writer, and freed before the next page is fetched. A 36-million-record span now uses the same working set as a 90-thousand-record one. Peak memory is one page — by construction, not by discipline.
  • Resume is an index, not a re-parse. A sidecar file records completed span keys; restarts read a few kilobytes instead of re-scanning everything. The "did we already do this" question went from O(dataset) to O(1) per span.
  • Failure is loud and bounded. A consecutive-failure counter aborts the run after a short streak — a dead API key can no longer burn through an entire job emitting errors.
  • Lifecycle is a service, not a shell trick. The fetcher runs under systemd with restart-on-failure and boot enablement, so the multi-hour job survives reboots instead of depending on a detached terminal.

What we measured

Same span, before and after: roughly eight minutes under the rewritten interpreter version, roughly four under the compiled service. That ~2× came almost entirely from keeping connections alive — the interpreted client paid a fresh TLS handshake on every request — plus faster JSON parsing and zero collector pauses.

Memory told the bigger story: flat under two hundred megabytes through spans that previously peaked at six gigabytes. Not tuned flat — structurally flat. There is no code path left that could hold the whole span.

The actual lesson

None of these fixes were exotic. Streaming I/O, append-only indexes, retry budgets, service managers — all standard. What's interesting is how long the broken version survived because each flaw was survivable alone. Buffering worked until spans grew. Re-scanning worked until the file grew. A missing retry budget worked until a transient error appeared. Stacked together, they made a routine job look haunted.

That's the argument for writing long-lived pipelines in a language where the wrong shape is hard to express. In a managed language, results.append(page) is one line and streaming takes thought. In Rust, streaming is the path of least resistance — ownership makes the buffer-everything version the harder thing to write. The language didn't make the job faster. It made the wrong shape unrepresentable, which turned out to be the same thing.

The pipeline is a lot more boring now. That's the review we wanted.

Put it to work

The research above powers the product — try it or compare it: