ADR-00018 — Row-Based Encrypted Attribute Model, Value-Level Encryption, and the Address-Snapshot Exception

By Engineering Team•October 1, 2026•7 min read
  • Status: Approved / Implemented
  • Date: 2026-10-01
  • Authors: Engineering Team

Context

pii-service has to hold personal data for an open-ended, growing set of unrelated consumers — a warehouse’s contact name today, a shopper’s date of birth tomorrow, something no team has asked for yet next quarter — without pii-service itself needing to know what any of those fields mean. Two design questions follow directly from that: how is a “PII field” modeled at the schema level, and at what granularity is it encrypted. A third, narrower question surfaced once order-service needed address deduplication at checkout: the generic model this ADR chooses turns out to have no answer for “look this value up by its content before any caller-owned ID exists for it,” and that gap needed its own scoped exception.


Decision Drivers (FRs & NFRs)

See SR-010 for the full requirement list.

  • FR-02 / FR-05 (EAV Keying, Consumer-Owned Namespace): a new consumer or a new field must be addable without a pii-service code or schema change.
  • FR-03 (Encryption At Rest): every value MUST be encrypted before persistence, with a fresh nonce per value.
  • FR-06 / FR-07 / FR-08 (Dedicated Snapshot Model, Content-Hash Dedup, Per-User Scope): address capture needs a lookup key the generic model has no equivalent of.
  • NFR-02 (Shared Implementation, Isolated Schema): pii-service and pci-service share one encryption implementation over two structurally identical, data-isolated schemas — see ADR-00020.

Decision 1 — Row-Based Attribute Model (EAV) vs. Fixed Columns Per Entity Type

Option 1 — Generic entity-attribute-value rows, keyed (tenant_id, entity_type, entity_id, attribute_key) (CHOSEN)

One user_pii_attributes table holds every consumer’s attributes as narrow rows — entity_type and attribute_key are free text chosen by the caller, never declared in pii-service’s own schema.

  • Pros:
    • A new consumer, or an existing consumer adding a new field, needs zero changes inside pii-service — no migration, no new column, no redeploy (FR-02, FR-05).
    • The exact same table shape serves inventory-service’s "warehouse" contact fields, user-secure-engine’s "user_personal" profile fields, and user-preference-service’s "user_address" bodies — one schema, not one per consumer.
    • The identical shape is what lets pci-service reuse the same implementation over its own schema (NFR-02) — a fixed-column design would be specific to one consumer’s fields and couldn’t generalize to card data at all.
  • Cons & Accepted Trade-offs:
    • No database-level type checking or NOT NULL constraint on any individual attribute — pii-service cannot enforce that "user_personal"’s email is ever actually present; that’s left entirely to each consumer.
    • No compile-time or schema-level protection against two consumers picking the same entity_type for unrelated data — see Technical Debt — Centralized Personal Information Store Gaps, TD-03.
    • Querying “every attribute for this entity” is a row-scan over a narrow table rather than a single-row read — acceptable at today’s volume, not benchmarked at scale.

Option 2 — Fixed columns per entity type (e.g. a warehouse_pii, user_pii, address_pii table each with named columns)

  • Pros:
    • Database-level typing and constraints per field; a single-row read returns an entity’s full attribute set with no row-scan.
  • Cons (Why Rejected):
    • A new PII field requires a schema migration in pii-service itself, coupling this service’s release cadence to every consumer’s own feature work — directly defeats FR-02/FR-05’s reusability goal.
    • Cannot be shared with pci-service — card-token/last4/brand fields are structurally unrelated to any of today’s PII fields, so a fixed-column design would need its own independent schema and its own independent encryption/audit code, reopening exactly the duplication ADR-00020 exists to avoid.
    • Every future consumer forces a new table — this is the EAV pattern’s entire reason for existing, restated as a cost instead of a benefit.

Decision 2 — Value-Level Encryption vs. Column-Level / Database-Level Encryption

Option 1 — Encrypt each attribute value independently with AES-256-GCM, fresh nonce per value (CHOSEN)

RowAttributeService encrypts plaintext_value before it is ever handed to the repository layer — the stored row’s encrypted_value column holds ciphertext, and nothing upstream of the cipher call ever touches disk.

  • Pros:
    • Fits Decision 1 directly — in a row-based model there is no fixed column to apply column-level encryption to; every value lives in the same generic column regardless of what it represents, so the only granularity that makes sense is per-value.
    • A fresh nonce per value means two identical plaintext values (e.g. two warehouses with the same city) never produce identical ciphertext — no pattern leakage across rows.
    • Decryption happens only inside GetAttributes/GetAddressSnapshots — a row at rest, or a row returned by any other path, is always ciphertext (FR-03).
  • Cons & Accepted Trade-offs:
    • Every value pays its own AES-GCM overhead rather than amortizing a single encryption pass over a whole row — negligible at today’s attribute-count-per-entity, not re-evaluated at higher scale.
    • The encryption key (PII_ENCRYPTION_KEY) is a single service-wide secret — losing it decrypts every row this service has ever written; there is no per-tenant or per-entity key segmentation today.

