ADR-00045 — Event-Driven Batch Creation From Storage-Upload Completion

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

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

Context

A tenant catalog bundle (CSV entity-type folders, optionally including images) is uploaded by ui-admin-application’s migration feature straight to the GCS staging bucket, tagged with transaction name migration_bundle. Depending on bundle size, this single upload can take anywhere from seconds to minutes — there is no REST endpoint that starts a batch (SR-029 FR-01); the only thing that exists on the server side at upload time is the object landing in the bucket.

The question this pipeline had to settle: at what point does tenant-migration’s migration_batches document actually get created, relative to the upload itself? The current code creates it only after firebase-function’s storage_migration_upload_event.js fires on onObjectFinalized for the staging bucket — a GCS-native event that only fires once an object’s write is fully durable — which publishes a batch-uploaded envelope that tenant-migration’s MigrationBatchCreatedReceiver consumes to create the batch (SR-029 FR-01, FR-02; Architecture-00024 flow steps 1-3).

Decision Drivers (FRs & NFRs)

  • SR-029 FR-01 — a migration_bundle-tagged upload fires the storage trigger; there is no REST endpoint to start a batch upload.
  • SR-029 FR-02 — batch creation is idempotent against redelivery of the same upload envelope.
  • SR-029 NFR-06 — no silent duplicate side effects; batch creation is never double-applied.
  • SR-029 NFR-01 — nothing in this pipeline runs synchronously inside an HTTP request/response cycle.

Decision — When batch creation happens relative to upload

Option 1 — Client-driven synchronous batch creation

The admin UI calls a REST endpoint to create the batch record itself, either before the upload starts (pre-registering a batch id the upload then references) or after the upload finishes client-side (a “mark upload complete” call once the browser’s own upload request resolves).

Pros:

  • Immediate feedback — the client gets a definite success/failure response for batch creation at a known point in its own control flow, with no wait for an asynchronous event to propagate.
  • Easier to reason about from the client’s perspective — “I called an API and it returned 200” is a simpler mental model than “I uploaded a file and something downstream eventually noticed.”

Cons:

  • Pre-upload registration creates a batch record for an upload that might never actually finish — a browser tab closed mid-upload, a dropped connection, or any client-side failure after registration but before the upload completes leaves a migration_batches document with no corresponding object in the bucket. Nothing in this design otherwise needs a reconciliation job to find and clean up such orphaned registrations; introducing one just to cover this failure mode would be new infrastructure built solely to compensate for this option’s own weakness.
  • Post-upload confirmation shifts the correctness burden onto the client reliably detecting and reporting its own upload completion. The failure mode this can’t cover — a network drop in the exact window between the browser receiving the last byte of a successful upload and that same browser’s “mark complete” call reaching the server — silently leaves a fully-uploaded, fully-valid bundle sitting in the bucket with no batch ever created for it, and no signal anywhere that this happened. For a bundle large enough to take minutes to upload (the scenario this decision is specifically about), this window is a real, non-negligible target, not a theoretical edge case.
  • Requires building and maintaining a batch-creation endpoint and its own idempotency handling by hand, duplicating protection this design already gets for free from the storage-trigger path’s existing idempotent receiver (FR-02).

Option 2 — Event-driven batch creation from the storage-upload-finalize event (CHOSEN)

firebase-function’s storage_migration_upload_event.js fires on the same onObjectFinalized GCS trigger already used for the independent content-processor image pipeline, filtered to the migration_bundle transaction name, and publishes the batch-uploaded envelope that MigrationBatchCreatedReceiver consumes to create the batch.

Pros:

  • GCS’s own onObjectFinalized event is the ground truth for “this upload is actually complete and durable” — it cannot fire for a partial or abandoned upload, because it fires on the storage system’s own confirmation that the object write finished, not on any claim the client makes about its own state. This eliminates the entire class of failure Option 1 is exposed to: there is no way for a batch to be created for an upload that didn’t actually land, and no way for a fully-landed upload to silently fail to produce a batch because a client-side confirmation call never arrived.
  • Decouples upload duration from the batch-tracking service entirely — tenant-migration does no work and holds no request open while a large bundle is uploading; it only wakes up once there’s something durable to act on, however long the upload itself took.
  • Reuses the exact same onStorageUpload trigger/envelope mechanism already proven for the unrelated image-processing pipeline (same wrapper, same tracing/tenant-identifier derivation logic) — no new upload-completion-detection mechanism had to be designed or built specifically for migration.
  • Pub/Sub’s at-least-once delivery is absorbed for free by the receiver’s existing idempotent create (FR-02) — a redelivered “upload finished” event is a safe no-op, rather than something a hand-built REST endpoint would need its own idempotency-key scheme to handle.

Cons:

  • No synchronous acknowledgment to the uploader at the moment the upload itself finishes — there’s a real, if normally short, window between the object becoming durable in the bucket and the migration_batches document actually existing (GCS event delivery → Pub/Sub publish → receiver processing). The admin UI must poll or wait for the batch to appear rather than receiving a batch id directly in the upload’s own response.
  • A failure anywhere in the trigger/publish/receive chain (a transaction_name filter mismatch, the receiver being down) fails silently from the uploader’s point of view — “nothing happens” rather than an HTTP error surfaced at upload time, which can make a misconfigured upload harder to diagnose than a synchronous call’s immediate error response would be.
  • Debugging ties batch-creation reliability to the health of the Storage-trigger infrastructure (Cloud Functions, Pub/Sub delivery) rather than to tenant-migration’s own HTTP stack alone — an operator investigating “why doesn’t my batch exist” has to look at a different service’s logs than the one that will eventually own the batch.

Final Decision

Create the batch record only in response to GCS’s own onObjectFinalized event (Option 2), never from a client-driven call before or after the upload. The upload’s actual completion, as confirmed by the storage system itself, is the only trustworthy signal that a bundle is safe to act on — a large bundle’s upload duration is exactly the scenario where a client-reported “I’m done” signal is least reliable, since the opportunity for a dropped connection or abandoned tab grows with how long the upload takes. The cost — no synchronous batch-id handoff to the uploader, and a short propagation window before the batch becomes visible — is accepted in exchange for eliminating an entire class of orphaned-upload and silently-missing-batch failure modes that a synchronous design would have to solve for separately.