@@ -29,6 +29,8 @@ progress in implementing the recommendations.
29
29
{{< checklist-item "#connection-pooling" >}}Connection pooling{{< /checklist-item >}}
30
30
{{< checklist-item "#client-side-caching" >}}Client-side caching{{< /checklist-item >}}
31
31
{{< checklist-item "#timeouts" >}}Timeouts{{< /checklist-item >}}
32
+ {{< checklist-item "#health-checks" >}}Health checks{{< /checklist-item >}}
33
+ {{< checklist-item "#tcp-keepalive" >}}TCP keepalive{{< /checklist-item >}}
32
34
{{< checklist-item "#exception-handling" >}}Exception handling{{< /checklist-item >}}
33
35
{{< checklist-item "#dns-cache-and-redis" >}}DNS cache and Redis{{< /checklist-item >}}
34
36
{{< /checklist >}}
@@ -84,6 +86,45 @@ JedisPooled jedisWithTimeout = new JedisPooled(hostAndPort,
84
86
);
85
87
```
86
88
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
+
87
128
### Exception handling
88
129
89
130
Redis handles many errors using return values from commands, but there
0 commit comments