ADR-00021 — PostgreSQL vs. MongoDB for pii-service's Data Store
- Status: Approved / Implemented
- Date: 2026-10-01
- Authors: Engineering Team
Context
Most of this monorepo’s Go and Node services default to MongoDB — inventory-service, user-preference-service, order-service, shipment-service, shop-service, provider-auth-service, document-service all persist there. pii-service instead persists to PostgreSQL, the same choice pci-service, payment-service, and user-secure-engine make. This ADR records why pii-service specifically sits on the Postgres side of that split rather than following the platform’s more common default.
Decision Drivers (FRs & NFRs)
See SR-010 for the full requirement list.
- FR-07 / FR-09 (Content-Hash Deduplication, Race-Safe Snapshot Insert): two concurrent
CreateAddressSnapshotscalls for the same(tenant_id, created_user_id, content_hash)must never both succeed in creating a new row — exactly one must win, and the loser must safely reuse the winner’ssnapshot_id. - NFR-02 (Shared Implementation, Isolated Schema): the data-store choice must be compatible with ADR-00020’s shared implementation, which
pci-servicealready consumes.
Options Evaluated
Option 1 — PostgreSQL via SeaORM (CHOSEN)
db::connect_postgres (rust-lang-core-lib) builds a tuned connection pool; user_pii_attributes, address_snapshots, address_snapshot_attributes, and row_attribute_audit_log live in a dedicated "pii-service" schema.
- Pros:
- FR-09’s race-safety falls directly out of a native feature.
address_snapshots’s unique index on(tenant_id, created_user_id, content_hash)turns a concurrent duplicate insert into aSqlErr::UniqueConstraintViolationthatpii-servicedetects and recovers from by re-fetching the winning row — Postgres’s row-level uniqueness constraint is the dedup mechanism, not a layer built on top of one. - Multi-row writes commit atomically. Creating a new address snapshot writes both the
address_snapshotsparent row and one or moreaddress_snapshot_attributeschild rows — Postgres’s ACID transaction guarantees this either fully commits or fully rolls back, with no window where a snapshot exists without its attributes or vice versa. - Already the dependency ADR-00020 requires.
rust-lang-core-lib’s sharedrepository::encryptmodule is itself built on SeaORM targeting Postgres, andpci-servicealready consumes that same path over its own Postgres schema — choosing Postgres forpii-servicekeeps both services on the one implementation ADR-00020 commits to, instead of requiring a second, Mongo-targeting repository implementation to exist in parallel. - Schema-level isolation matches an already-proven pattern.
"pii-service"and"pci-service"sit in the same Postgres instance as two separate schemas, the same physical-isolation-without-a-separate-instance approach already used between those two services.
- FR-09’s race-safety falls directly out of a native feature.
- Cons & Accepted Trade-offs:
- The EAV model (ADR-00018) stores an entity’s attributes as several narrow rows rather than one natural document — a shape Mongo would represent more directly as a single document per entity.
- Requires Postgres-specific pool tuning (
DB_MAX_CONNECTIONS, fixedmin_connections/idle_timeout— see the platform’s performance & metrics standards) rather than a document-client connection.
Option 2 — MongoDB, matching the platform’s more common default
pii-service would persist to Mongo, the same way inventory-service, user-preference-service, and most of this monorepo’s other services do.
- Pros:
- Matches the platform’s more common default, lowering the number of distinct data-store technologies an operator has to run.
- A document per
(entity_type, entity_id)would represent an entity’s full attribute set more naturally than EAV’s narrow-row shape.
- Cons (Why Rejected):
- No equivalent atomic guarantee for FR-09’s dedup path without extra work. Mongo’s unique index can reject a duplicate insert, but recovering safely from the resulting race (detecting which concurrent writer actually won and re-fetching its
snapshot_id) needs an application-level retry/compare loop rather than the singleSqlErr::UniqueConstraintViolationmatch Postgres makes available — more code, and a narrower guarantee than a relational unique constraint plus transaction gives for free. - Breaks NFR-02’s shared-implementation requirement.
rust-lang-core-lib’srepository::encryptmodule is SeaORM/Postgres-specific; choosing Mongo forpii-servicewould force either a second, Mongo-targeting implementation of the same encrypt/audit logic (reopening exactly the duplication ADR-00020 exists to avoid) or divergingpii-servicefrompci-service’s store entirely, with no requirement calling for that divergence. - Multi-document transactional guarantees exist in Mongo but are a heavier, more operationally complex feature than Postgres’s native single-instance ACID transactions — not justified for the comparatively simple two-table write this service needs.
- No equivalent atomic guarantee for FR-09’s dedup path without extra work. Mongo’s unique index can reject a duplicate insert, but recovering safely from the resulting race (detecting which concurrent writer actually won and re-fetching its
Final Decision
Option 1 is selected. PostgreSQL gives the address-snapshot dedup path (FR-07, FR-09) an atomic, native unique-constraint-plus-transaction guarantee that would otherwise need to be hand-built, and keeps pii-service on the same SeaORM/Postgres foundation ADR-00020’s shared implementation already requires pci-service to use. This is a deliberate, scoped departure from the platform’s more common Mongo default — justified by this service’s specific dedup and shared-implementation constraints, not a general preference for Postgres over Mongo elsewhere in the monorepo.
Related Documents
- BRD-009 — Centralized Personal Information Store
- SR-010 — Centralized Personal Information Store System Requirements
- ADR-00018 — Row-Based Encrypted Attribute Model, Value-Level Encryption, and the Address-Snapshot Exception
- ADR-00019 — Dedicated pii-service vs. Duplicated Per-Service PII Handling
- ADR-00020 — Shared Core-Lib Encryption/Audit/CRUD Pattern vs. In-Service Implementation
- Architecture: Centralized Personal Information Store
- Microservices: pii-service