Option 2 — Column-level encryption (pgcrypto) on fixed columns, or database-level encryption at rest (TDE) only

  • Pros:
    • Database-level TDE needs zero application code — the database handles it transparently.
    • pgcrypto-based column encryption would integrate natively with SQL query predicates on an encrypted column (not actually needed here, since pii-service never filters by attribute value).
  • Cons (Why Rejected):
    • Column-level encryption presumes Decision 1’s “Option 2” (fixed columns) — it has no natural target in a row-based schema where every value shares one generic column.
    • Database-level TDE alone protects only against stolen disk/backup media, not against a compromised database credential or a misconfigured query — it does not satisfy BR-03’s requirement that decrypted PII be reachable only through pii-service’s own audited code path, since anyone with raw SQL access to the database would read plaintext directly.
    • Neither option gives pii-service control over nonce freshness or decrypt-only-inside-one-call scoping — both are properties of application-level encryption that a database-managed scheme doesn’t expose.

Decision 3 — Address Snapshot Modeling: A Scoped Exception to Decision 1

order-service needs a shipping/billing address deduplicated so a returning customer’s repeat address doesn’t produce a new encrypted copy every order. The natural first instinct — store it exactly like any other PII attribute, under a new entity_type — runs into a real gap in Decision 1’s own model: the generic model assumes the caller already has a durable entity_id to key by, but an address snapshot’s whole purpose is to be shared across many orders before any one order can claim it as “its” entity.

Option 1 — Two dedicated tables (address_snapshots, address_snapshot_attributes), reusing the shared write path only for the child table (CHOSEN)

address_snapshots is a new parent table with a unique index on (tenant_id, created_user_id, content_hash) — the dedup lookup key. address_snapshot_attributes reuses RowAttributeRepositoryTrait/RowAttributeService exactly like user_pii_attributes does, with entity_type hardcoded to "address" and entity_id set to the snapshot’s generated UUID.

  • Pros:
    • Solves the real ordering problem directly: a snapshot can exist and be looked up by content before any order claims it, because its key is content_hash, not a caller-supplied entity_id (FR-06, FR-07).
    • Still reuses Decision 2’s shared encrypted write/audit path for the actual attribute data (FR-12) — only the parent dedup table is genuinely new.
    • Dedup scope (tenant_id, created_user_id) falls out naturally from the parent table’s own unique index, with no cross-service coordination needed (FR-08).
  • Cons & Accepted Trade-offs:
    • Breaks Decision 1’s own stated guidance (“adding PII to a new entity type requires zero changes here — do not add per-entity-type tables”) — a deliberate, explicitly-documented exception, not an oversight.
    • GetAddressSnapshots has to bypass the shared RowAttributeService::get_attributes read path entirely (which requires a non-empty, caller-known attribute-key list) and decrypt locally with a second, directly-instantiated AesGcmCipher instance — slightly more code to keep in sync with the shared cipher’s behavior.

Option 2 — Attach address attributes directly to the submitting order (entity_type="order", entity_id=order_id)

  • Pros: No new tables at all — pure reuse of Decision 1’s existing generic model, zero new code in pii-service.
  • Cons (Why Rejected): Gives every order its own address copy instead of real cross-order deduplication — a returning customer’s repeat shipping address would be re-encrypted and re-stored on every order, defeating BR-04/FR-07’s entire purpose.

Option 3 — Generic EAV model with a synthetic pre-dedup entity_id minted by the caller

order-service would compute its own content hash, mint a deterministic ID from it, and call the existing UpsertAttributes/GetAttributes RPCs directly with that ID as entity_id.

  • Pros: Avoids adding new tables or RPCs to pii-service’s proto surface.
  • Cons (Why Rejected): Pushes the dedup-key computation and collision-safety logic (race-safe insert-or-reuse) into every calling service instead of centralizing it once — the opposite of BR-05’s platform-reusability goal. pii-service would have no way to enforce the (tenant_id, created_user_id, content_hash) uniqueness constraint itself, since the generic model has no such index — a caller-computed ID collision would silently overwrite unrelated data instead of being caught at insert time.

Final Decision

Option 1 is selected for all three decisions. The row-based attribute model (Decision 1) keeps pii-service reusable across an open-ended set of consumers and entity types without schema coupling. Value-level encryption (Decision 2) is the only granularity that model’s own shape admits, and it keeps decrypted PII reachable only through pii-service’s audited code path rather than through database-level access alone. The dedicated address-snapshot tables (Decision 3) solve a real key-ordering problem the row-based model has no native answer for, while still reusing that same encrypted write path for the actual attribute data — the exception is scoped to the one piece that’s genuinely different (the dedup lookup key), not a wholesale departure from the row-based model.