Architecture: Document Service Audit Trail

By Engineering Team•September 24, 2026•6 min read

This is the architecture behind the decision recorded in ADR-00002, satisfying the requirements in BRD-002 and SR-002.

Diagram

Document service audit trail — publisher, Pub/Sub, and sink component/data flow diagram

Components

Component Language / Runtime Role
payment-service Rust Publishes an audit event after each create_payment_order Razorpay Orders API call, and after each Razorpay webhook (payment.captured/payment.failed/refund.processed) is dispatched — both fire-and-forget via tokio::spawn.
shipment-service Go Publishes an audit event for each of its 5 Shiprocket fulfillment-step calls (create_order, assign_awb, generate_label, generate_invoice, schedule_pickup), each serviceability cache-miss quote, and each inbound Shiprocket tracking webhook — on both success and failure, fire-and-forget via a goroutine over context.WithoutCancel(ctx).
Pub/Sub — document-audit GCP Pub/Sub Single topic/subscription pair shared by every publisher, routed by transaction_name rather than by topic — the same fan-in pattern the image pipeline uses for fan-out.
document-service Rust Subscribe-only sink: masks PII/PCI, uploads the masked JSON to the Storage Bucket, and tracks the transaction in MongoDB. No gRPC/REST surface beyond a health check.
Storage Bucket GCS (Firebase Storage) Holds the masked JSON, one object per audit event, keyed {service_name}/{transaction_name}/{uuid_v4}.json.
MongoDB — document_track MongoDB One row per (tenant_id, service_name, transaction_name, transaction_id) — the idempotency key — recording storage_path, status (PENDING/SUCCESS/FAILED), and error_message.

End-to-end flow

  1. payment-service calls Razorpay’s Orders API or receives a Razorpay webhook; shipment-service calls a Shiprocket fulfillment endpoint, checks serviceability, or receives a Shiprocket tracking webhook (FR-02, FR-08, FR-09).
  2. Each publisher builds an AuditEventPayload/AuditEventInput — {tenant_id, service_name, transaction_name, transaction_id, request, response}, always the raw unmasked JSON — and publishes it fire-and-forget to the document-audit topic, never blocking or failing its own RPC/webhook response (FR-08, NFR-01).
  3. document-service’s Pub/Sub receiver picks up the message and calls DocumentAuditService.process_audit_event (FR-01).
  4. It inserts a PENDING document_track row keyed by (tenant_id, service_name, transaction_name, transaction_id) — a duplicate/redelivered event is caught here as a no-op before any masking or upload work happens (FR-06).
  5. It masks every PII_KEYS-matching field in request and response (FR-03).
  6. It uploads the masked JSON to the Storage Bucket at {service_name}/{transaction_name}/{uuid_v4}.json (FR-04).
  7. It updates the document_track row to SUCCESS, or to FAILED with an error_message, once the upload attempt completes (FR-05, FR-06).
  8. The Pub/Sub message is acked regardless of the outcome — a handler failure is captured only as a FAILED tracking row, not retried by redelivery (NFR-02 — see Technical Debt, TD-01).