Skip to content

Commit 0787464

Browse files
DOC-5283 added reconnection example
1 parent 4444f8a commit 0787464

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

content/develop/clients/jedis/connect.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,53 @@ poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(1));
368368
// to prevent connection starvation
369369
JedisPooled jedis = new JedisPooled(poolConfig, "localhost", 6379);
370370
```
371+
372+
### Retrying a command after a connection failure
373+
374+
If a connection is lost before a command is completed, the command will fail with a `JedisConnectionException`. Although the connection pool manages the connections
375+
for you, you must request a new connection from the pool to retry the command.
376+
You would typically do this in a loop that makes several attempts to reconnect
377+
before aborting and reporting that the error isn't transient. The example below
378+
shows a retry loop that uses a simple
379+
[exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
380+
strategy:
381+
382+
```java
383+
JedisPooled jedis = new JedisPooled(
384+
new HostAndPort("localhost", 6379),
385+
clientConfig,
386+
poolConfig
387+
);
388+
389+
// Set max retry attempts
390+
final int MAX_RETRIES = 5;
391+
392+
// Example of retrying a command
393+
String key = "retry-example";
394+
String value = "success";
395+
396+
int attempts = 0;
397+
boolean success = false;
398+
399+
while (!success && attempts < MAX_RETRIES) {
400+
try {
401+
attempts++;
402+
String result = jedis.set(key, value);
403+
System.out.println("Command succeeded on attempt " + attempts + ": " + result);
404+
success = true;
405+
} catch (JedisConnectionException e) {
406+
System.out.println("Connection failed on attempt " + attempts + ": " + e.getMessage());
407+
if (attempts >= MAX_RETRIES) {
408+
System.out.println("Max retries reached. Giving up.");
409+
throw e;
410+
}
411+
412+
// Wait before retrying
413+
try {
414+
Thread.sleep(500 * attempts); // Exponential backoff
415+
} catch (InterruptedException ie) {
416+
Thread.currentThread().interrupt();
417+
}
418+
}
419+
}
420+
```

content/develop/clients/jedis/produsage.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ progress in implementing the recommendations.
2626

2727
{{< checklist "prodlist" >}}
2828
{{< checklist-item "#connection-pooling" >}}Connection pooling{{< /checklist-item >}}
29+
{{< checklist-item "#connection-retries" >}}Connection retries{{< /checklist-item >}}
2930
{{< checklist-item "#client-side-caching" >}}Client-side caching{{< /checklist-item >}}
3031
{{< checklist-item "#timeouts" >}}Timeouts{{< /checklist-item >}}
3132
{{< checklist-item "#health-checks" >}}Health checks{{< /checklist-item >}}
@@ -51,6 +52,13 @@ write your own code to cache and reuse open connections. See
5152
[Connect with a connection pool]({{< relref "/develop/clients/jedis/connect#connect-with-a-connection-pool" >}})
5253
to learn how to use this technique with Jedis.
5354

55+
### Connection retries
56+
57+
If a connection is lost before a command is completed, the command will fail with a `JedisConnectionException`. However, a connection error is often transient, in which case the
58+
command will succeed after one or more reconnection attempts. See
59+
[Retrying a command after a connection failure]({{< relref "/develop/clients/jedis/connect#retrying-a-command-after-a-connection-failure" >}})
60+
for an example of a simple retry loop that can recover from a transient connection error.
61+
5462
### Client-side caching
5563

5664
[Client-side caching]({{< relref "/develop/clients/client-side-caching" >}})

0 commit comments

Comments
 (0)