| title | Database Resilience |
|---|---|
| layout | default |
| parent | Features |
Connection pool monitoring, circuit breakers, and connection warm-up for all 10 database providers.
Encina's database resilience feature provides production-ready monitoring and fault tolerance for database connections. All features are opt-in following Encina's pay-for-what-you-use philosophy.
Key capabilities:
- Connection Pool Monitoring: Real-time pool statistics via
IDatabaseHealthMonitor.GetPoolStatistics() - Health Checks: Active database health probing with
CheckHealthAsync()returningHealthy,Degraded, orUnhealthy - Circuit Breaker: Database-aware circuit breaker using Polly that fails fast when the database is unreachable
- Transient Error Detection: Provider-agnostic identification of recoverable database errors
- Connection Warm-up: Pre-establish connections during application startup to avoid cold-start latency
// Register your provider (ADO.NET, Dapper, or EF Core)
services.AddEncinaADO(connectionString, config => { });
// Health monitor is automatically registered as IDatabaseHealthMonitor
// Optional: Add circuit breaker pipeline behavior
services.AddEncinaPolly();
services.AddDatabaseCircuitBreaker(options =>
{
options.FailureThreshold = 0.3;
options.BreakDuration = TimeSpan.FromMinutes(1);
});services.AddEncinaEntityFrameworkCore<AppDbContext>(config =>
{
config.UseResilience(options =>
{
options.EnablePoolMonitoring = true;
options.EnableCircuitBreaker = true;
options.CircuitBreaker.FailureThreshold = 0.3;
options.CircuitBreaker.SamplingDuration = TimeSpan.FromSeconds(15);
options.CircuitBreaker.BreakDuration = TimeSpan.FromMinutes(1);
options.CircuitBreaker.MinimumThroughput = 20;
options.WarmUpConnections = 5;
options.HealthCheckInterval = TimeSpan.FromSeconds(30);
});
});The central interface for database resilience monitoring. Each of the 10 providers registers its own implementation as a singleton.
public interface IDatabaseHealthMonitor
{
string ProviderName { get; } // e.g. "ado-sqlserver", "dapper-postgresql"
bool IsCircuitOpen { get; } // Circuit breaker state (volatile bool)
ConnectionPoolStats GetPoolStatistics(); // Point-in-time pool snapshot
Task<DatabaseHealthResult> CheckHealthAsync(CancellationToken ct = default);
Task ClearPoolAsync(CancellationToken ct = default);
}Immutable record providing a snapshot of the connection pool state.
var stats = monitor.GetPoolStatistics();
Console.WriteLine($"Active: {stats.ActiveConnections}");
Console.WriteLine($"Idle: {stats.IdleConnections}");
Console.WriteLine($"Total: {stats.TotalConnections}");
Console.WriteLine($"Pending: {stats.PendingRequests}");
Console.WriteLine($"Max: {stats.MaxPoolSize}");
Console.WriteLine($"Utilization: {stats.PoolUtilization:P0}"); // Computed, clamped [0, 1]Providers that do not expose pool statistics (EF Core) return ConnectionPoolStats.CreateEmpty().
Readonly record struct with factory methods for health check results. A result carries a description and data, never an exception object or its message; report the exception type name or an error code instead.
var result = await monitor.CheckHealthAsync(ct);
switch (result.Status)
{
case DatabaseHealthStatus.Healthy:
// Database is functioning correctly
break;
case DatabaseHealthStatus.Degraded:
// Reachable but pool utilization > 90%
break;
case DatabaseHealthStatus.Unhealthy:
// Unreachable or circuit breaker is open
break;
}
// Access additional data (pool stats, provider name)
var provider = result.Data["provider"];All options are disabled by default:
| Option | Default | Description |
|---|---|---|
EnablePoolMonitoring |
false |
Register IDatabaseHealthMonitor |
EnableCircuitBreaker |
false |
Enable circuit breaker for database operations |
WarmUpConnections |
0 |
Connections to establish on startup |
HealthCheckInterval |
TimeSpan.Zero |
Periodic health check interval (0 = disabled) |
| Option | Default | Description |
|---|---|---|
FailureThreshold |
0.5 |
Failure rate to open circuit (0-1) |
SamplingDuration |
10s |
Window for measuring failure rate |
BreakDuration |
30s |
How long circuit stays open |
MinimumThroughput |
10 |
Min operations before evaluating |
IncludeTimeouts |
true |
Count timeouts as failures |
IncludeConnectionFailures |
true |
Count connection failures |
The DatabaseCircuitBreakerPipelineBehavior<TRequest, TResponse> integrates with Encina's pipeline as a behavior that wraps every command/query handler.
- Fast-path check: If
IDatabaseHealthMonitor.IsCircuitOpenistrue, the behavior immediately returns an error without invoking the handler - Polly integration: Uses
Polly.CircuitBreakerwith provider-specific configuration - State transitions: Monitors
Opened,Closed, andHalfOpenedtransitions with logging - Per-provider circuits: Each provider name gets its own circuit breaker instance (cached in
ConcurrentDictionary)
DatabaseTransientErrorPredicate identifies recoverable errors by exception type name, avoiding hard assembly references:
| Provider | Exception Types |
|---|---|
| SQL Server | SqlException |
| PostgreSQL | NpgsqlException, PostgresException |
| MySQL | MySqlException |
| MongoDB | MongoException, MongoConnectionException |
| Generic | DbException, TimeoutException, SocketException, IOException |
The predicate walks the exception hierarchy and checks inner exceptions.
| Scenario | FailureThreshold | BreakDuration | MinimumThroughput |
|---|---|---|---|
| Low-traffic API | 0.5 | 30s | 5 |
| High-traffic API | 0.3 | 60s | 20 |
| Background worker | 0.5 | 120s | 10 |
| Health check endpoint | 0.8 | 15s | 3 |
| Provider | ActiveConnections | IdleConnections | TotalConnections | MaxPoolSize |
|---|---|---|---|---|
| SQL Server (ADO/Dapper) | RetrieveStatistics() |
RetrieveStatistics() |
RetrieveStatistics() |
Connection string |
| PostgreSQL (ADO/Dapper) | 0 | 0 | 0 | Connection string |
| MySQL (ADO/Dapper) | 0 | 0 | 0 | Connection string |
| EF Core (all DBs) | CreateEmpty() |
CreateEmpty() |
CreateEmpty() |
CreateEmpty() |
| MongoDB | Cluster servers | 0 | Server count | Server count |
| Provider | Method |
|---|---|
| SQL Server | SqlConnection.ClearAllPools() |
| PostgreSQL | NpgsqlConnection.ClearAllPools() |
| MySQL | MySqlConnection.ClearPoolAsync() |
| EF Core | No-op (delegates to ADO.NET driver) |
| MongoDB | No-op (internal pool management) |
All relational providers execute SELECT 1 via the base class DatabaseHealthMonitorBase. MongoDB uses the ping command on the admin database.
classDiagram
class IDatabaseHealthMonitor {
<<interface>>
core interface, Encina package
}
class DatabaseHealthMonitorBase {
<<abstract>>
Encina.Messaging
}
class SqlServerDatabaseHealthMonitor {
Encina.ADO.SqlServer
}
class PostgreSqlDatabaseHealthMonitor {
Encina.ADO.PostgreSQL
}
class MySqlDatabaseHealthMonitor {
Encina.ADO.MySQL
}
class DapperSqlServerDatabaseHealthMonitor {
Encina.Dapper.SqlServer
}
class DapperPostgreSqlDatabaseHealthMonitor {
Encina.Dapper.PostgreSQL
}
class DapperMySqlDatabaseHealthMonitor {
Encina.Dapper.MySQL
}
class EfCoreDatabaseHealthMonitor {
Encina.EntityFrameworkCore
}
class MongoDbDatabaseHealthMonitor {
Encina.MongoDB
direct implementation
}
IDatabaseHealthMonitor <|.. DatabaseHealthMonitorBase
IDatabaseHealthMonitor <|.. MongoDbDatabaseHealthMonitor
DatabaseHealthMonitorBase <|-- SqlServerDatabaseHealthMonitor
DatabaseHealthMonitorBase <|-- PostgreSqlDatabaseHealthMonitor
DatabaseHealthMonitorBase <|-- MySqlDatabaseHealthMonitor
DatabaseHealthMonitorBase <|-- DapperSqlServerDatabaseHealthMonitor
DatabaseHealthMonitorBase <|-- DapperPostgreSqlDatabaseHealthMonitor
DatabaseHealthMonitorBase <|-- DapperMySqlDatabaseHealthMonitor
DatabaseHealthMonitorBase <|-- EfCoreDatabaseHealthMonitor
- Template Method Pattern:
DatabaseHealthMonitorBaseprovides shared logic (CheckHealthAsync,GetPoolStatistics) and delegates provider-specific work toGetPoolStatisticsCore()andClearPoolCoreAsync() - Volatile circuit state:
IsCircuitOpenusesvolatile boolfor lock-free atomic reads - Scoped connections: Each
CheckHealthAsynccall creates a new scoped connection — no shared state between invocations TryAddSingleton: Providers register withTryAddSingleton<IDatabaseHealthMonitor>so the first registration wins when multiple providers are present (ADO.NET and Dapper share the same underlying pool)
ConnectionPoolMonitoringInterceptor tracks connection lifecycle events:
// Automatically registered when resilience is enabled
services.AddEncinaEntityFrameworkCore<AppDbContext>(config =>
{
config.UseResilience(options =>
{
options.EnablePoolMonitoring = true;
});
});
// Thread-safe counters
var interceptor = serviceProvider.GetService<ConnectionPoolMonitoringInterceptor>();
Console.WriteLine($"Total opened: {interceptor.TotalConnectionsOpened}");
Console.WriteLine($"Total failed: {interceptor.TotalConnectionsFailed}");DatabasePoolMetrics exposes pool statistics as OpenTelemetry gauges:
| Metric Name | Type | Tags | Description |
|---|---|---|---|
encina.db.pool.active |
Gauge | provider |
Active connections |
encina.db.pool.idle |
Gauge | provider |
Idle connections |
encina.db.pool.total |
Gauge | provider |
Total connections |
encina.db.pool.pending |
Gauge | provider |
Pending requests |
encina.db.pool.utilization |
Gauge | provider |
Pool utilization ratio |
encina.db.circuit.open |
Gauge | provider |
Circuit breaker state (0/1) |
- Issue: #290 - Connection Pool Resilience
- Source:
src/Encina/Database/(core types),src/Encina.Messaging/Health/(base class),src/Encina.Polly/(circuit breaker) - Tests: 189 tests across 5 test types (Unit, Guard, Contract, Property, Integration)