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

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
payment-servicecalls Razorpay’s Orders API or receives a Razorpay webhook;shipment-servicecalls a Shiprocket fulfillment endpoint, checks serviceability, or receives a Shiprocket tracking webhook (FR-02, FR-08, FR-09).- 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 thedocument-audittopic, never blocking or failing its own RPC/webhook response (FR-08, NFR-01). document-service’s Pub/Sub receiver picks up the message and callsDocumentAuditService.process_audit_event(FR-01).- It inserts a
PENDINGdocument_trackrow 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). - It masks every
PII_KEYS-matching field inrequestandresponse(FR-03). - It uploads the masked JSON to the Storage Bucket at
{service_name}/{transaction_name}/{uuid_v4}.json(FR-04). - It updates the
document_trackrow toSUCCESS, or toFAILEDwith anerror_message, once the upload attempt completes (FR-05, FR-06). - The Pub/Sub message is acked regardless of the outcome — a handler failure is captured only as a
FAILEDtracking row, not retried by redelivery (NFR-02 — see Technical Debt, TD-01).
Related Documents
- BRD-002 — Document Service: Centralized Audit Trail
- SR-002 — Document Service Audit Trail System Requirements
- ADR-00002 — Subscribe-Only Audit Sink, Mask-Once-At-Persistence
- Technical Debt — Document Service Audit Trail Gaps — tracked redelivery gap for this pipeline
- Microservices: document-service