Next-generation persistence stack
Elsa.Persistence.VNext is an experimental, provider-neutral persistence stack for data that can use a shared document and schema model. It is designed to let Elsa modules describe their data once, run it on relational or document databases, and adopt the new stack beside the existing persistence providers rather than replace them at once. See the persistence overview for the current persistence model and EF Core persistence providers for the existing relational path.
System role
Persistence boundary
The VNext stack separates Elsa data contracts from database choices. A module describes storage units, fields, keys, and indexes. A provider then turns that description into database objects and uses the same document operations for reads and writes.
The boundary has two related shapes:
| Shape | Main use | Database form |
|---|---|---|
| Document storage | Store module data as versioned documents with declared index values | Collections or shared relational document tables |
| Relational schema | Describe structured tables and columns | Provider-specific tables and indexes |
This split lets a module keep one persistence contract while a deployment chooses SQLite, SQL Server, PostgreSQL, or MongoDB. The stack remains experimental, so existing specialized providers still own data that needs behavior VNext does not yet cover.
References: src/…/Elsa.Persistence.VNext.csproj• L3-8 src/…/IDocumentStore.cs• L3-14 src/…/PersistenceSchema.cs• L3-7
Persistence flow
The persistence flow combines module schema descriptions with a provider plan. Materialization prepares the database before document operations use the declared storage units.
This diagram shows how a module schema becomes provider storage and then supports application data.
flowchart LR
A[Elsa module] --> B[Schema provider]
B --> C[Schema catalog]
C --> D[Provider planner]
D --> E[Database objects]
E --> F[Document store]
F --> G[Versioned module data]
The same flow supports relational and MongoDB providers. The provider changes the physical plan, but the module still works through the document store contract.
References: src/…/DefaultPersistenceSchemaCatalog.cs• L7-25 src/…/IPersistenceSchemaPlanner.cs• L3-6 src/…/IDocumentStore.cs• L3-14
Persistence abstractions
Schema descriptions
The schema description is the portable statement of what a module needs to store. It has a name and version, then groups data into relational tables or document storage units.
Each storage unit can define:
- Named fields with a type and null policy.
- A key for document identity.
- Indexes for supported filters.
- An optional namespace for provider naming.
The schema provider only describes the model. A planner maps it to a target database, and a renderer or migrator applies that plan. This keeps schema intent separate from SQL text or MongoDB commands.
References: src/…/IPersistenceSchemaProvider.cs• L3-6 src/…/PersistenceStorageUnit.cs• L3-8 src/…/PersistenceTable.cs• L3-8 src/…/IPersistenceSchemaRenderer.cs• L3-6 src/…/IPersistenceSchemaMigrator.cs• L3-6
Document operations
The document store is the common runtime interface for VNext data. It supports materialization, save, load, delete, and indexed query operations.
Each stored document carries its storage unit, ID, content, version, and creation and update times. A save request also carries the values needed by the declared indexes. An optional expected version provides optimistic concurrency: the write succeeds only when the caller still has the expected version.
Queries use a storage unit and a set of indexed field filters. The store rejects a query when no declared index can serve it. This makes query support part of the module's schema contract instead of an accidental provider feature.
References: src/…/IDocumentStore.cs• L3-14 src/…/StoredDocument.cs• L3-9 src/…/SaveDocumentRequest.cs• L3-8 src/…/DocumentQuery.cs• L3-5 src/…/DocumentQueryNotIndexedException.cs• L1-6 src/…/DocumentStoreConcurrencyException.cs• L1-8
Physicalization policies
Physicalization is the choice to give one logical storage unit a dedicated database object. It is useful when a shared document store is not enough for a high-use data shape or when a deployment needs a provider-specific index layout.
The policy names the storage unit, the target type, an optional physical name, and the indexes to create. A provider returns a plan of operations rather than changing the portable schema itself.
| Target | Intended provider form |
|---|---|
| Dedicated relational table | A separate SQL table with selected columns and indexes |
| Dedicated document collection | A separate MongoDB collection with selected indexes |
Physicalization is an adoption aid. It allows a workload to use VNext contracts while preserving a database shape that fits its access pattern.
References: src/…/StoragePhysicalizationPolicy.cs• L3-10 src/…/IPhysicalizationPlanner.cs• L5-8 src/…/PhysicalizationPlan.cs• L3-11 src/…/PhysicalizationTarget.cs• L1-7
Runtime layer
Runtime entity model
The runtime layer provides a VNext data model for runtime-defined entities. A runtime entity definition names a flexible set of fields and indexes. Its instances hold data that follows a published definition version.
Definitions move through a small life cycle:
- Draft: the shape can be edited and checked.
- Published: instances can be saved against the shape.
- Retired: the shape is no longer active.
The manager stores definitions, instances, and audit records through the document store. It validates fields and index references before saving, and it records changes so callers can inspect the history of a definition or instance.
References: src/…/RuntimeEntityDefinition.cs• L3-13 src/…/RuntimeEntityInstance.cs• L3-11 src/…/RuntimeEntityDefinitionStatus.cs• L1-7 src/…/RuntimeEntityManager.cs• L17-109
Runtime entity queries
Runtime entity queries use indexes declared by the published definition. The runtime layer maps each indexed field to a bounded index slot, then asks the document store for matching instances.
This design supports flexible entity data without making every possible field a database column. It also sets a clear limit: a definition cannot declare more indexed fields than the configured runtime slots. The default limit is four.
References: src/…/RuntimeEntityDefinitionValidator.cs• L8-38 src/…/RuntimeEntityManager.cs• L111-140 src/…/RuntimeEntityPersistenceSchemaProvider.cs• L9-13 src/…/RuntimeEntityOptions.cs• L3-6
Runtime entity schema
The runtime layer contributes one schema for definitions, one for instances, and one for audit records. The instance storage unit has fixed index slots so provider-neutral queries can find data by a definition name and an indexed field value.
The schema provider makes runtime entities a normal VNext module. The extension registration adds that provider and the entity manager to the application services.
References: src/…/RuntimeEntityPersistenceSchemaProvider.cs• L14-63 src/…/ServiceCollectionExtensions.cs• L9-21
Extension layer
Schema catalog
The extension package assembles schema descriptions from all registered VNext modules. It checks that table and storage-unit names are unique, then publishes one combined schema with the application schema name and version.
This catalog is the coordination point between module registration and provider materialization. A module therefore contributes a schema without knowing which other VNext modules are enabled.
References: src/…/IPersistenceSchemaCatalog.cs• L5-10 src/…/DefaultPersistenceSchemaCatalog.cs• L7-45
Startup materialization
Startup materialization prepares every registered document store from the combined catalog. It can be enabled by default or disabled when a separate deployment process owns database changes.
The startup task records whether materialization was skipped, completed, or failed. On failure it reports provider connection, database permission, and concurrent-start checks so an operator can recover the service without guessing at the cause.
References: src/…/PersistenceVNextOptions.cs• L3-8 src/…/PersistenceVNextStartupTask.cs• L9-51 src/…/IPersistenceVNextStatus.cs• L1-8
Module registration
The extension package exposes both service registration and module configuration. Applications can add the catalog and startup task through AddPersistenceVNext, or enable the feature through UsePersistenceVNext.
The runtime entity and secrets packages add their own schema providers to this shared catalog. The secrets package also replaces the normal secret repository so secret reads and writes use the VNext document contract.
References: src/…/ServiceCollectionExtensions.cs• L10-22 src/…/ModuleExtensions.cs• L6-11 src/…/ServiceCollectionExtensions.cs• L9-21 src/…/ServiceCollectionExtensions.cs• L9-16
Provider layers
Relational providers
The relational layer gives SQL databases one shared document store and one schema-planning path. It uses a document table plus index values for portable document queries, while a relational schema planner can also turn module tables and storage units into normal SQL tables and indexes.
SQLite, SQL Server, and PostgreSQL supply database-specific connection types and SQL dialects. Their dialects handle parameter syntax, identifier quoting, timestamp values, and materialization locks where the database needs one.
| Provider | VNext role |
|---|---|
| SQLite | Lightweight relational provider and local test target |
| SQL Server | Relational provider with a transaction-scoped materialization lock |
| PostgreSQL | Relational provider with a transaction-scoped advisory lock |
The shared relational path gives these providers the same save, load, delete, version, and indexed-query behavior. The dialect remains the place for database-specific SQL differences.
References: src/…/RelationalDocumentStore.cs• L8-107 src/…/RelationalDocumentStoreDialect.cs• L5-121 src/…/RelationalSchemaPlanner.cs• L5-59 src/…/SqliteDocumentStore.cs• L7-14 src/…/SqlServerDocumentStore.cs• L7-14 src/…/PostgreSqlDocumentStore.cs• L7-14
MongoDB provider
The MongoDB provider maps each logical storage unit to a collection and each declared index to fields under the document's index-value area. It materializes collections and indexes before serving document operations.
MongoDB keeps the same document contract as the relational providers. It also applies expected-version checks to replace and delete operations, so callers receive the same concurrency meaning even though the database write model differs.
References: src/…/MongoDbDocumentStore.cs• L7-123 src/…/MongoDbDatabasePlanner.cs• L6-32 src/…/MongoDbDatabasePlan.cs• L3-4 src/…/MongoDbCollectionPlan.cs• L5-8
Provider physicalization
The SQL and MongoDB physicalization planners turn an approved policy into provider operations. SQL creates a dedicated table and selected indexes. MongoDB creates a dedicated collection and selected indexes.
The planners accept only the target type that matches their provider. This prevents an application from treating a relational table policy as if it were a MongoDB collection policy.
References: src/…/SqlitePhysicalizationPlanner.cs• L7-60 src/…/MongoDbPhysicalizationPlanner.cs• L6-37
Adoption boundary
Workload decisions
The runtime evaluator helps decide where VNext is suitable beside the current persistence providers. It treats portable metadata as the easiest fit, allows interactive lookups when indexes and evidence exist, and keeps specialized providers for workloads with stronger behavior needs.
| Workload signal | Direction |
|---|---|
| Metadata | Adopt VNext when provider contract tests exist |
| Interactive lookup | Adopt with physicalization and benchmark evidence |
| Hot lookup with distributed locking | Defer until benchmarks and lock tests exist |
| Queue workload | Keep the specialized provider |
| Ordered append workload | Keep the specialized provider |
This boundary is intentional. VNext provides a common storage shape, but queue leases, retries, dead letters, ordered append, replay, and retention can require provider behavior that the general document model does not promise.
References: src/…/WorkflowRuntimePersistenceEvaluator.cs• L3-69 src/…/WorkflowRuntimePersistenceDecision.cs• L3-15 src/…/WorkflowRuntimePersistenceCandidate.cs• L1-12
Existing persistence coexistence
VNext is a second persistence path, not a transparent migration of all Elsa data. A module can register a VNext schema provider and document repository while another module continues to use the existing EF Core provider.
The practical migration unit is therefore a workload or module. Teams can first move data that fits versioned documents and declared indexes, then retain specialized stores until benchmarks and provider contract tests show that VNext can meet their needs.
References: src/…/VNextSecretRepository.cs• L9-115 src/…/WorkflowRuntimePersistenceEvaluator.cs• L5-59 src/…/DefaultPersistenceSchemaCatalog.cs• L7-25
Change checklist
When these files or sections are changed, remember to consider these:
- When a module changes a storage unit, field, key, or index, keep its schema provider, document index values, query filters, and every provider planner aligned.
- When the document version or concurrency contract changes, review the relational and MongoDB stores together because callers depend on the same expected-version behavior.
- When a runtime entity field or index rule changes, update definition validation, index-slot mapping, and the runtime schema as one contract.
- When schema catalog or startup options change, check duplicate-name validation, materialization timing, status reporting, and multi-node provider locks.
- When a workload moves from an existing provider to VNext, preserve the evaluator evidence requirements for provider tests, benchmarks, locking, and rollback.
- When physicalization targets or index policies change, keep the SQL and MongoDB planners restrictive so each provider receives only its supported target.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Two registered modules declare the same table or storage unit name | The combined schema cannot be built | Keep names unique within their schema or namespace. |
| Startup materialization cannot connect or create database objects | The startup task records failure and rethrows, so the application does not start as if storage were ready | Check provider access, database permissions, and materialization lock support. |
| A save omits a value required by a declared index | The document store rejects the save | Make each repository supply all fields used by its indexes. |
| A query has no matching declared index | The document store rejects the query instead of silently scanning | Declare the filter index and use the same field names in the query. |
| A caller writes with a stale expected version | The store raises a concurrency error and does not accept the stale write | Reload the document and retry or return a conflict to the caller. |
| A runtime instance uses a draft or retired definition | The instance cannot be saved as a valid runtime entity | Publish the definition before saving instances. |
| A workload with queue or ordered-append needs is moved to VNext | Lease, retry, ordering, replay, or retention behavior may be lost | Keep the specialized provider until the evaluator's required evidence exists. |
| A physicalization policy uses the wrong provider target | The provider rejects the plan rather than creating the intended object | Use a relational-table target for SQL and a document-collection target for MongoDB. |
Key files
| File | Why to read it |
|---|---|
| src/…/IDocumentStore.cs | Defines the portable document operations used by VNext modules. |
| src/…/PersistenceSchema.cs | Shows the top-level schema shape shared by planners and providers. |
| src/…/IPersistenceSchemaProvider.cs | Shows how a module contributes a schema without selecting a database. |
| src/…/DefaultPersistenceSchemaCatalog.cs | Shows how module schemas become one application schema. |
| src/…/PersistenceVNextStartupTask.cs | Shows the startup materialization and status boundary. |
| src/…/RuntimeEntityManager.cs | Shows the runtime entity life cycle and its use of the document store. |
| src/…/RuntimeEntityPersistenceSchemaProvider.cs | Shows the schema for definitions, instances, and audit records. |
| src/…/RelationalDocumentStore.cs | Shows the shared SQL document behavior. |
| src/…/MongoDbDocumentStore.cs | Shows the MongoDB document behavior and index materialization. |
| src/…/WorkflowRuntimePersistenceEvaluator.cs | Shows how VNext adoption is bounded by workload needs and evidence. |
| src/…/VNextSecretRepository.cs | Shows how an existing Elsa module can use VNext beside older persistence paths. |