In a Redis cluster, you can read from replicas (previously known as slaves) to distribute the read load and reduce pressure on the master. This is especially helpful when you have read-heavy applications.
To read from a replica, you need to use the
READONLY command. This command modifies the state of the connection so that it is redirected to the closest replica, rather than the master.
However, please note that there's a possibility the data you're reading from the replica may be stale. This is because replication is asynchronous in Redis, meaning there can be a slight delay between when data is written to the master and when it's replicated to its replicas.
Here's an example of how to use the
You can return to reading from the master by using the
Note: The Python redis client has built-in support for executing arbitrary commands with
execute_command(), which allows you to use commands like
READWRITE that aren't explicitly supported by the library.