Architecture Decision Record
ADR-006: Database per Service
Status
Accepted (2026-06-07). Supersedes the earlier "deliberately one shared database" stance.
Clarified 2026-06-27: the single context class became one sealed context class per engine when
ADR-018 added the orthogonal engine axis (the Name/database axis here is unchanged).
Updated 2026-09-03: the legacy AtlDevCon archive database no longer exists (dropped 2026-09-02);
rollback now restores from a bacpac blob.
Revised 2026-09-09: PostgreSQL is a fourth engine
(ADR-113), so the per-engine context set is
SQL Server / Cosmos / SQLite / PostgreSQL; the one-instance-per-database rule below is unchanged.
Revised 2026-10-06: the AtlDevCon bacpac was deleted on 2026-10-03, so no pre-cutover rollback
copy exists; the four live ADC_* databases rely on their own PITR plus LTR.
Revised 2026-10-07: anchors refreshed after the v1.233.0 release.
Revised 2026-10-21: the Decision bullet now names PostgreSQLDbContext among the per-engine
contexts, attributes once-per-key caching to the scoped DbContextFactory (not
PhysicalDbContextFactory), and scopes the no-per-module-context rule to concrete runtime contexts
(abstract per-module ModuleApplicationDbContext DbSet holders exist);
infra/main.bicep anchors refreshed.
Revised 2026-10-26: per-module ModuleApplicationDbContext declarations are scoped to modules that
own their own aggregates (ADC Notification declares none); infra/main.bicep anchors refreshed.
Revised 2026-10-28: anchors refreshed after Helpdesk #199 moved the no-per-module-context rule to
MMCA.Helpdesk/AGENTS.md:62.
Revised 2026-11-17: the abstract per-module ModuleApplicationDbContext DbSet holders were deleted
from ADC, Store and Helpdesk (nothing derived from or referenced them, and their DbSet lists had
drifted from the model); no module declares a DbContext class of any kind.
Revised 2026-11-23: the ADC Decision bullet now says the two attendee hot-path databases run at S2
Standard instead of Basic while conferenceMode is on; anchors refreshed.
Context
When the modules were first extracted into independently-deployable services, all services in an
app still pointed at a single shared SQL database with a single OutboxMessages table. That
left one significant defect: every service's OutboxProcessor polled the same outbox table with no
origin filter, so the services raced to claim each other's rows (ADR-003 / ArchitecturalAnalysis
§4.4 #5). It also undermined the data-autonomy half of "microservices": services that share a
database are not independently evolvable at the schema level.
MMCA.Common already provided the machinery for multiple physical data sources (DataSourceResolver,
EntityDataSourceRegistry, DbContextFactory, CrossDataSourceDegradeConvention), so the move was
a configuration/deployment change, not a framework rewrite.
Decision
Adopt database-per-service: each service owns its own physical database with its own
OutboxMessages table.
- One sealed concrete context class per engine, one instance per database. We do not
introduce a per-module DbContext class, concrete or abstract: the model is built from the
module's entity configurations alone (rule stated at
MMCA.Helpdesk/AGENTS.md:64). ADC Notification still owns theADC_Notificationdatabase, but itsPushNotificationandUserNotificationentities live in MMCA.Common (MMCA.ADC/Source/Services/MMCA.ADC.Notification.Service/Program.cs:141-142), so its module projects are API, Application and Shared only, with no Infrastructure project. The default engine is SQL Server (SQLServerDbContext); ADR-018 later addedSqliteDbContextandCosmosDbContext, and ADR-113 addedPostgreSQLDbContext, each a sealed subclass of the abstractApplicationDbContext. The singletonPhysicalDbContextFactorycreates a new raw context on everyCreate(DataSourceKey)call; the scopedDbContextFactorycaches one instance perDataSourceKey(Engine, Name)for the scope; entities route to a physical source by logical name ([UseDatabase]/ module namespace) viaDataSourceResolver, and to an engine by configuration base class ([UseDataSource], ADR-018). The forbidden split is per-module, not per-engine. - ADC runs
ADC_Identity,ADC_Conference,ADC_Engagement,ADC_Notification: locally on the shared Aspire SQL container and in Azure as four Basic-tier databases, of whichADC_ConferenceandADC_Engagementmove to S2 Standard whileconferenceModeis on (MMCA.ADC/infra/main.bicep:215-217,:1308-1310). Those four are the entire application data estate: the legacyAtlDevCondatabase was exported to the bacpac blobsql-archive/AtlDevCon-20260902.bacpacand dropped on 2026-09-02, andinfra/main.bicepno longer declares it. That bacpac was itself deleted permanently on 2026-10-03 (blob soft delete and versioning were off), so there is no restore path to the pre-cutoverAtlDevCondata; its contents had already been copied into the per-service databases at cutover, and those four databases keep their own PITR plus LTR (MMCA.ADC/infra/main.bicep:1271,MMCA.ADC/infra/main.bicep:1323,MMCA.ADC/infra/POST-CUTOVER-atldevcon-downgrade.md:103,MMCA.ADC/infra/DISASTER-RECOVERY.md:27). - Per-source outbox. Each database has its own
OutboxMessages; theOutboxProcessordrains only the sources its host owns, so no service ever sees another's rows. - Cross-service references are scalar IDs, not FKs.
CrossDataSourceDegradeConventionremoves FK constraints/navigations that would span databases; runtime joins flow throughINavigationPopulator(ADR-002), and cross-service consistency flows through the outbox + broker. - A host with no
DataSourcesconfiguration still collapses onto oneDefaultsource, so the single-database monolith deployment continues to work unchanged.
Rationale
- Removes the shared-outbox race (the sharpest cost of the shared DB) without an
OriginServicefilter: physical isolation is simpler and stronger than a logical filter. - Real data autonomy: each service can evolve and scale its schema independently; lifts the former single-database scaling ceiling.
- Reuses existing framework extension points: no new DbContext classes, no business-logic rewrite.
Trade-offs
- No cross-database FKs or transactions. Relationships that span services degrade to scalar IDs; consistency across services is eventual (outbox + broker), not transactional.
- More databases to provision, migrate, and back up. Each service has its own migrations project and its own backup/restore concern.
- Referential integrity across services is the application's responsibility (compensating indexes survive, FK enforcement does not).
Revision (2026-10-06)
- The ADC Decision bullet no longer names the
AtlDevConbacpac as the rollback source of record: the blob was deleted permanently on 2026-10-03, so no pre-cutover restore path exists, and the liveADC_*databases rely on PITR plus LTR (bacpac deletion atMMCA.ADC/infra/main.bicep:921, LTR policy atMMCA.ADC/infra/main.bicep:966,MMCA.ADC/infra/POST-CUTOVER-atldevcon-downgrade.md:103). The 2026-09-03 Status note is kept as recorded on that date. - Anchors re-verified against current source.
Revision (2026-10-07)
Re-verified against current source. The decision is unchanged: four per-service ADC_* databases,
no AtlDevCon database or bacpac, and PITR plus LTR as the only restore path. Only the
infra/main.bicep anchors moved; the 2026-10-06 revision above keeps the anchors it recorded.
- Anchors re-verified against current source: the
AtlDevConarchived-and-dropped note with the 2026-10-03 bacpac deletion atMMCA.ADC/infra/main.bicep:937(was:921), theserviceDatabaseLtrpolicy (weeklyP4W, monthlyP12M, yearlyP1Y) atMMCA.ADC/infra/main.bicep:982(was:966);MMCA.ADC/infra/POST-CUTOVER-atldevcon-downgrade.md:103andMMCA.ADC/infra/DISASTER-RECOVERY.md:27are unchanged.
Revision (2026-10-21)
Re-verified against current source. The decision is unchanged: one sealed context class per engine,
one instance per database, four per-service ADC_* databases with PITR plus LTR as the only restore
path. The Decision bullet now names all four engine contexts and assigns the per-key cache to the
right factory; the infra/main.bicep anchors moved again. The 2026-10-06 and 2026-10-07 revisions
above keep the anchors they recorded.
- The per-engine context set in the Decision bullet now includes
PostgreSQLDbContext(MMCA.Common/Source/Core/MMCA.Common.Infrastructure/Persistence/DbContexts/PostgreSQLDbContext.cs:23, ADR-113) alongsideSQLServerDbContext.cs:15,SqliteDbContext.cs:12andCosmosDbContext.cs:15, all sealed over the abstractApplicationDbContext.cs:46. - Once-per-key materialization belongs to the scoped
DbContextFactory: itsDictionary<DataSourceKey, ApplicationDbContext>atMMCA.Common/Source/Core/MMCA.Common.Infrastructure/Persistence/DbContexts/Factory/DbContextFactory.cs:74is checked atDbContextFactory.cs:103before calling the singletonPhysicalDbContextFactory(PhysicalDbContextFactory.cs:19), whoseCreateatPhysicalDbContextFactory.cs:25-33builds a new context on every call. - The no-per-module-context rule now says no per-module concrete context class: every consumer
module declares an abstract
ModuleApplicationDbContextthat only lists itsDbSets (for exampleMMCA.ADC/Source/Modules/Conference/MMCA.ADC.Conference.Infrastructure/Persistence/DbContexts/ModuleApplicationDbContext.cs:22,MMCA.Helpdesk/Source/Modules/Tickets/MMCA.Helpdesk.Tickets.Infrastructure/Persistence/DbContexts/ModuleApplicationDbContext.cs:14), and none is subclassed at runtime. The rule points atMMCA.Helpdesk/AGENTS.md:74, which states it in that form, instead of a convention name that has no source of its own. - Anchors re-verified against current source: the
AtlDevConarchived-and-dropped note with the 2026-10-03 bacpac deletion atMMCA.ADC/infra/main.bicep:1080(deletion sentence at:1085; was:937), theserviceDatabaseLtrpolicy atMMCA.ADC/infra/main.bicep:1132(weeklyP4W, monthlyP12M, yearlyP1Yat:1137-1139; was:982);MMCA.ADC/infra/POST-CUTOVER-atldevcon-downgrade.md:103andMMCA.ADC/infra/DISASTER-RECOVERY.md:27are unchanged.
Revision (2026-10-26)
Re-verified against current source. The decision is unchanged: one sealed context class per engine,
one instance per database, four per-service ADC_* databases with PITR plus LTR as the only restore
path. The Decision bullet now scopes per-module ModuleApplicationDbContext declarations to modules
that own their own aggregates, and its infra/main.bicep anchors moved. The 2026-10-21 revision above keeps
the wording and anchors it recorded (its "every consumer module" statement and its
PostgreSQLDbContext.cs:23 anchor are superseded by this revision).
- Not every consumer module declares a
ModuleApplicationDbContext. ADC Identity (MMCA.ADC/Source/Modules/Identity/MMCA.ADC.Identity.Infrastructure/Persistence/DbContexts/ModuleApplicationDbContext.cs:15), Conference (MMCA.ADC/Source/Modules/Conference/MMCA.ADC.Conference.Infrastructure/Persistence/DbContexts/ModuleApplicationDbContext.cs:22), Engagement (MMCA.ADC/Source/Modules/Engagement/MMCA.ADC.Engagement.Infrastructure/Persistence/DbContexts/ModuleApplicationDbContext.cs:20) and Helpdesk Tickets (MMCA.Helpdesk/Source/Modules/Tickets/MMCA.Helpdesk.Tickets.Infrastructure/Persistence/DbContexts/ModuleApplicationDbContext.cs:14) do; ADC Notification declares none. Its module projects are onlyMMCA.ADC.Notification.API,MMCA.ADC.Notification.ApplicationandMMCA.ADC.Notification.Shared(the host-sideMMCA.ADC.Notification.ContractsandMMCA.ADC.Notification.Serviceprojects live underMMCA.ADC/Source/Services), and none of them is an Infrastructure project. Notification still owns persistence: theADC_Notificationdatabase mapsPushNotificationandUserNotificationto aNotificationschema (MMCA.ADC/Source/Hosting/MMCA.ADC.Migrations.SqlServer.Notification/Migrations/SQLServerDbContextModelSnapshot.cs:95,:156), but those entities live in MMCA.Common and the module owns no aggregates of its own (MMCA.ADC/Source/Services/MMCA.ADC.Notification.Service/Program.cs:269-270). The Decision bullet keeps "Each module may declare" and names Notification as the module that does not. - Anchors re-verified against current source:
PostgreSQLDbContextis declared atMMCA.Common/Source/Core/MMCA.Common.Infrastructure/Persistence/DbContexts/PostgreSQLDbContext.cs:24(was:23); theAtlDevConarchived-and-dropped note atMMCA.ADC/infra/main.bicep:1114(was:1080), with the 2026-10-03 bacpac deletion sentence at:1119(was:1085) and a second mention in the LTR comment at:1164-1165; theserviceDatabaseLtrpolicy atMMCA.ADC/infra/main.bicep:1166(was:1132), weeklyP4W, monthlyP12M, yearlyP1Yat:1171-1173(values unchanged);MMCA.Helpdesk/AGENTS.md:74,MMCA.ADC/infra/POST-CUTOVER-atldevcon-downgrade.md:103andMMCA.ADC/infra/DISASTER-RECOVERY.md:27are unchanged.
Revision (2026-10-28)
Re-verified against current source. The decision is unchanged: one sealed context class per engine,
one instance per database, abstract per-module ModuleApplicationDbContext DbSet holders only in
modules that own their own aggregates, and four per-service ADC_* databases with PITR plus LTR as
the only restore path. Only the Helpdesk AGENTS.md anchor moved: Helpdesk #199 moved
template-engine traps out of that file, which shifted the rule up. The 2026-10-21 and 2026-10-26
revisions above keep the anchors they recorded (their MMCA.Helpdesk/AGENTS.md:74 was correct when
written and is superseded by this revision).
- Anchors re-verified against current source: the no-per-module-context rule ("one instance per
database, never a per-module context class (ADR-006)") at
MMCA.Helpdesk/AGENTS.md:62(was:74, which now holds the unrelated tenancy override-key bullet);MMCA.Helpdesk/Source/Modules/Tickets/MMCA.Helpdesk.Tickets.Infrastructure/Persistence/DbContexts/ModuleApplicationDbContext.cs:14,MMCA.ADC/Source/Services/MMCA.ADC.Notification.Service/Program.cs:269-270, theAtlDevConnote atMMCA.ADC/infra/main.bicep:1114and theserviceDatabaseLtrpolicy atMMCA.ADC/infra/main.bicep:1166are unchanged.
Revision (2026-11-17)
The decision is unchanged: one sealed context class per engine, one instance per database, four
per-service ADC_* databases with PITR plus LTR as the only restore path. What changed is the
per-module holder. The abstract ModuleApplicationDbContext classes in ADC (Conference,
Engagement, Identity), Store (Catalog, Identity, Sales) and Helpdesk (Tickets) were deleted: no code
derived from or referenced any of them, the runtime model never read their DbSet lists, and those
lists had already drifted (ADC Conference lacked SessionAiScore and SpeakerQuestionAnswer,
Engagement lacked TicketImportRun, Store Catalog lacked ProductVariant). A module now declares no
DbContext class at all, and the rule is restated at MMCA.Helpdesk/AGENTS.md:64. The earlier
revisions keep the wording and anchors they recorded; their ModuleApplicationDbContext anchors are
superseded by this revision. Source: the 2026-11-17 Infrastructure-projects drift review (item A5).
Revision (2026-11-23)
Re-verified against current source. The decision is unchanged: one sealed context class per engine,
one instance per database, no module DbContext class, and four per-service ADC_* databases with
PITR plus LTR as the only restore path. The ADC Decision bullet now qualifies the Basic tier for
conference mode, and its anchors moved. The earlier revisions keep the anchors they recorded (their
ApplicationDbContext.cs:46, SQLServerDbContextModelSnapshot.cs:95 / :156, Program.cs:269-270,
main.bicep:1114 and main.bicep:1166 anchors are superseded by this revision).
- Two of the four ADC databases leave Basic in conference mode:
conferenceScaledDatabaseNames(MMCA.ADC/infra/main.bicep:215-217) listsADC_ConferenceandADC_Engagement, and the per-service database resource gives those twoS2/StandardwhileconferenceModeis on andBasic(capacity 5) otherwise (MMCA.ADC/infra/main.bicep:1308-1310). The four names are declared atMMCA.ADC/infra/main.bicep:1292-1295and locally atMMCA.ADC/Source/Hosting/MMCA.ADC.AppHost/Program.cs:37-40. - Anchors re-verified against current source: the abstract
ApplicationDbContextatMMCA.Common/Source/Core/MMCA.Common.Infrastructure/Persistence/DbContexts/ApplicationDbContext.cs:53(was:46); the comment placingPushNotificationandUserNotificationin MMCA.Common atMMCA.ADC/Source/Services/MMCA.ADC.Notification.Service/Program.cs:141-142(was:269-270); theNotificationschema mappings atMMCA.ADC/Source/Hosting/MMCA.ADC.Migrations.SqlServer.Notification/Migrations/SQLServerDbContextModelSnapshot.cs:111and:172(was:95,:156), over the MMCA.Common entity types declared at:25and:114; the Notification module is still the three API, Application and Shared projects underMMCA.ADC/Source/Modules/Notification; theAtlDevConarchived-and-dropped note atMMCA.ADC/infra/main.bicep:1271(was:1114), with the 2026-10-03 bacpac deletion sentence at:1276-1277and a second mention in the LTR comment at:1321-1322; theserviceDatabaseLtrpolicy atMMCA.ADC/infra/main.bicep:1323(was:1166), weeklyP4W, monthlyP12M, yearlyP1Yat:1328-1330(values unchanged);MMCA.Helpdesk/AGENTS.md:64,MMCA.ADC/infra/POST-CUTOVER-atldevcon-downgrade.md:103andMMCA.ADC/infra/DISASTER-RECOVERY.md:27are unchanged.