Skip to content

Commit 88975d5

Browse files
DOC-5665 added health check strategies
1 parent f8f796c commit 88975d5

File tree

1 file changed

+122
-6
lines changed

1 file changed

+122
-6
lines changed

content/develop/clients/jedis/failover.md

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ diagram below shows this process:
3636

3737
{{< image filename="images/failover/failover-client-reconnect.svg" alt="Failover and client reconnection" >}}
3838

39-
The complementary technique of *failback* then involves checking the original
40-
endpoint periodically to see if it has recovered, and switching back to it
41-
when it is available again:
39+
The complementary technique of *failback* then involves periodically checking the health
40+
of endpoints that are preferred to the current, temporary endpoint.
41+
If any preferred endpoint has recovered, the connection is switched over to it.
42+
This could potentially continue until the best endpoint is available again.
4243

4344
{{< image filename="images/failover/failover-client-failback.svg" alt="Failback: client switches back to original server" width="75%" >}}
4445

@@ -86,7 +87,7 @@ server during periods of inactivity. This can help to detect when the
8687
server has failed and a failover is needed even when your app is not actively
8788
using it.
8889

89-
## Configure Jedis for failover
90+
## Failover configuration
9091

9192
The example below shows a simple case with a list of two servers,
9293
`redis-east` and `redis-west`, where `redis-east` is the preferred
@@ -194,7 +195,122 @@ The `MultiClusterClientConfig` builder has the following options to configure re
194195
| `retryIncludedExceptionList()` | See description | `List` of `Throwable` classes that should be considered as failures to be retried. By default, it includes just `JedisConnectionException`. |
195196
| `retryIgnoreExceptionList()` | `null` | `List` of `Throwable` classes that should be ignored for retry. |
196197

197-
### Health check configuration
198+
## Health check configuration
198199

199200
The general strategy for health checks is to ask the Redis server for a
200-
response that it could only give if it is healthy.
201+
response that it could only give if it is healthy. There are several
202+
specific strategies available for health checks that you can configure using the
203+
`MultiClusterClientConfig` builder. The sections below explain these
204+
in more detail.
205+
206+
### `EchoStrategy` (default)
207+
208+
The default strategy, `EchoStrategy`, periodically sends a Redis
209+
[`ECHO`]({{< relref "/commands/echo" >}}) command
210+
and checks that it gives the expected response. Any unexpected response
211+
or exception indicates an unhealthy server. Although `EchoStrategy` is
212+
very simple, it is a good basic approach for most Redis deployments.
213+
214+
### `LagAwareStrategy` (preview)
215+
216+
`LagAwareStrategy` (currently in preview) is designed specifically for
217+
Redis Enterprise [Active-Active]({{< relref "/operate/rs/databases/active-active" >}})
218+
deployments. It uses the Redis Enterprise REST API to check database availability
219+
and can also optionally check replication lag.
220+
221+
`LagAwareStrategy` determines the health of the server using the
222+
[REST API]({{< relref "/operate/rs/references/rest-api" >}}). The example
223+
below shows how to configure `LagAwareStrategy` and activate it using
224+
the `healthCheckStrategySupplier()` method of the `MultiClusterClientConfig.ClusterConfig`
225+
builder.
226+
227+
```java
228+
BiFunction<HostAndPort, Supplier<RedisCredentials>, MultiClusterClientConfig.StrategySupplier> healthCheckStrategySupplier =
229+
(HostAndPort clusterHostPort, Supplier<RedisCredentials> credentialsSupplier) -> {
230+
LagAwareStrategy.Config lagConfig = LagAwareStrategy.Config.builder(clusterHostPort, credentialsSupplier)
231+
.interval(5000) // Check every 5 seconds
232+
.timeout(3000) // 3 second timeout
233+
.extendedCheckEnabled(true) // Check replication lag
234+
.build();
235+
236+
return (hostAndPort, jedisClientConfig) -> new LagAwareStrategy(lagConfig);
237+
};
238+
239+
// Configure REST API endpoint and credentials
240+
Endpoint restEndpoint = new HostAndPort("redis-enterprise-cluster-fqdn", 9443);
241+
Supplier<RedisCredentials> credentialsSupplier = () ->
242+
new DefaultRedisCredentials("rest-api-user", "pwd");
243+
244+
MultiClusterClientConfig.StrategySupplier lagawareStrategySupplier = healthCheckStrategySupplier.apply(
245+
restEndpoint, credentialsSupplier);
246+
247+
MultiClusterClientConfig.ClusterConfig clusterConfig =
248+
MultiClusterClientConfig.ClusterConfig.builder(hostAndPort, clientConfig)
249+
.healthCheckStrategySupplier(lagawareStrategySupplier)
250+
.build();
251+
```
252+
253+
### Custom health check strategy
254+
255+
You can supply your own custom health check strategy by
256+
implementing the `HealthCheckStrategy` interface. You might
257+
use this to implement custom checks or to integrate with
258+
external monitoring tools, for example. The example below
259+
shows a simple custom strategy. As with `LagAwareStrategy`, you
260+
can pass a custom strategy implementation to the `MultiClusterClientConfig.ClusterConfig`
261+
builder with the `healthCheckStrategySupplier()` method.
262+
263+
```java
264+
MultiClusterClientConfig.StrategySupplier pingStrategy = (hostAndPort, jedisClientConfig) -> {
265+
return new HealthCheckStrategy() {
266+
@Override
267+
public int getInterval() {
268+
return 1000; // Check every second
269+
}
270+
271+
@Override
272+
public int getTimeout() {
273+
return 500; // 500ms timeout
274+
}
275+
276+
@Override
277+
public int minConsecutiveSuccessCount() {
278+
return 1; // Single success required
279+
}
280+
281+
@Override
282+
public HealthStatus doHealthCheck(Endpoint endpoint) {
283+
try (UnifiedJedis jedis = new UnifiedJedis(hostAndPort, jedisClientConfig)) {
284+
String result = jedis.ping();
285+
return "PONG".equals(result) ? HealthStatus.HEALTHY : HealthStatus.UNHEALTHY;
286+
} catch (Exception e) {
287+
return HealthStatus.UNHEALTHY;
288+
}
289+
}
290+
291+
@Override
292+
public void close() {
293+
// Cleanup resources if needed
294+
}
295+
};
296+
};
297+
298+
MultiClusterClientConfig.ClusterConfig clusterConfig =
299+
MultiClusterClientConfig.ClusterConfig.builder(hostAndPort, clientConfig)
300+
.healthCheckStrategySupplier(pingStrategy)
301+
.build();
302+
```
303+
304+
### Disable health checks
305+
306+
To disable health checks completely, use the
307+
`healthCheckEnabled()` method of the `MultiClusterClientConfig.ClusterConfig`
308+
builder:
309+
310+
```java
311+
MultiClusterClientConfig.ClusterConfig clusterConfig =
312+
MultiClusterClientConfig.ClusterConfig.builder(hostAndPort, clientConfig)
313+
.healthCheckEnabled(false) // Disable health checks entirely
314+
.build();
315+
```
316+

0 commit comments

Comments
 (0)