feat(extract): stream sources with bounded memory#10
Merged
Conversation
Replace the load-everything-then-upload pipeline with a streaming one: - extractor: streamCustomers() async-generates rows via Postgres/MySQL server-side cursors (pg-query-stream / mysql2 row streams) and Stripe lazy pagination. Add batchAsync() to group the stream into bounded batches, and keep extractCustomers() as an array-draining convenience. - sync: introduce createSyncSession() with send()/finalize(). Each hashed batch is fanned out to both platforms (re-chunked to each one's limit); Google's offline job is created lazily on the first batch and run on finalize. Per-platform failures are captured without aborting the other. - index: runSync now loops extract → hash → upload one batch at a time, so the full audience is never materialized in memory. Adds bounded-memory streaming tests for batchAsync. Documents the memory characteristics in the README and CHANGELOG. Closes #2 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #2.
What
Replaces the load-everything-then-upload pipeline with a streaming one so peak memory stays flat regardless of audience size.
streamCustomers()async-generates rows via Postgres/MySQL server-side cursors (pg-query-stream/ mysql2 row streams) and Stripe lazy pagination. NewbatchAsync()groups the stream into bounded batches;extractCustomers()remains as an array-draining convenience for callers/tests.createSyncSession()withsend()/finalize(). Each hashed batch fans out to both platforms (re-chunked to each one's own limit). Google's offline job is created lazily on the first batch andrunon finalize. A failure in one platform is captured per-platform without aborting the other or the source stream.runSyncloops extract → hash → upload one batch at a time; the full audience is never materialized.Why
The previous implementation buffered the entire result set (
pool.query) and the entire hashed array in memory — fine for small lists, but it undercut the "lightweight" positioning for multi-million-row audiences. (See #2.)Tests
test/extractor.test.tscoversbatchAsyncgrouping, the empty stream, size validation, and laziness (only one batch is pulled ahead → bounded buffering).typecheck,lint,format:check,buildall green locally.Note
The DB streaming code paths require a live Postgres/MySQL to integration-test and aren't exercised by CI (no DB in the runner). The pure batching utility is unit-tested; the driver wiring is type-checked and built.