Persistence
Comprehensive guide to choosing, configuring, and tuning persistence providers for Elsa Workflows v3, covering EF Core, MongoDB, and Dapper, along with retention, migrations, and operational best prac
Executive Summary
Elsa Workflows uses persistence providers to store workflow definitions, workflow instances, bookmarks, and execution logs. Choosing the right persistence strategy is critical for performance, scalability, and operational requirements. This guide covers:
Provider selection — When to choose EF Core, MongoDB, or Dapper
Configuration patterns — Connection strings, migrations, and store registration
Indexing recommendations — Essential indexes for common queries
Retention & cleanup — Managing completed workflows and bookmark cleanup
Migrations & versioning — Handling schema changes and rolling upgrades
Observability — Measuring persistence latency and tracing
Persistence Stores Overview
Elsa organizes persistence into logical stores, each responsible for a specific data type:
Workflow Definition Store
Stores published and draft workflow definitions
WorkflowDefinitions
Workflow Instance Store
Stores workflow execution state and history
WorkflowInstances
Bookmark Store
Stores suspension points for workflow resume
Bookmarks
Activity Execution Store
Stores activity execution records
ActivityExecutionRecords
Workflow Execution Log Store
Stores detailed execution logs
WorkflowExecutionLogRecords
Workflow Inbox Store
Stores incoming messages for correlation
WorkflowInboxMessages
Code Reference: src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs — Registers workflow definition and instance stores.
Code Reference: src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs — Registers runtime stores (bookmarks, inbox, execution logs).
Persistence Providers
Elsa supports three primary persistence providers:
Entity Framework Core (EF Core)
Best for: General-purpose relational database persistence with migration support.
Supported Databases:
SQL Server
PostgreSQL
SQLite
MySQL/MariaDB
Pros:
✅ Built-in migration support for schema versioning
✅ Mature ecosystem with robust tooling
✅ Transactional consistency across stores
✅ Wide database support
Cons:
❌ May have higher overhead for extremely high-throughput scenarios
❌ Requires migration management for schema changes
When to Choose:
Production deployments requiring schema versioning
Teams familiar with EF Core and relational databases
Scenarios requiring transactional consistency
Documentation:
SQL Server Guide - Comprehensive SQL Server setup and configuration
EF Core Migrations Guide - Working with migrations and custom entities
EF Core Setup Example - Basic configuration patterns
MongoDB
Best for: Document-oriented persistence with flexible schemas.
Pros:
✅ Flexible schema evolution without migrations
✅ Native document storage suits workflow state
✅ Horizontal scaling via sharding
✅ Built-in replication for high availability
Cons:
❌ No built-in migration tooling (schema changes require application logic)
❌ Index creation must be managed manually
❌ Different consistency model than relational databases
When to Choose:
Teams already using MongoDB
Scenarios requiring flexible schema evolution
High-volume workloads with horizontal scaling needs
See MongoDB Setup Example for configuration details.
Dapper
Best for: Performance-critical scenarios requiring fine-grained SQL control.
Pros:
✅ Minimal ORM overhead
✅ Direct SQL control for optimization
✅ Lower memory footprint
Cons:
❌ Manual schema management (no built-in migrations)
❌ Requires SQL expertise for customization
❌ Less abstraction than EF Core
When to Choose:
Extreme performance requirements
Teams with strong SQL expertise
Scenarios requiring custom query optimization
See Dapper Setup Example for configuration details.
Configuration Patterns
Basic Configuration
All persistence providers are configured through the services.AddElsa(...) method:
Code Reference: src/modules/Elsa.Workflows.Core/Features/WorkflowsFeature.cs — Core services wire-up.
Connection Strings
appsettings.json:
Environment Variables:
EF Core Migrations
For EF Core providers, migrations manage schema changes:
1. Install EF Core Tools:
2. Apply Migrations at Startup (Recommended for Development):
3. Apply Migrations via CLI (Recommended for Production):
Schema Versioning Notes:
Always test migrations in a non-production environment first
Use a staging database identical to production for migration testing
Consider blue-green deployments for zero-downtime migrations
Keep migration scripts in source control
For detailed information on working with EF Core migrations, adding custom entities, and migration strategies, see the EF Core Migrations Guide.
MongoDB Configuration
MongoDB does not use migrations. Configure the database and collection names:
Index Creation: MongoDB requires manual index creation. See Indexing Notes for recommended indexes. Refer to MongoDB Index Documentation for detailed guidance.
Mapping Considerations:
Elsa uses MongoDB driver's conventions for BSON serialization
Custom activity data must be serializable to BSON
Consider using
BsonIgnoreattribute for non-persisted properties
Dapper Configuration
Dapper requires a connection factory and manual schema setup:
Schema Responsibility:
You are responsible for creating and maintaining the database schema
Use SQL scripts or a migration tool like FluentMigrator
See Dapper Setup Example for schema scripts
Indexes & Queries
Proper indexing is essential for production performance. Create indexes for frequently queried columns:
Recommended Indexes
Workflow Instances:
Bookmarks:
Incidents:
Code Reference: src/modules/Elsa.Workflows.Core/Bookmarks/* — Bookmark hashing and storage logic.
See Indexing Notes for provider-specific guidance.
Note: Defer detailed vendor-specific index tuning (covering indexes, partial indexes, index-only scans) to official database documentation.
Retention & Cleanup
Over time, completed workflow instances and bookmarks accumulate. Configure retention policies to manage storage:
Workflow Instance Retention
Use the built-in retention feature to automatically clean up old workflow instances:
Code Reference: src/modules/Elsa.Workflows.Core/Models/WorkflowOptions.cs — Retention context and options.
Bookmark Cleanup
Orphaned bookmarks (where the associated workflow instance no longer exists) should be cleaned up:
Workflow Inbox Cleanup
The WorkflowInboxCleanup job removes stale inbox messages:
Code Reference: src/modules/Elsa.Workflows.Runtime/Features/WorkflowRuntimeFeature.cs — Inbox cleanup options.
Manual Cleanup (SQL)
For immediate cleanup needs:
Backup & Restore
Environment Consistency
When backing up and restoring Elsa databases:
Version Alignment: Ensure the Elsa version in your application matches the schema version in the database. Mismatched versions can cause runtime errors.
Consistent Backups: For clustered deployments, quiesce the cluster or use database-native snapshot capabilities to ensure consistency.
Include All Stores: If using separate databases for management and runtime stores, back up both.
Test Restores: Regularly test restore procedures in a non-production environment.
Backup Commands
PostgreSQL:
SQL Server:
MongoDB:
Migrations & Versioning
Managing Breaking Changes
When Elsa releases a new version with schema changes:
Review Release Notes: Check for migration steps or breaking changes.
Test in Staging: Apply migrations to a staging environment first.
Rolling Upgrades: For clustered deployments:
Apply database migrations first (backward-compatible changes)
Roll out new application version to nodes one at a time
Monitor for errors during transition
Rollback Plan: Keep database backups and have a rollback strategy.
EF Core Migration Steps
1. Update Elsa Packages:
2. Generate Migration:
3. Review Migration: Inspect the generated migration file for potentially destructive changes.
4. Apply Migration:
Schema Versioning Best Practices
Keep migrations in source control alongside application code
Use semantic versioning to correlate Elsa versions with schema versions
Document any manual data transformations required between versions
Consider database branching strategies for team development
Observability & Performance
Measuring Persistence Latency
Monitor database operations to identify bottlenecks:
Key Metrics to Monitor
db.query.duration
Database query execution time
P95 > 500ms
elsa.workflow_instance.save.duration
Workflow state persistence time
P95 > 1000ms
elsa.bookmark.lookup.duration
Bookmark query time
P95 > 100ms
db.connection.pool.active
Active database connections
> 80% of max pool
Tracing with Elsa.OpenTelemetry
For distributed tracing of workflow execution including persistence operations:
See Performance & Scaling Guide for detailed observability configuration.
Note: Elsa provides built-in tracing. Custom metrics (throughput, latency percentiles) are user-defined. See DOC-016 (Monitoring Guide) for implementation patterns.
Common Pitfalls
1. Long Transactions
Problem: Workflows with many activities in a single burst can hold database locks for extended periods.
Symptoms:
Lock wait timeouts
Blocked queries
Degraded throughput under load
Mitigation:
Use commit strategies to limit transaction scope (see Performance Guide)
Configure shorter lock timeouts
Consider breaking large workflows into smaller sub-workflows
2. High-Cardinality Bookmarks
Problem: Workflows creating many unique bookmarks (e.g., one per user or order) can overwhelm the bookmark index.
Symptoms:
Slow bookmark lookups
Index bloat
Memory pressure
Mitigation:
Limit bookmark cardinality by design
Use correlation IDs to group related bookmarks
Implement bookmark cleanup policies
Code Reference: src/modules/Elsa.Workflows.Core/Bookmarks/* — Understand bookmark hashing to design efficient bookmark strategies.
3. Missing Indexes
Problem: Production deployments without proper indexes suffer degraded query performance.
Symptoms:
Full table scans in query plans
Slow workflow list/search operations
High database CPU
Mitigation:
Apply recommended indexes (see Indexing Notes)
Monitor slow query logs
Use database-native query analysis tools
4. Noisy Logging of Large Payloads
Problem: Logging workflow inputs/outputs can expose sensitive data and bloat logs.
Symptoms:
Excessive log volume
Sensitive data in logs
Log aggregation costs
Mitigation:
Configure log levels appropriately for production
Use structured logging with field exclusions
Consider log retention policies
5. Connection Pool Exhaustion
Problem: High-concurrency workflows exhaust database connection pools.
Symptoms:
Timeout waiting for connection
Intermittent failures under load
Degraded throughput
Mitigation:
Increase connection pool size appropriately
Monitor pool utilization metrics
Configure connection timeout and retry policies
Related Documentation
SQL Server Guide — Complete SQL Server configuration and troubleshooting
EF Core Migrations Guide — Working with migrations and custom entities
Clustering Guide — Distributed deployment and distributed locking (DOC-015)
Troubleshooting Guide — Diagnosing common issues (DOC-017)
Performance & Scaling Guide — Commit strategies and observability (DOC-021)
Database Configuration — Basic database setup
Retention — Detailed retention configuration
Log Persistence — Activity log optimization
Example Files
Last Updated: 2025-11-28
Last updated