Skip to content

Commit a8b8eee

Browse files
DOC-5065 added redis-py production usage page
1 parent fea4498 commit a8b8eee

File tree

1 file changed

+32
-67
lines changed

1 file changed

+32
-67
lines changed

content/develop/clients/redis-py/produsage.md

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ progress in implementing the recommendations.
2828
{{< checklist-item "#connection-pooling" >}}Connection pooling{{< /checklist-item >}}
2929
{{< checklist-item "#client-side-caching" >}}Client-side caching{{< /checklist-item >}}
3030
{{< checklist-item "#retries" >}}Retries{{< /checklist-item >}}
31-
{{< checklist-item "#timeouts" >}}Timeouts{{< /checklist-item >}}
3231
{{< checklist-item "#health-checks" >}}Health checks{{< /checklist-item >}}
3332
{{< checklist-item "#exception-handling" >}}Exception handling{{< /checklist-item >}}
3433
{{< /checklist >}}
@@ -68,11 +67,11 @@ such as temporary network outages or timeouts. When this happens,
6867
the operation will generally succeed after a few attempts, despite
6968
failing the first time.
7069

71-
You can configure `redis-py` to retry connections automatically after
72-
specified errors occur. Use an instance of the `Retry` class to
73-
specify the number of times to retry after a failure and a backoff
74-
strategy to determine the time between attempts. For example, the
75-
following code creates a `Retry` with
70+
You can configure `redis-py` to retry commands automatically when
71+
errors occur. Use an instance of the `Retry` class to
72+
specify the number of times to retry after a failure. You can also
73+
specify a backoff strategy to control the time gap between attempts.
74+
For example, the following code creates a `Retry` with
7675
[exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
7776
that will make three repeated attempts after a failure:
7877

@@ -87,7 +86,7 @@ retry = Retry(ExponentialBackoff(), 3)
8786
Pass the `Retry` instance in the `retry` parameter when you connect
8887
to Redis. When you are connecting to a standalone Redis server,
8988
you can also set the `retry_on_timeout` parameter to `True`
90-
to retry only after timeout errors, or pass a list of exception
89+
(to retry only after timeout errors), or pass a list of exception
9190
types in the `retry_on_error` parameter.
9291

9392
```py
@@ -120,38 +119,9 @@ If you specify either `retry_on_timeout` or `retry_on_error` without
120119
a `retry` parameter, the default is to retry once immediately with no
121120
backoff.
122121

123-
For a connection to a Redis cluster, you can specify a `retry` instance.
124-
However, the list of exceptions is not configurable and is always set
125-
to `TimeoutError`, `ConnectionError`, and `ClusterDownError`. You can use
126-
the `cluster_error_retry_attempts` parameter to specify the number of
127-
attempts to make after encountering one of these errors:
128-
129-
```py
130-
131-
```
132-
133-
### Timeouts
134-
135-
If a network or server error occurs while your code is opening a
136-
connection or issuing a command, it can end up hanging indefinitely.
137-
You can prevent this from happening by setting timeouts for socket
138-
reads and writes and for opening connections.
139-
140-
To set a timeout for a connection, use the `JedisPooled` or `JedisPool` constructor with the `timeout` parameter, or use `JedisClientConfig` with the `socketTimeout` and `connectionTimeout` parameters.
141-
(The socket timeout is the maximum time allowed for reading or writing data while executing a
142-
command. The connection timeout is the maximum time allowed for establishing a new connection.)
143-
144-
```java
145-
HostAndPort hostAndPort = new HostAndPort("localhost", 6379);
146-
147-
JedisPooled jedisWithTimeout = new JedisPooled(hostAndPort,
148-
DefaultJedisClientConfig.builder()
149-
.socketTimeoutMillis(5000) // set timeout to 5 seconds
150-
.connectionTimeoutMillis(5000) // set connection timeout to 5 seconds
151-
.build(),
152-
poolConfig
153-
);
154-
```
122+
For a connection to a Redis cluster, you can specify a `retry` instance,
123+
but the list of exceptions is not configurable and is always set
124+
to `TimeoutError`, `ConnectionError`, and `ClusterDownError`.
155125

156126
### Health checks
157127

@@ -184,31 +154,26 @@ reporting failure.
184154

185155
Redis handles many errors using return values from commands, but there
186156
are also situations where exceptions can be thrown. In production code,
187-
you should handle exceptions wherever they can occur. In particular,
188-
exceptions can occur when you
189-
[connect to the server]({{< relref "/develop/clients/redis-py/connect" >}}),
190-
create a [query index]({{< relref "/develop/interact/search-and-query/indexing" >}}),
191-
or execute a
192-
[watched transaction]({{< relref "/develop/clients/redis-py/transpipe#watch-keys-for-changes" >}}).
193-
194-
#### General exceptions
195-
196-
In general, Jedis can throw the following exceptions while executing commands:
197-
198-
- `JedisConnectionException` - when the connection to Redis is lost or closed unexpectedly. Configure failover to handle this exception automatically with Resilience4J and the built-in Jedis failover mechanism.
199-
- `JedisAccessControlException` - when the user does not have the permission to execute the command or the user ID and/or password are incorrect.
200-
- `JedisDataException` - when there is a problem with the data being sent to or received from the Redis server. Usually, the error message will contain more information about the failed command.
201-
- `JedisException` - this exception is a catch-all exception that can be thrown for any other unexpected errors.
202-
203-
Conditions when `JedisException` can be thrown:
204-
- Bad return from a health check with the [`PING`]({{< relref "/commands/ping" >}}) command
205-
- Failure during SHUTDOWN
206-
- Pub/Sub failure when issuing commands (disconnect)
207-
- Any unknown server messages
208-
- Sentinel: can connect to sentinel but master is not monitored or all Sentinels are down.
209-
- MULTI or DISCARD command failed
210-
- Shard commands key hash check failed or no Reachable Shards
211-
- Retry deadline exceeded/number of attempts (Retry Command Executor)
212-
- POOL - pool exhausted, error adding idle objects, returning broken resources to the pool
213-
214-
All the Jedis exceptions are runtime exceptions and in most cases irrecoverable, so in general bubble up to the API capturing the error message.
157+
you should handle exceptions wherever they can occur.
158+
159+
Import the exceptions you need to check from the `redis.exceptions`
160+
module. The list below describes some of the most common exceptions.
161+
162+
- `ConnectionError`: Thrown when a connection attempt fails
163+
(for example, when connection parameters are invalid or the server
164+
is unavailable) and sometimes when a [health check](#health-checks)
165+
fails. There is also a subclass, `AuthenticationError`, specifically
166+
for authentication failures.
167+
- `ResponseError`: Thrown when you attempt an operation that has no valid
168+
response. Examples include executing a command on the wrong type of key
169+
(as when you try an
170+
['LPUSH']({{< relref "/develop/data-types/lists#automatic-creation-and-removal-of-keys" >}})
171+
command on a string key), creating an
172+
[index]({{< relref "/develop/interact/search-and-query/indexing" >}})
173+
with a name that already exists, and using an invalid ID for a
174+
[stream entry]({{< relref "/develop/data-types/streams/#entry-ids" >}}).
175+
- `TimeoutError`: Thrown when a timeout persistently happens for a command,
176+
despite any [retries](#retries).
177+
- `WatchError`: Thrown when a
178+
[watched key]({{< relref "/develop/clients/redis-py/transpipe#watch-keys-for-changes" >}}) is
179+
modified during a transaction.

0 commit comments

Comments
 (0)