ADR-00002 — Subscribe-Only Audit Sink, Mask-Once-At-Persistence

By Engineering Team•September 24, 2026•5 min read
  • Status: Approved / Implemented
  • Date: 2026-09-24
  • Authors: Engineering Team

Context

payment-service and shipment-service both call third-party provider APIs (Razorpay, Shiprocket) as part of core checkout and fulfillment flows. Compliance and support both need a durable, centrally reviewable record of exactly what was sent to and received from those providers — without every integrated service independently building masking, storage, and retention logic, and without that instrumentation ever becoming a new latency or availability dependency for the checkout/fulfillment paths it observes.


Decision Drivers (FRs & NFRs)

See SR-002 for the full requirement list.

  • FR-01 / FR-07 (Subscribe-Only, Health-Only Surface): the sink must not become a synchronous dependency any publisher calls inline.
  • FR-03 / NFR-05 (Mask-Once, Masking Idempotency): masking must happen in exactly one place, not be duplicated per publisher.
  • NFR-01 (Non-Blocking Publish Latency): a publisher’s own response path must never be affected by audit capture.
  • NFR-06 (Extensible Coverage): adding a new sensitive field, or a new publisher, must not require touching every existing publisher.

Options Evaluated

Option 1 — Subscribe-only Pub/Sub sink, mask-once-at-persistence, UUID-keyed GCS storage (CHOSEN)

A dedicated Rust microservice (document-service) subscribes to a shared document-audit Pub/Sub topic. Publishers send the raw, unmasked request/response JSON; document-service masks it, uploads the masked JSON to GCS under a UUID-keyed object path, and tracks the transaction in MongoDB via a two-phase PENDING → SUCCESS/FAILED write.

  • Pros:
    • Fully decoupled and non-blocking: publishers never wait on document-service, and a document-service outage cannot fail a payment or shipment call (FR-08, NFR-01).
    • Masking centralized in one reviewable place — coverage can be extended by editing one PII_KEYS set, never touching a publisher (FR-03, NFR-06).
    • Storage collisions are structurally impossible: the GCS object key uses a fresh UUID, not transaction_id, so even a redelivered event can never overwrite an existing object (FR-04).
    • Per-transaction idempotency is enforced at the database layer via a unique compound index on (tenant_id, service_name, transaction_name, transaction_id), not by application-level deduplication logic (FR-06).
    • Extensible to any future publisher with zero code change inside document-service itself — a new publisher only needs to publish to the existing topic in the standard envelope shape (FR-02).
  • Cons & Accepted Trade-offs:
    • No retrieval/query API exists yet — today’s audit trail is write-only from the reviewer’s perspective, read directly out of MongoDB/GCS (out of scope per BRD-002).
    • A failed process_audit_event handler invocation is never retried by Pub/Sub redelivery, since the shared core-lib receiver acks unconditionally regardless of handler outcome — a transient failure is captured only as a FAILED tracking row, with no automatic re-drive. See Technical Debt — Document Service Audit Trail Gaps, TD-01.

Option 2 — Publisher-side masking (each publisher masks its own payload before publishing)

Each instrumented publisher runs its own masking pass over the request/response before sending the audit event.

  • Pros:
    • No new Pub/Sub topic, subscriber, or centralized document-service-equivalent dependency needed — masking and persistence stay entirely within each publisher’s own deploy and failure domain.
    • A publisher’s masking rules can be tailored precisely to its own domain’s sensitive fields, without needing a shared, generic PII_KEYS vocabulary to cover every publisher at once.
  • Cons (Why Rejected):
    • Duplicates masking logic across every language a publisher is written in (Rust today, Go today, potentially Node in the future) — each needs independent PII_KEYS-equivalent coverage, and a gap in one publisher’s copy silently leaks unmasked PII while another’s copy stays safe (violates FR-03/NFR-05’s “exactly once, exactly one place” principle).
    • Masking stops being a single, reviewable surface — auditing masking correctness means auditing N codebases instead of one.

Option 3 — Synchronous audit API call (gRPC/REST) from each publisher, inline before its own response

Each publisher calls document-service synchronously, waiting for the audit write to complete before returning its own RPC/webhook response.

  • Pros:
    • Strong consistency: a publisher would know for certain, at response time, whether its audit record was durably written — no fire-and-forget/eventual-consistency reasoning needed.
    • A failed audit write could be surfaced immediately as part of the same request, instead of being discoverable only later as a FAILED tracking row.
  • Cons (Why Rejected):
    • Makes every instrumented publisher’s response latency and availability dependent on document-service’s — exactly the coupling NFR-01 rules out.
    • Would force document-service to grow a gRPC/REST surface it was deliberately built without — it is gRPC/REST-free by design, subscribe-only, with no feature surface beyond a health endpoint (violates FR-01/FR-07).

Option 4 — Direct write from each publisher straight to a shared GCS bucket + MongoDB collection, no central service

Each publisher writes directly to the shared storage bucket and tracking collection itself, with no intermediary service.

  • Pros:
    • Removes the extra network hop and processing latency of a separate consuming service — a publisher’s audit write completes as soon as its own Mongo/GCS calls return.
    • No dependency on Pub/Sub delivery semantics or a separate service’s uptime for an audit record to land.
  • Cons (Why Rejected):
    • Spreads GCS/Mongo credentials across every publisher instead of isolating them in one narrowly-scoped service — the same credential-isolation principle already applied to media-access-service’s signing keys.
    • A masking bug fix would need to ship independently to every publisher instead of one place, reintroducing Option 2’s coverage-gap risk alongside the credential-sprawl problem.

Final Decision

Option 1 is selected. It is the only candidate that keeps publishers fully decoupled from the audit sink’s availability (NFR-01), centralizes masking into one reviewable surface (FR-03, NFR-05), and lets new publishers or new sensitive fields be added without touching existing code (NFR-06). payment-service and shipment-service are both already built against this design.