Appearance
ADR-2238: Universal PostgreSQL Persistence Standard and Hybrid JSONB Record Projection
- Status: Accepted
- Date: 2026-09-03
- Authors: @fderuiter
- Deciders: @fderuiter
1. Context & Problem Statement
In the legacy Python platform topology, database persistence was fragmented across two tiers:
- Core services (
execution,etmf,ctms,quality,org) persisted state in PostgreSQL instances. - Seven operational microservices (
safety,interop,tickets,notifications,econsent,eisf,fileshare) persisted state in isolated SQLite database files (*.db).
Furthermore, clinical observation models (ClinicalObservation.additional_properties) and eCRF submissions (FormSubmission.form_data) relied on untyped Python dictionaries (dict[str, Any]), bypassing compile-time safety and creating runtime schema fragility.
Under 21 CFR Part 11 and ICH GCP E6(R2) (PRD-SYS-053), every entity mutation requires immutable cryptographic audit logging (GxPAuditInterceptor) and atomic event publication (MassTransit Transactional Outbox). SQLite does not support PostgreSQL session context variables (set_config('cadence.current_user_id', ...)), database-level trigger functions, or concurrent outbox locking under high concurrency.
2. Decision Drivers & Constraints
- 21 CFR Part 11 Traceability (
PRD-SYS-053): Universal enforcement of non-repudiation, SHA-256 Merkle block sealing, and tamper-evident audit ledgers across all services. - Compile-Time Type Safety & Native AOT: Elimination of runtime attribute errors and reflection-based dynamic dictionary access in C# 14 / .NET 10.
- Dual-Write Elimination: Guaranteed atomic persistence of domain entity state changes and outbound MassTransit domain events within a single PostgreSQL database transaction.
- Schema Evolution & Flexibility: Retaining the ability to support variable laboratory parameters, site-specific analyte units, and custom protocol properties without requiring physical database column migrations for every protocol amendment.
3. Options Considered
Option 1: Universal PostgreSQL Persistence + Hybrid Strongly-Typed JSONB Record Projection (Selected)
- Consolidate all 18 microservices on PostgreSQL 14+ with logical database or schema-level multi-tenant isolation.
- Standardize on
Npgsql.EntityFrameworkCore.PostgreSQLand the sharedGxPAuditInterceptorfromCadence.Database. - Model dynamic clinical attributes as strongly-typed C# 14 record hierarchies mapped to PostgreSQL
jsonbviaSystem.Text.Jsonsource generator converters, paired with an unmapped fallback dictionary (Dictionary<string, JsonElement>) for forward compatibility. - Adopt
MassTransitwith PostgreSQL Transactional Outbox for all cross-service asynchronous domain event dispatches. - Pros:
- ✅ Addresses requirements
- Cons:
- ❌ Trade-offs accepted
Option 2: Retain SQLite for Edge Services + Dynamic JsonDocument
- Keep SQLite for the 7 edge services via
Microsoft.EntityFrameworkCore.Sqlite. - Use unconstrained
JsonDocumentproperties for observation and form data. - Cons: Fractures persistence abstractions, prevents uniform database triggers and Merkle outbox integration, and forces dual-driver test configurations.
- Pros:
- ✅ Addresses requirements
4. Decision Outcome
Chosen Option: Option 1 (Universal PostgreSQL Persistence + Hybrid Strongly-Typed JSONB Record Projection).
Architectural Standards Adopted:
- Universal Persistence: All services deploy
Npgsql.EntityFrameworkCore.PostgreSQLtargeting PostgreSQL 14+. The legacy SQLite database files are retired and migrated. - Hybrid JSONB Mapping:
ClinicalObservation.AdditionalPropertiesis backed by PostgreSQLjsonb, strongly typed to polymorphic records (LaboratoryObservationProperties,VitalSignProperties) with Native AOT source generation and fallback to unmapped property dictionaries. - Transactional Outbox Event Backbone: All mutating domain commands write outbound events to
IntegrationOutboxin the same EF Core transaction, dispatched asynchronously viaMassTransit.
5. Consequences & Trade-offs
- Positive Impact:
- Standardizes the entire persistence layer on a single, production-grade RDBMS engine.
- Enables uniform 21 CFR Part 11
GxPAuditInterceptorMerkle block sealing across all 18 microservices. - Completely eliminates dual-write failures between database commits and message dispatches.
- Combines compile-time safety for known clinical properties with zero-downtime extensibility for unanticipated protocol fields.
- Negative Impact / Liabilities:
- Local development environment requires Docker Compose PostgreSQL container rather than zero-dependency SQLite files.
- Requires EF Core PostgreSQL migrations for previously schema-less SQLite tables.
6. Implementation & Verification
- Documentation & Vocabulary: Updated
CONTEXT.mdwithUniversal PostgreSQL Persistence StandardandHybrid Strongly-Typed JSONB Record Projection. - Database Layer: Standardized on
packages/database/src/Cadence.Database.csprojwith PostgreSQL provider. - Test Verification: Verified via
tests/Cadence.ArchitectureTests/Cadence.ArchitectureTests.csprojandtests/Cadence.UnitTests/Cadence.UnitTests.csprojunder@req:PRD-SYS-053.