ADR-00043 — NDJSON Streaming and Wave-Barrier Execution for Tenant Catalog Migration

By Engineering Team•October 11, 2026•6 min read

Status: Accepted Date: 2026-10-11 Authors: Engineering Team

Context

tenant-migration’s verify step compiles each entity type’s uploaded CSV rows into a structured output that tenant-migration’s own execute step, and tenant-migration-orchestrator’s per-record dispatch worker, both need to read back later — execute reads it once per wave to publish records, and dispatch reads a single record’s worth of it per Pub/Sub message, potentially minutes or hours after it was compiled.

Two implementation questions this pipeline had to settle, both visible directly in the current code and in artifacts of an earlier shape still called out in tenant-migration’s and tenant-migration-orchestrator’s own documentation:

  1. How is a compiled entity type’s output stored and read back? The current code reads one record at a time via byteOffset/byteLength slices (readMigrationRecords, readMigrationRecordAt) against a line-delimited file. Both services’ documentation explicitly describes this as a replacement for an earlier recordIndex-based lookup against a single JSON array per file, which required re-parsing the whole file on every message — O(n²) across a wave of any real size.
  2. How does execute decide a wave of records is safe to publish? The current code (ExecuteService.doExecute/waitForWave) gates each of its four fixed stages as a whole — stage N+1 is never published until every entity type in stage N has finished. The orchestrator additionally re-checks each record’s own dependsOn entity types before dispatch (checkDependenciesReady), independently of whatever execute already waited for.

Decision Drivers (FRs & NFRs)

  • SR-029 FR-07, FR-14 — compiled output must be producible and consumable without loading a whole file into memory.
  • SR-029 NFR-02 — no step in the pipeline loads a whole file into memory.
  • SR-029 FR-12, FR-18 — a wave must be safely gated before publishing, with a defensive backstop at dispatch time.
  • SR-029 NFR-05 — a stalled wave must fail with a bounded timeout, not hang forever.

Decision 1 — Compiled-record storage and read shape

Option 1 — Single JSON array per entity type, read by recordIndex

Each compiled entity type’s records are written as one JSON array file; a Pub/Sub message carries the record’s recordIndex, and the reader parses the whole array to pull out that one index.

Pros:

  • Simple to produce — a single JSON.stringify of the whole array.
  • Human-inspectable as one coherent document.

Cons:

  • Reading one record means parsing the entire file — cost scales with file size per message, not per record, which is O(n²) total across a wave of n records.
  • The whole file must be held in memory to parse it, for both the writer (building the array before it can serialize) and every reader.
  • No way to append incrementally while compiling — the full array must be assembled before any of it can be written.

Option 2 — Line-delimited (NDJSON) records, read by byte offset (CHOSEN)

Each compiled entity type’s output is written as one JSON record per line (writeMigrationRecords); a Pub/Sub message for a large record carries byteOffset/byteLength locating exactly where its line sits in the file, and the reader seeks and parses only that slice (readMigrationRecordAt).

Pros:

  • Reading one record costs proportional to that record’s own size, not the whole file’s size — publish-time full-file streaming (readMigrationRecords) and dispatch-time single-record reads (readMigrationRecordAt) both avoid re-parsing anything beyond what’s needed.
  • Compiling can write records incrementally as they’re produced, never holding the full compiled output in memory.
  • A small record can still be sent inline in the message payload (usesStorageReference returns false for it) — the byte-offset path is reserved for records large enough that inlining them would bloat every Pub/Sub message.

Cons:

  • Not human-inspectable as a single structured document without tooling that understands the line-delimited format.
  • Requires every consumer (execute’s publish loop, dispatch’s per-record read) to agree on the same byte-offset contract — a message missing byteOffset/byteLength alongside a storage reference fails validation rather than degrading gracefully.
  • A batch compiled under the old JSON-array format is not readable by the new byte-offset readers — no compatibility shim exists, so such a batch must be re-verified before it can be executed (tracked as a gap, not a design flaw in the new format itself).

Decision 2 — Wave-gating granularity

Option 1 — Single fine-grained dependency graph, record by record

Every record carries its own dependency edges, and a scheduler walks the graph per record, publishing each one the instant its specific dependencies are individually satisfied, without regard to any larger stage grouping.

Pros:

  • Maximum parallelism — a record whose dependencies finish early never waits on unrelated records in the same stage.
  • No artificial stage boundaries to maintain or keep in sync with the entity-type catalog.

Cons:

  • A single evolving graph shared across a whole batch is much harder to reason about and test than four fixed, named stages.
  • Harder to build simple operator-facing progress reporting (“how far along is this batch?”) from a free-form graph than from four sequential buckets.
  • A full graph walk still needs some coarse backstop for message loss or stuck dependencies — it doesn’t eliminate the need for Decision 2’s Option 2 mechanism, it just replaces the coarse layer with a finer, more complex one.

Option 2 — Coarse wave barrier (execute-side) plus a per-record dependency recheck (dispatch-side), both active (CHOSEN)

ExecuteService gates each of its four fixed stages as a whole, never publishing stage N+1 until every entity type in stage N is done (waitForWave), with a bounded timeout. Independently, tenant-migration-orchestrator rechecks each record’s own dependsOn entity types before dispatch (checkDependenciesReady) and nacks/redelivers a not-yet-ready record.

Pros:

  • The stage barrier gives a simple, fixed four-stage model — “foundational → dependent → publish-gating → finalization” — that’s easy to reason about, report progress against, and bound with a single timeout.
  • The dispatch-side recheck is a cheap, independent backstop: if the stage barrier’s own bookkeeping were ever wrong or a message arrived out of the expected order, dispatch still refuses to process a record whose prerequisite hasn’t actually finished, rather than trusting the upstream gate blindly.
  • Both layers are already simple, existing mechanisms reused for this purpose, not new infrastructure built specifically for this decision.

Cons:

  • Coarser than Option 1 — a stage’s slowest entity type blocks every other entity type in the next stage, even ones with no real dependency on it.
  • Two independent gating mechanisms (execute’s wave barrier, dispatch’s dependency recheck) must both be kept correct and in sync with the entity-type catalog as new entity types are added — a drift between the two is a latent correctness risk, not something either mechanism alone would catch.

Final Decision

Line-delimited (NDJSON) compiled records read by byte offset (Decision 1, Option 2), combined with a coarse per-stage wave barrier on the execute side backed by a defensive per-record dependency recheck on the dispatch side (Decision 2, Option 2). Both choices favor the pipeline’s actual throughput and memory profile — streaming instead of whole-file buffering, and a simple four-stage model with a cheap independent backstop instead of either an unguarded coarse gate or a more complex fine-grained graph.