public static class MultiDbConfig.Builder
extends java.lang.Object
The Builder provides a fluent API for configuring all aspects of multi-database failover behavior, including retry logic, circuit breaker settings, and failback mechanisms. It uses sensible defaults based on production best practices while allowing fine-tuning for specific requirements.
MultiDbConfig,
MultiDbConfig.DatabaseConfig| Constructor and Description |
|---|
Builder()
Constructs a new Builder with the specified database configurations.
|
Builder(java.util.List<MultiDbConfig.DatabaseConfig> databaseConfigs)
Constructs a new Builder with the specified database configurations.
|
Builder(MultiDbConfig.DatabaseConfig[] databaseConfigs)
Constructs a new Builder with the specified database configurations.
|
| Modifier and Type | Method and Description |
|---|---|
MultiDbConfig |
build()
Builds and returns a new MultiDbConfig instance with all configured settings.
|
MultiDbConfig.Builder |
commandRetry(MultiDbConfig.RetryConfig commandRetry)
Sets the encapsulated retry configuration for command execution.
|
MultiDbConfig.Builder |
database(Endpoint endpoint,
float weight,
JedisClientConfig clientConfig)
Adds a database endpoint with custom client configuration.
|
MultiDbConfig.Builder |
database(MultiDbConfig.DatabaseConfig databaseConfig)
Adds a pre-configured database configuration.
|
MultiDbConfig.Builder |
delayInBetweenFailoverAttempts(int delayInBetweenFailoverAttempts)
Sets the delay in milliseconds between failover attempts.
|
MultiDbConfig.Builder |
failbackCheckInterval(long failbackCheckInterval)
Sets the interval between checks for failback opportunities to recovered databases.
|
MultiDbConfig.Builder |
failbackSupported(boolean supported)
Sets whether automatic failback to higher-priority databases is supported.
|
MultiDbConfig.Builder |
failureDetector(MultiDbConfig.CircuitBreakerConfig failureDetector)
Sets the encapsulated circuit breaker configuration for failure detection.
|
MultiDbConfig.Builder |
fallbackExceptionList(java.util.List<java.lang.Class<? extends java.lang.Throwable>> fallbackExceptionList)
Sets the list of exception classes that trigger immediate fallback to the next available
database.
|
MultiDbConfig.Builder |
fastFailover(boolean fastFailover)
Sets whether to forcefully terminate connections during failover for faster database
switching.
|
MultiDbConfig.Builder |
gracePeriod(long gracePeriod)
Sets the grace period to keep databases disabled after they become unhealthy.
|
MultiDbConfig.Builder |
maxNumFailoverAttempts(int maxNumFailoverAttempts)
Sets the maximum number of failover attempts.
|
MultiDbConfig.Builder |
retryOnFailover(boolean retryOnFailover)
Sets whether failed commands should be retried during the failover process.
|
public Builder()
public Builder(MultiDbConfig.DatabaseConfig[] databaseConfigs)
databaseConfigs - array of database configurations defining available Redis endpointsJedisValidationException - if databaseConfigs is null or emptypublic Builder(java.util.List<MultiDbConfig.DatabaseConfig> databaseConfigs)
databaseConfigs - list of database configurations defining available Redis endpointsJedisValidationException - if databaseConfigs is null or emptypublic MultiDbConfig.Builder database(MultiDbConfig.DatabaseConfig databaseConfig)
This method allows adding a fully configured DatabaseConfig instance, providing maximum flexibility for advanced configurations including custom health check strategies, connection pool settings, etc.
databaseConfig - the pre-configured database configurationpublic MultiDbConfig.Builder database(Endpoint endpoint, float weight, JedisClientConfig clientConfig)
This method allows specifying database-specific configuration such as authentication, SSL settings, timeouts, etc. This configuration will override the default client configuration for this specific database endpoint.
endpoint - the Redis server endpointweight - the weight for this endpoint (higher values = higher priority)clientConfig - the client configuration for this endpointpublic MultiDbConfig.Builder commandRetry(MultiDbConfig.RetryConfig commandRetry)
This provides a cleaner API for configuring retry behavior by using a dedicated
MultiDbConfig.RetryConfig object.
commandRetry - the retry configurationMultiDbConfig.RetryConfigpublic MultiDbConfig.Builder failureDetector(MultiDbConfig.CircuitBreakerConfig failureDetector)
This provides a cleaner API for configuring circuit breaker behavior by using a dedicated
MultiDbConfig.CircuitBreakerConfig object.
failureDetector - the circuit breaker configurationMultiDbConfig.CircuitBreakerConfigpublic MultiDbConfig.Builder fallbackExceptionList(java.util.List<java.lang.Class<? extends java.lang.Throwable>> fallbackExceptionList)
When these exceptions occur, the system will immediately attempt to failover to the next available database without waiting for circuit breaker thresholds. This enables fast failover for specific error conditions.
Default exceptions:
CallNotPermittedException - Circuit breaker is openConnectionFailoverException - Connection-level failover
requiredfallbackExceptionList - list of exception classes that trigger immediate fallbackpublic MultiDbConfig.Builder retryOnFailover(boolean retryOnFailover)
When enabled, commands that fail during failover will be retried according to the configured retry settings on the new database. When disabled, failed commands during failover will immediately return the failure to the caller.
Trade-offs:
retryOnFailover - true to retry failed commands during failover, false otherwisepublic MultiDbConfig.Builder failbackSupported(boolean supported)
When enabled, the system will automatically monitor failed da using health checks and failback to higher-priority (higher weight) databases when they recover. When disabled, failback must be triggered manually.
Requirements for automatic failback:
supported - true to enable automatic failback, false for manual failback onlypublic MultiDbConfig.Builder failbackCheckInterval(long failbackCheckInterval)
This controls how frequently the system checks if a higher-priority database has recovered and is available for failback. Lower values provide faster failback response but increase monitoring overhead.
Typical Values:
failbackCheckInterval - interval in milliseconds between failback checkspublic MultiDbConfig.Builder gracePeriod(long gracePeriod)
After a database is marked as unhealthy, it remains disabled for this grace period before being eligible for failback, even if health checks indicate recovery. This prevents rapid oscillation between databases during intermittent failures.
Considerations:
gracePeriod - grace period in millisecondspublic MultiDbConfig.Builder fastFailover(boolean fastFailover)
When enabled, existing connections to the failed database are immediately closed during failover, potentially reducing failover time but may cause some in-flight operations to fail. When disabled, connections are closed gracefully.
Trade-offs:
fastFailover - true for fast failover, false for graceful failoverpublic MultiDbConfig.Builder maxNumFailoverAttempts(int maxNumFailoverAttempts)
This setting controls how many times the system will attempt to failover to a different database before giving up. For example, if set to 3, the system will make 1 initial attempt plus 2 failover attempts for a total of 3 attempts.
Default:
maxNumFailoverAttempts - maximum number of failover attemptspublic MultiDbConfig.Builder delayInBetweenFailoverAttempts(int delayInBetweenFailoverAttempts)
This setting controls how long the system will wait before attempting to failover to a different database. For example, if set to 1000, the system will wait 1 second before attempting to failover to a different database.
Default: milliseconds
delayInBetweenFailoverAttempts - delay in milliseconds between failover attemptspublic MultiDbConfig build()
This method creates the final configuration object by copying all builder settings to the configuration instance. The builder can be reused after calling build() to create additional configurations with different settings.
Copyright © 2025. All rights reserved.