An Akka Persistence Module for SQL Databases using Linq2Db.
$ dotnet add package Akka.Persistence.SqlA Cross-SQL-DB Engine Akka.Persistence plugin with broad database compatibility thanks to Linq2Db.
This is a port of the amazing akka-persistence-jdbc package from Scala, with a few improvements based on C# as well as our choice of data library.
Please read the documentation carefully. Some features may have specific use case and have trade-offs (namely, compatibility modes).
If you're migrating from legacy Akka.Persistence.Sql.Common based plugins, you can read the migration guide documentation, the migration tutorial, and watch the migration tutorial video.
Akka.HostingAssuming a MS SQL Server 2019 setup:
var host = new HostBuilder()
.ConfigureServices((context, services) => {
services.AddAkka("my-system-name", (builder, provider) =>
{
builder.WithSqlPersistence(
connectionString: _myConnectionString,
providerName: ProviderName.SqlServer2019);
});
});You can also provide your own LinqToDb.DataOptions object for a more advanced configuration.
Assuming a setup with a custom NpgsqlDataSource:
NpgsqlDataSource dataSource = new NpgsqlDataSourceBuilder(_myConnectionString).Build();
DataOptions dataOptions = new DataOptions()
.UseDataProvider(DataConnection.GetDataProvider(ProviderName.PostgreSQL, dataSource.ConnectionString) ?? throw new Exception("Could not get data provider"))
.UseProvider(ProviderName.PostgreSQL)
.UseConnectionFactory((opt) => dataSource.CreateConnection());
var host = new HostBuilder()
.ConfigureServices((context, services) => {
services.AddAkka("my-system-name", (builder, provider) =>
{
builder.WithSqlPersistence(dataOptions);
});
});If dataOptions are provided, you must supply enough information for linq2db to connect to the database.
This includes setting the connection string and provider name again, if necessary for your use case.
Please consult the Linq2Db documentation for more details on configuring a valid DataOptions object.
Note that MappingSchema and RetryPolicy will always be overridden by Akka.Persistence.Sql.
Starting with Akka.Persistence.Sql v1.5.51 or later, you can add health checks for your persistence plugins. Akka.Persistence.Sql supports two types of health checks that integrate with Microsoft.Extensions.Diagnostics.HealthChecks and can be used with ASP.NET Core health check endpoints.
Standard health checks monitor the persistence plugins themselves and verify that journals and snapshot stores are properly initialized and accessible.
To configure standard health checks, use the journalBuilder and snapshotBuilder parameters with the .WithHealthCheck() method:
var host = new HostBuilder()
.ConfigureServices((context, services) => {
services.AddAkka("my-system-name", (builder, provider) =>
{
builder.WithSqlPersistence(
connectionString: _myConnectionString,
providerName: ProviderName.SqlServer2019,
journalBuilder: journal => journal.WithHealthCheck(HealthStatus.Degraded),
snapshotBuilder: snapshot => snapshot.WithHealthCheck(HealthStatus.Degraded));
});
});Standard health checks are tagged with akka, persistence, and either journal or snapshot-store for filtering and organization purposes.
Connectivity health checks proactively verify database connectivity regardless of recent operation activity. This helps detect database outages during idle periods when the standard health checks might not catch issues.
Using Akka.Hosting 1.5.55.1 or later (Simplified API):
services.AddAkka("my-system-name", (builder, provider) =>
{
builder.WithSqlPersistence(
connectionString: connectionString,
providerName: ProviderName.SqlServer2022,
journalBuilder: journal =>
{
journal.WithHealthCheck(); // Monitor plugin status
journal.WithConnectivityCheck(); // Verify database connectivity
},
snapshotBuilder: snapshot =>
{
snapshot.WithHealthCheck(); // Monitor plugin status
snapshot.WithConnectivityCheck(); // Verify database connectivity
});
});The connectivity checks will automatically:
Healthy when the database is accessibleUnhealthy (configurable) when the database is unreachable or unresponsiveConnectivity checks are tagged with akka, persistence, sql, and either journal or snapshot-store for filtering and organization purposes.
For ASP.NET Core applications, you can expose these health checks via an endpoint:
var builder = WebApplication.CreateBuilder(args);
// Add health checks service
builder.Services.AddHealthChecks();
builder.Services.AddAkka("my-system-name", (configBuilder, provider) =>
{
configBuilder.WithSqlPersistence(
connectionString: _myConnectionString,
providerName: ProviderName.SqlServer2019,
journalBuilder: journal => journal.WithHealthCheck(),
snapshotBuilder: snapshot => snapshot.WithHealthCheck());
});
var app = builder.Build();
// Map health check endpoint
app.MapHealthChecks("/healthz");
app.Run();You can customize the names, tags, and unhealthy status for both standard and connectivity health checks:
services.AddAkka("my-system-name", (builder, provider) =>
{
builder.WithSqlPersistence(
connectionString: connectionString,
providerName: ProviderName.SqlServer2022,
journalBuilder: journal =>
{
// Customize standard health check
journal.WithHealthCheck(
unHealthyStatus: HealthStatus.Degraded,
name: "sql-journal",
tags: new[] { "backend", "database", "sql" });
// Customize connectivity health check
journal.WithConnectivityCheck(
unHealthyStatus: HealthStatus.Unhealthy,
name: "sql-journal-connectivity",
tags: new[] { "backend", "database", "sql", "connectivity" });
},
snapshotBuilder: snapshot =>
{
// Customize standard health check
snapshot.WithHealthCheck(
unHealthyStatus: HealthStatus.Degraded,
name: "sql-snapshot",
tags: new[] { "backend", "database", "sql" });
// Customize connectivity health check
snapshot.WithConnectivityCheck(
unHealthyStatus: HealthStatus.Unhealthy,
name: "sql-snapshot-connectivity",
tags: new[] { "backend", "database", "sql", "connectivity" });
});
});Default tags when not specified:
["akka", "persistence", "journal"] or ["akka", "persistence", "snapshot-store"]["akka", "persistence", "sql", "journal", "connectivity"] or ["akka", "persistence", "sql", "snapshot-store", "connectivity"]These are the minimum HOCON configuration you need to start using Akka.Persistence.Sql:
akka.persistence {
journal {
plugin = "akka.persistence.journal.sql"
sql {
class = "Akka.Persistence.Sql.Journal.SqlWriteJournal, Akka.Persistence.Sql"
connection-string = "{database-connection-string}"
provider-name = "{provider-name}"
}
}
query.journal.sql {
class = "Akka.Persistence.Sql.Query.SqlReadJournalProvider, Akka.Persistence.Sql"
connection-string = "{database-connection-string}"
provider-name = "{provider-name}"
}
snapshot-store {
plugin = "akka.persistence.snapshot-store.sql"
sql {
class = "Akka.Persistence.Sql.Snapshot.SqlSnapshotStore, Akka.Persistence.Sql"
connection-string = "{database-connection-string}"
provider-name = "{provider-name}"
}
}
}LinqToDB.ProviderName static class. Refer to the Members of LinqToDb.ProviderName for included providers.Note: For best performance, one should use the most specific provider name possible. i.e. LinqToDB.ProviderName.SqlServer2012 instead of LinqToDB.ProviderName.SqlServer. Otherwise certain provider detections have to run more frequently which may impair performance slightly.
journal_metadata table containing the last sequence numberDelete Compatibility mode is expensive.
Pre-generated DDL scripts are available in docs/ddl/ for creating Akka.Persistence.Sql tables through your own migrations instead of relying on automatic table creation at runtime.
Directory Structure:
docs/ddl/default/ - For new deployments using table-mapping = defaultdocs/ddl/compat/ - For migration from legacy plugins (uses provider-specific table mappings)Each directory contains SQL scripts for each supported provider (sqlserver, postgresql, mysql, sqlite):
journal.sql - Event journal tablejournal-tags.sql - Tag table for TagMode.TagTablesnapshot.sql - Snapshot store tablemetadata.sql - Metadata table for delete-compatibility-modeWhen to use DDL scripts:
auto-initialize = false)See the DDL documentation for detailed usage instructions and examples.
Default polling intervals cause 500-1200ms projection latency. For low-latency CQRS, tune BOTH settings together:
akka.persistence.query.journal.sql {
refresh-interval = 10ms # Query stream polling
max-buffer-size = 50
journal-sequence-retrieval {
query-delay = 10ms # Sequence actor polling - CRITICAL!
}
}Important: Tuning only refresh-interval does NOT work. The sequence actor polls independently via query-delay.
| Configuration | Latency | Database Load |
|---|---|---|
| Default (1s/1s) | 500-1200ms | Low |
| Balanced (50ms/50ms) | 50-200ms | Medium |
| Low latency (10ms/10ms) | 20-100ms | High |
Trade-off: Lower = faster but higher DB load. In clusters, each node polls independently.
See persistence.conf for detailed settings documentation.
To run the build script associated with this solution, execute the following:
Windows
c:\> build.cmd all
Linux / OS X
c:\> build.sh allIf you need any information on the supported commands, please execute the build.[cmd|sh] help command.
This build script is powered by FAKE; please see their API documentation should you need to make any changes to the build.fsx file.
The attached build script will automatically do the following based on the conventions of the project names added to this project:
.Tests will automatically be treated as a XUnit2 project and will be included during the test stages of this build script;.Tests will automatically be treated as a NBench project and will be included during the test stages of this build script; and.nupkg file will automatically be placed in the bin\nuget folder upon running the build.[cmd|sh] all command.This project will automatically populate its release notes in all of its modules via the entries written inside RELEASE_NOTES.md and will automatically update the versions of all assemblies and NuGet packages via the metadata included inside Directory.Build.props.