Skip to content

Commit 7490d77

Browse files
DOC-5064 added health checks and TCP keepalive
1 parent b2f5c99 commit 7490d77

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

content/develop/clients/jedis/produsage.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ progress in implementing the recommendations.
2929
{{< checklist-item "#connection-pooling" >}}Connection pooling{{< /checklist-item >}}
3030
{{< checklist-item "#client-side-caching" >}}Client-side caching{{< /checklist-item >}}
3131
{{< checklist-item "#timeouts" >}}Timeouts{{< /checklist-item >}}
32+
{{< checklist-item "#health-checks" >}}Health checks{{< /checklist-item >}}
33+
{{< checklist-item "#tcp-keepalive" >}}TCP keepalive{{< /checklist-item >}}
3234
{{< checklist-item "#exception-handling" >}}Exception handling{{< /checklist-item >}}
3335
{{< checklist-item "#dns-cache-and-redis" >}}DNS cache and Redis{{< /checklist-item >}}
3436
{{< /checklist >}}
@@ -84,6 +86,45 @@ JedisPooled jedisWithTimeout = new JedisPooled(hostAndPort,
8486
);
8587
```
8688

89+
### Health checks
90+
91+
If your code doesn't access the Redis server continuously then it
92+
might be useful to make a "health check" periodically (perhaps once
93+
every few seconds). You can do this using a simple
94+
[`PING`]({{< relref "/commands/ping" >}}) command:
95+
96+
```java
97+
try (Jedis jedis = jedisPool.getResource()) {
98+
if (! "PONG".equals(jedis.ping())) {
99+
// Report problem.
100+
}
101+
}
102+
```
103+
104+
Health checks help to detect problems as soon as possible without
105+
waiting for a user to report them.
106+
107+
### TCP keepalive
108+
109+
[TCP keepalive](https://en.wikipedia.org/wiki/Keepalive) is a technique
110+
where TCP packets are periodically sent on an otherwise idle connection
111+
to check that it is still working. You can enable TCP keepalive for a
112+
connection using an option on the connection config builder:
113+
114+
```java
115+
JedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
116+
.connectionTimeoutMillis(2000)
117+
.socketTimeoutMillis(2000)
118+
.tcpKeepAlive(true)
119+
.build();
120+
121+
JedisPool pool = new JedisPool(poolConfig, "redis-host", clientConfig);
122+
```
123+
124+
TCP keepalive can be especially useful to detect when unused connections
125+
in a [connection pool](#connection-pooling) have been dropped due to
126+
inactivity.
127+
87128
### Exception handling
88129

89130
Redis handles many errors using return values from commands, but there

0 commit comments

Comments
 (0)