Connecting to Redis#

Generic Client#

This is the client used to connect directly to a standard Redis node.

class redis.Redis(host='localhost', port=6379, db=0, password=None, socket_timeout=None, socket_connect_timeout=None, socket_keepalive=None, socket_keepalive_options=None, connection_pool=None, unix_socket_path=None, encoding='utf-8', encoding_errors='strict', charset=None, errors=None, decode_responses=False, retry_on_timeout=False, retry_on_error=None, ssl=False, ssl_keyfile=None, ssl_certfile=None, ssl_cert_reqs='required', ssl_ca_certs=None, ssl_ca_path=None, ssl_ca_data=None, ssl_check_hostname=False, ssl_password=None, ssl_validate_ocsp=False, ssl_validate_ocsp_stapled=False, ssl_ocsp_context=None, ssl_ocsp_expected_cert=None, max_connections=None, single_connection_client=False, health_check_interval=0, client_name=None, lib_name='redis-py', lib_version='99.99.99', username=None, retry=None, redis_connect_func=None, credential_provider=None, protocol=2)[source]#

Implementation of the Redis protocol.

This abstract class provides a Python interface to all Redis commands and an implementation of the Redis protocol.

Pipelines derive from this, implementing how the commands are sent and received to the Redis server. Based on configuration, an instance will either use a ConnectionPool, or Connection object to talk to redis.

It is not safe to pass PubSub or Pipeline objects between threads.

Parameters
  • credential_provider (Optional[CredentialProvider], default: None) –

  • protocol (Optional[int], default: 2) –

execute_command(*args, **options)[source]#

Execute a command and return a parsed response

classmethod from_pool(connection_pool)[source]#

Return a Redis client from the given connection pool. The Redis client will take ownership of the connection pool and close it when the Redis client is closed.

Parameters

connection_pool (ConnectionPool) –

Return type

Redis

classmethod from_url(url, **kwargs)[source]#

Return a Redis client object configured from the given URL

For example:

redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[username@]/path/to/socket.sock?db=0[&password=password]

Three URL schemes are supported:

The username, password, hostname, path and all querystring values are passed through urllib.parse.unquote in order to replace any percent-encoded values with their corresponding characters.

There are several ways to specify a database number. The first value found will be used:

  1. A db querystring option, e.g. redis://localhost?db=0

  2. If using the redis:// or rediss:// schemes, the path argument of the url, e.g. redis://localhost/0

  3. A db keyword argument to this function.

If none of these options are specified, the default db=0 is used.

All querystring options are cast to their appropriate Python types. Boolean arguments can be specified with string values “True”/”False” or “Yes”/”No”. Values that cannot be properly cast cause a ValueError to be raised. Once parsed, the querystring arguments and keyword arguments are passed to the ConnectionPool’s class initializer. In the case of conflicting arguments, querystring arguments always win.

Parameters

url (str) –

Return type

None

get_connection_kwargs()[source]#

Get the connection’s key-word arguments

Return type

Dict

get_encoder()[source]#

Get the connection pool’s encoder

Return type

Encoder

load_external_module(funcname, func)[source]#

This function can be used to add externally defined redis modules, and their namespaces to the redis client.

funcname - A string containing the name of the function to create func - The function, being added to this class.

ex: Assume that one has a custom redis module named foomod that creates command named ‘foo.dothing’ and ‘foo.anotherthing’ in redis. To load function functions into this namespace:

from redis import Redis from foomodule import F r = Redis() r.load_external_module(“foo”, F) r.foo().dothing(‘your’, ‘arguments’)

For a concrete example see the reimport of the redisjson module in tests/test_connection.py::test_loading_external_modules

Return type

None

lock(name, timeout=None, sleep=0.1, blocking=True, blocking_timeout=None, lock_class=None, thread_local=True)[source]#

Return a new Lock object using key name that mimics the behavior of threading.Lock.

If specified, timeout indicates a maximum life for the lock. By default, it will remain locked until release() is called.

sleep indicates the amount of time to sleep per loop iteration when the lock is in blocking mode and another client is currently holding the lock.

blocking indicates whether calling acquire should block until the lock has been acquired or to fail immediately, causing acquire to return False and the lock not being acquired. Defaults to True. Note this value can be overridden by passing a blocking argument to acquire.

blocking_timeout indicates the maximum amount of time in seconds to spend trying to acquire the lock. A value of None indicates continue trying forever. blocking_timeout can be specified as a float or integer, both representing the number of seconds to wait.

lock_class forces the specified lock implementation. Note that as of redis-py 3.0, the only lock class we implement is Lock (which is a Lua-based lock). So, it’s unlikely you’ll need this parameter, unless you have created your own custom lock class.

thread_local indicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline:

time: 0, thread-1 acquires my-lock, with a timeout of 5 seconds.

thread-1 sets the token to “abc”

time: 1, thread-2 blocks trying to acquire my-lock using the

Lock instance.

time: 5, thread-1 has not yet completed. redis expires the lock

key.

time: 5, thread-2 acquired my-lock now that it’s available.

thread-2 sets the token to “xyz”

time: 6, thread-1 finishes its work and calls release(). if the

token is not stored in thread local storage, then thread-1 would see the token value as “xyz” and would be able to successfully release the thread-2’s lock.

In some use cases it’s necessary to disable thread local storage. For example, if you have code where one thread acquires a lock and passes that lock instance to a worker thread to release later. If thread local storage isn’t disabled in this case, the worker thread won’t see the token set by the thread that acquired the lock. Our assumption is that these cases aren’t common and as such default to using thread local storage.

Parameters
  • name (str) –

  • timeout (Optional[float], default: None) –

  • sleep (float, default: 0.1) –

  • blocking (bool, default: True) –

  • blocking_timeout (Optional[float], default: None) –

  • lock_class (Optional[Any], default: None) –

  • thread_local (bool, default: True) –

parse_response(connection, command_name, **options)[source]#

Parses a response from the Redis server

pipeline(transaction=True, shard_hint=None)[source]#

Return a new pipeline object that can queue multiple commands for later execution. transaction indicates whether all commands should be executed atomically. Apart from making a group of operations atomic, pipelines are useful for reducing the back-and-forth overhead between the client and server.

Return type

Pipeline

pubsub(**kwargs)[source]#

Return a Publish/Subscribe object. With this object, you can subscribe to channels and listen for messages that get published to them.

set_response_callback(command, callback)[source]#

Set a custom Response Callback

Parameters
  • command (str) –

  • callback (Callable) –

Return type

None

transaction(func, *watches, **kwargs)[source]#

Convenience method for executing the callable func as a transaction while watching all keys specified in watches. The ‘func’ callable should expect a single argument which is a Pipeline object.

Parameters

func (Callable[[Pipeline], None]) –

Return type

None

Sentinel Client#

Redis Sentinel provides high availability for Redis. There are commands that can only be executed against a Redis node running in sentinel mode. Connecting to those nodes, and executing commands against them requires a Sentinel connection.

Connection example (assumes Redis exists on the ports listed below):

>>> from redis import Sentinel
>>> sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
>>> sentinel.discover_master('mymaster')
('127.0.0.1', 6379)
>>> sentinel.discover_slaves('mymaster')
[('127.0.0.1', 6380)]

Sentinel#

class redis.sentinel.Sentinel(sentinels, min_other_sentinels=0, sentinel_kwargs=None, **connection_kwargs)[source]#

Redis Sentinel cluster client

>>> from redis.sentinel import Sentinel
>>> sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1)
>>> master.set('foo', 'bar')
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
>>> slave.get('foo')
b'bar'

sentinels is a list of sentinel nodes. Each node is represented by a pair (hostname, port).

min_other_sentinels defined a minimum number of peers for a sentinel. When querying a sentinel, if it doesn’t meet this threshold, responses from that sentinel won’t be considered valid.

sentinel_kwargs is a dictionary of connection arguments used when connecting to sentinel instances. Any argument that can be passed to a normal Redis connection can be specified here. If sentinel_kwargs is not specified, any socket_timeout and socket_keepalive options specified in connection_kwargs will be used.

connection_kwargs are keyword arguments that will be used when establishing a connection to a Redis server.

discover_master(service_name)[source]#

Asks sentinel servers for the Redis master’s address corresponding to the service labeled service_name.

Returns a pair (address, port) or raises MasterNotFoundError if no master is found.

discover_slaves(service_name)[source]#

Returns a list of alive slaves for service service_name

execute_command(*args, **kwargs)[source]#

Execute Sentinel command in sentinel nodes. once - If set to True, then execute the resulting command on a single node at random, rather than across the entire sentinel cluster.

filter_slaves(slaves)[source]#

Remove slaves that are in an ODOWN or SDOWN state

master_for(service_name, redis_class=<class 'redis.client.Redis'>, connection_pool_class=<class 'redis.sentinel.SentinelConnectionPool'>, **kwargs)[source]#

Returns a redis client instance for the service_name master.

A SentinelConnectionPool class is used to retrieve the master’s address before establishing a new connection.

NOTE: If the master’s address has changed, any cached connections to the old master are closed.

By default clients will be a Redis instance. Specify a different class to the redis_class argument if you desire something different.

The connection_pool_class specifies the connection pool to use. The SentinelConnectionPool will be used by default.

All other keyword arguments are merged with any connection_kwargs passed to this class and passed to the connection pool as keyword arguments to be used to initialize Redis connections.

slave_for(service_name, redis_class=<class 'redis.client.Redis'>, connection_pool_class=<class 'redis.sentinel.SentinelConnectionPool'>, **kwargs)[source]#

Returns redis client instance for the service_name slave(s).

A SentinelConnectionPool class is used to retrieve the slave’s address before establishing a new connection.

By default clients will be a Redis instance. Specify a different class to the redis_class argument if you desire something different.

The connection_pool_class specifies the connection pool to use. The SentinelConnectionPool will be used by default.

All other keyword arguments are merged with any connection_kwargs passed to this class and passed to the connection pool as keyword arguments to be used to initialize Redis connections.

SentinelConnectionPool#

class redis.sentinel.SentinelConnectionPool(service_name, sentinel_manager, **kwargs)[source]#

Sentinel backed connection pool.

If check_connection flag is set to True, SentinelManagedConnection sends a PING command right after establishing the connection.

rotate_slaves()[source]#

Round-robin slave balancer

Cluster Client#

This client is used for connecting to a Redis Cluster.

RedisCluster#

class redis.cluster.RedisCluster(host=None, port=6379, startup_nodes=None, cluster_error_retry_attempts=3, retry=None, require_full_coverage=False, reinitialize_steps=5, read_from_replicas=False, dynamic_startup_nodes=True, url=None, address_remap=None, **kwargs)[source]#
Parameters
  • host (Optional[str], default: None) –

  • port (int, default: 6379) –

  • startup_nodes (Optional[List[ClusterNode]], default: None) –

  • cluster_error_retry_attempts (int, default: 3) –

  • retry (Optional[Retry], default: None) –

  • require_full_coverage (bool, default: False) –

  • reinitialize_steps (int, default: 5) –

  • read_from_replicas (bool, default: False) –

  • dynamic_startup_nodes (bool, default: True) –

  • url (Optional[str], default: None) –

  • address_remap (Optional[Callable[[str, int], Tuple[str, int]]], default: None) –

determine_slot(*args)[source]#

Figure out what slot to use based on args.

Raises a RedisClusterException if there’s a missing key and we can’t

determine what slots to map the command to; or, if the keys don’t all map to the same key slot.

execute_command(*args, **kwargs)[source]#

Wrapper for ERRORS_ALLOW_RETRY error handling.

It will try the number of times specified by the config option “self.cluster_error_retry_attempts” which defaults to 3 unless manually configured.

If it reaches the number of times, the command will raise the exception

Key argument :target_nodes: can be passed with the following types:

nodes_flag: PRIMARIES, REPLICAS, ALL_NODES, RANDOM ClusterNode list<ClusterNode> dict<Any, ClusterNode>

classmethod from_url(url, **kwargs)[source]#

Return a Redis client object configured from the given URL

For example:

redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[username@]/path/to/socket.sock?db=0[&password=password]

Three URL schemes are supported:

The username, password, hostname, path and all querystring values are passed through urllib.parse.unquote in order to replace any percent-encoded values with their corresponding characters.

There are several ways to specify a database number. The first value found will be used:

  1. A db querystring option, e.g. redis://localhost?db=0

  2. If using the redis:// or rediss:// schemes, the path argument of the url, e.g. redis://localhost/0

  3. A db keyword argument to this function.

If none of these options are specified, the default db=0 is used.

All querystring options are cast to their appropriate Python types. Boolean arguments can be specified with string values “True”/”False” or “Yes”/”No”. Values that cannot be properly cast cause a ValueError to be raised. Once parsed, the querystring arguments and keyword arguments are passed to the ConnectionPool’s class initializer. In the case of conflicting arguments, querystring arguments always win.

get_connection_kwargs()[source]#

Get the connections’ key-word arguments

get_default_node()[source]#

Get the cluster’s default node

get_encoder()[source]#

Get the connections’ encoder

get_node_from_key(key, replica=False)[source]#

Get the node that holds the key’s slot. If replica set to True but the slot doesn’t have any replicas, None is returned.

keyslot(key)[source]#

Calculate keyslot for a given key. See Keys distribution model in https://redis.ac.cn/topics/cluster-spec

load_external_module(funcname, func)[source]#

This function can be used to add externally defined redis modules, and their namespaces to the redis client.

funcname - A string containing the name of the function to create func - The function, being added to this class.

lock(name, timeout=None, sleep=0.1, blocking=True, blocking_timeout=None, lock_class=None, thread_local=True)[source]#

Return a new Lock object using key name that mimics the behavior of threading.Lock.

If specified, timeout indicates a maximum life for the lock. By default, it will remain locked until release() is called.

sleep indicates the amount of time to sleep per loop iteration when the lock is in blocking mode and another client is currently holding the lock.

blocking indicates whether calling acquire should block until the lock has been acquired or to fail immediately, causing acquire to return False and the lock not being acquired. Defaults to True. Note this value can be overridden by passing a blocking argument to acquire.

blocking_timeout indicates the maximum amount of time in seconds to spend trying to acquire the lock. A value of None indicates continue trying forever. blocking_timeout can be specified as a float or integer, both representing the number of seconds to wait.

lock_class forces the specified lock implementation. Note that as of redis-py 3.0, the only lock class we implement is Lock (which is a Lua-based lock). So, it’s unlikely you’ll need this parameter, unless you have created your own custom lock class.

thread_local indicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline:

time: 0, thread-1 acquires my-lock, with a timeout of 5 seconds.

thread-1 sets the token to “abc”

time: 1, thread-2 blocks trying to acquire my-lock using the

Lock instance.

time: 5, thread-1 has not yet completed. redis expires the lock

key.

time: 5, thread-2 acquired my-lock now that it’s available.

thread-2 sets the token to “xyz”

time: 6, thread-1 finishes its work and calls release(). if the

token is not stored in thread local storage, then thread-1 would see the token value as “xyz” and would be able to successfully release the thread-2’s lock.

In some use cases it’s necessary to disable thread local storage. For example, if you have code where one thread acquires a lock and passes that lock instance to a worker thread to release later. If thread local storage isn’t disabled in this case, the worker thread won’t see the token set by the thread that acquired the lock. Our assumption is that these cases aren’t common and as such default to using thread local storage.

monitor(target_node=None)[source]#

Returns a Monitor object for the specified target node. The default cluster node will be selected if no target node was specified. Monitor is useful for handling the MONITOR command to the redis server. next_command() method returns one command from monitor listen() method yields commands from monitor.

on_connect(connection)[source]#
Initialize the connection, authenticate and select a database and send

READONLY if it is set during object initialization.

pipeline(transaction=None, shard_hint=None)[source]#
Cluster impl:

Pipelines do not work in cluster mode the same way they do in normal mode. Create a clone of this object so that simulating pipelines will work correctly. Each command will be called directly when used and when calling execute() will only return the result stack.

pubsub(node=None, host=None, port=None, **kwargs)[source]#

Allows passing a ClusterNode, or host&port, to get a pubsub instance connected to the specified node

set_default_node(node)[source]#

Set the default node of the cluster. :type node: :param node: ‘ClusterNode’ :return True if the default node was set, else False

set_response_callback(command, callback)[source]#

Set a custom Response Callback

ClusterNode#

class redis.cluster.ClusterNode(host, port, server_type=None, redis_connection=None)[source]#

Async Client#

See complete example: here

This client is used for communicating with Redis, asynchronously.

class redis.asyncio.client.Redis(*, host='localhost', port=6379, db=0, password=None, socket_timeout=None, socket_connect_timeout=None, socket_keepalive=None, socket_keepalive_options=None, connection_pool=None, unix_socket_path=None, encoding='utf-8', encoding_errors='strict', decode_responses=False, retry_on_timeout=False, retry_on_error=None, ssl=False, ssl_keyfile=None, ssl_certfile=None, ssl_cert_reqs='required', ssl_ca_certs=None, ssl_ca_data=None, ssl_check_hostname=False, max_connections=None, single_connection_client=False, health_check_interval=0, client_name=None, lib_name='redis-py', lib_version='99.99.99', username=None, retry=None, auto_close_connection_pool=None, redis_connect_func=None, credential_provider=None, protocol=2)[source]#

Implementation of the Redis protocol.

This abstract class provides a Python interface to all Redis commands and an implementation of the Redis protocol.

Pipelines derive from this, implementing how the commands are sent and received to the Redis server. Based on configuration, an instance will either use a ConnectionPool, or Connection object to talk to redis.

Parameters
  • host (str, default: 'localhost') –

  • port (int, default: 6379) –

  • db (Union[str, int], default: 0) –

  • password (Optional[str], default: None) –

  • socket_timeout (Optional[float], default: None) –

  • socket_connect_timeout (Optional[float], default: None) –

  • socket_keepalive (Optional[bool], default: None) –

  • socket_keepalive_options (Optional[Mapping[int, Union[int, bytes]]], default: None) –

  • connection_pool (Optional[ConnectionPool], default: None) –

  • unix_socket_path (Optional[str], default: None) –

  • encoding (str, default: 'utf-8') –

  • encoding_errors (str, default: 'strict') –

  • decode_responses (bool, default: False) –

  • retry_on_timeout (bool, default: False) –

  • retry_on_error (Optional[list], default: None) –

  • ssl (bool, default: False) –

  • ssl_keyfile (Optional[str], default: None) –

  • ssl_certfile (Optional[str], default: None) –

  • ssl_cert_reqs (str, default: 'required') –

  • ssl_ca_certs (Optional[str], default: None) –

  • ssl_ca_data (Optional[str], default: None) –

  • ssl_check_hostname (bool, default: False) –

  • max_connections (Optional[int], default: None) –

  • single_connection_client (bool, default: False) –

  • health_check_interval (int, default: 0) –

  • client_name (Optional[str], default: None) –

  • lib_name (Optional[str], default: 'redis-py') –

  • lib_version (Optional[str], default: '99.99.99') –

  • username (Optional[str], default: None) –

  • retry (Optional[Retry], default: None) –

  • auto_close_connection_pool (Optional[bool], default: None) –

  • credential_provider (Optional[CredentialProvider], default: None) –

  • protocol (Optional[int], default: 2) –

async aclose(close_connection_pool=None)[source]#

Closes Redis client connection :rtype: None

Parameters

close_connection_pool (Optional[bool], default: None) – decides whether to close the connection pool used

by this Redis client, overriding Redis.auto_close_connection_pool. By default, let Redis.auto_close_connection_pool decide whether to close the connection pool.

close(close_connection_pool=None)[source]#

Alias for aclose(), for backwards compatibility

Parameters

close_connection_pool (Optional[bool], default: None) –

Return type

None

async execute_command(*args, **options)[source]#

Execute a command and return a parsed response

classmethod from_pool(connection_pool)[source]#

Return a Redis client from the given connection pool. The Redis client will take ownership of the connection pool and close it when the Redis client is closed.

Parameters

connection_pool (ConnectionPool) –

Return type

Redis

classmethod from_url(url, single_connection_client=False, auto_close_connection_pool=None, **kwargs)[source]#

Return a Redis client object configured from the given URL

For example:

redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[username@]/path/to/socket.sock?db=0[&password=password]

Three URL schemes are supported:

The username, password, hostname, path and all querystring values are passed through urllib.parse.unquote in order to replace any percent-encoded values with their corresponding characters.

There are several ways to specify a database number. The first value found will be used:

  1. A db querystring option, e.g. redis://localhost?db=0

  2. If using the redis:// or rediss:// schemes, the path argument

    of the url, e.g. redis://localhost/0

  3. A db keyword argument to this function.

If none of these options are specified, the default db=0 is used.

All querystring options are cast to their appropriate Python types. Boolean arguments can be specified with string values “True”/”False” or “Yes”/”No”. Values that cannot be properly cast cause a ValueError to be raised. Once parsed, the querystring arguments and keyword arguments are passed to the ConnectionPool’s class initializer. In the case of conflicting arguments, querystring arguments always win.

Parameters
  • url (str) –

  • single_connection_client (bool, default: False) –

  • auto_close_connection_pool (Optional[bool], default: None) –

get_connection_kwargs()[source]#

Get the connection’s key-word arguments

get_encoder()[source]#

Get the connection pool’s encoder

load_external_module(funcname, func)[source]#

This function can be used to add externally defined redis modules, and their namespaces to the redis client.

funcname - A string containing the name of the function to create func - The function, being added to this class.

ex: Assume that one has a custom redis module named foomod that creates command named ‘foo.dothing’ and ‘foo.anotherthing’ in redis. To load function functions into this namespace:

from redis import Redis from foomodule import F r = Redis() r.load_external_module(“foo”, F) r.foo().dothing(‘your’, ‘arguments’)

For a concrete example see the reimport of the redisjson module in tests/test_connection.py::test_loading_external_modules

lock(name, timeout=None, sleep=0.1, blocking=True, blocking_timeout=None, lock_class=None, thread_local=True)[source]#

Return a new Lock object using key name that mimics the behavior of threading.Lock.

If specified, timeout indicates a maximum life for the lock. By default, it will remain locked until release() is called.

sleep indicates the amount of time to sleep per loop iteration when the lock is in blocking mode and another client is currently holding the lock.

blocking indicates whether calling acquire should block until the lock has been acquired or to fail immediately, causing acquire to return False and the lock not being acquired. Defaults to True. Note this value can be overridden by passing a blocking argument to acquire.

blocking_timeout indicates the maximum amount of time in seconds to spend trying to acquire the lock. A value of None indicates continue trying forever. blocking_timeout can be specified as a float or integer, both representing the number of seconds to wait.

lock_class forces the specified lock implementation. Note that as of redis-py 3.0, the only lock class we implement is Lock (which is a Lua-based lock). So, it’s unlikely you’ll need this parameter, unless you have created your own custom lock class.

thread_local indicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline:

time: 0, thread-1 acquires my-lock, with a timeout of 5 seconds.

thread-1 sets the token to “abc”

time: 1, thread-2 blocks trying to acquire my-lock using the

Lock instance.

time: 5, thread-1 has not yet completed. redis expires the lock

key.

time: 5, thread-2 acquired my-lock now that it’s available.

thread-2 sets the token to “xyz”

time: 6, thread-1 finishes its work and calls release(). if the

token is not stored in thread local storage, then thread-1 would see the token value as “xyz” and would be able to successfully release the thread-2’s lock.

In some use cases it’s necessary to disable thread local storage. For example, if you have code where one thread acquires a lock and passes that lock instance to a worker thread to release later. If thread local storage isn’t disabled in this case, the worker thread won’t see the token set by the thread that acquired the lock. Our assumption is that these cases aren’t common and as such default to using thread local storage.

Parameters
  • name (Union[bytes, str, memoryview]) –

  • timeout (Optional[float], default: None) –

  • sleep (float, default: 0.1) –

  • blocking (bool, default: True) –

  • blocking_timeout (Optional[float], default: None) –

  • lock_class (Optional[Type[Lock]], default: None) –

  • thread_local (bool, default: True) –

Return type

Lock

async parse_response(connection, command_name, **options)[source]#

Parses a response from the Redis server

Parameters
  • connection (Connection) –

  • command_name (Union[str, bytes]) –

pipeline(transaction=True, shard_hint=None)[source]#

Return a new pipeline object that can queue multiple commands for later execution. transaction indicates whether all commands should be executed atomically. Apart from making a group of operations atomic, pipelines are useful for reducing the back-and-forth overhead between the client and server.

Parameters
  • transaction (bool, default: True) –

  • shard_hint (Optional[str], default: None) –

Return type

Pipeline

pubsub(**kwargs)[source]#

Return a Publish/Subscribe object. With this object, you can subscribe to channels and listen for messages that get published to them.

Return type

PubSub

set_response_callback(command, callback)[source]#

Set a custom Response Callback

Parameters
  • command (str) –

  • callback (Union[ResponseCallbackProtocol, AsyncResponseCallbackProtocol]) –

async transaction(func, *watches, shard_hint=None, value_from_callable=False, watch_delay=None)[source]#

Convenience method for executing the callable func as a transaction while watching all keys specified in watches. The ‘func’ callable should expect a single argument which is a Pipeline object.

Parameters
  • func (Callable[[Pipeline], Union[Any, Awaitable[Any]]]) –

  • watches (Union[bytes, str, memoryview]) –

  • shard_hint (Optional[str], default: None) –

  • value_from_callable (bool, default: False) –

  • watch_delay (Optional[float], default: None) –

Async Cluster Client#

RedisCluster (Async)#

class redis.asyncio.cluster.RedisCluster(host=None, port=6379, startup_nodes=None, require_full_coverage=True, read_from_replicas=False, reinitialize_steps=5, cluster_error_retry_attempts=3, connection_error_retry_attempts=3, max_connections=2147483648, db=0, path=None, credential_provider=None, username=None, password=None, client_name=None, lib_name='redis-py', lib_version='99.99.99', encoding='utf-8', encoding_errors='strict', decode_responses=False, health_check_interval=0, socket_connect_timeout=None, socket_keepalive=False, socket_keepalive_options=None, socket_timeout=None, retry=None, retry_on_error=None, ssl=False, ssl_ca_certs=None, ssl_ca_data=None, ssl_cert_reqs='required', ssl_certfile=None, ssl_check_hostname=False, ssl_keyfile=None, protocol=2, address_remap=None)[source]#

Create a new RedisCluster client.

Pass one of parameters:

  • host & port

  • startup_nodes

Use await initialize() to find cluster nodes & create connections.
Use await close() to disconnect connections & close client.

Many commands support the target_nodes kwarg. It can be one of the NODE_FLAGS:

  • PRIMARIES

  • REPLICAS

  • ALL_NODES

  • RANDOM

  • DEFAULT_NODE

Note: This client is not thread/process/fork safe.

Parameters
  • host (Optional[str], default: None) –

    Can be used to point to a startup node

  • port (Union[str, int], default: 6379) –

    Port used if host is provided

  • startup_nodes (Optional[List[ClusterNode]], default: None) –

    ClusterNode to used as a startup node

  • require_full_coverage (bool, default: True) –

    When set to False: the client will not require a full coverage of the slots. However, if not all slots are covered, and at least one node has cluster-require-full-coverage set to yes, the server will throw a ClusterDownError for some key-based commands.
    When set to True: all slots must be covered to construct the cluster client. If not all slots are covered, RedisClusterException will be thrown.

  • read_from_replicas (bool, default: False) –

    Enable read from replicas in READONLY mode. You can read possibly stale data. When set to true, read commands will be assigned between the primary and its replications in a Round-Robin manner.

  • reinitialize_steps (int, default: 5) –

    Specifies the number of MOVED errors that need to occur before reinitializing the whole cluster topology. If a MOVED error occurs and the cluster does not need to be reinitialized on this current error handling, only the MOVED slot will be patched with the redirected node. To reinitialize the cluster on every MOVED error, set reinitialize_steps to 1. To avoid reinitializing the cluster on moved errors, set reinitialize_steps to 0.

  • cluster_error_retry_attempts (int, default: 3) –

    Number of times to retry before raising an error when TimeoutError or ConnectionError or ClusterDownError are encountered

  • connection_error_retry_attempts (int, default: 3) –

    Number of times to retry before reinitializing when TimeoutError or ConnectionError are encountered. The default backoff strategy will be set if Retry object is not passed (see default_backoff in backoff.py). To change it, pass a custom Retry object using the “retry” keyword.

  • max_connections (int, default: 2147483648) –

    Maximum number of connections per node. If there are no free connections & the maximum number of connections are already created, a MaxConnectionsError is raised. This error may be retried as defined by connection_error_retry_attempts

  • address_remap (Optional[Callable[[str, int], Tuple[str, int]]], default: None) –

    An optional callable which, when provided with an internal network address of a node, e.g. a (host, port) tuple, will return the address where the node is reachable. This can be used to map the addresses at which the nodes _think_ they are, to addresses at which a client may reach them, such as when they sit behind a proxy.

Rest of the arguments will be passed to the Connection instances when created
Raises

RedisClusterException

if any arguments are invalid or unknown. Eg:

  • db != 0 or None

  • path argument for unix socket connection

  • none of the host/port & startup_nodes were provided

Parameters
  • db (Union[str, int], default: 0) –

  • path (Optional[str], default: None) –

  • credential_provider (Optional[CredentialProvider], default: None) –

  • username (Optional[str], default: None) –

  • password (Optional[str], default: None) –

  • client_name (Optional[str], default: None) –

  • lib_name (Optional[str], default: 'redis-py') –

  • lib_version (Optional[str], default: '99.99.99') –

  • encoding (str, default: 'utf-8') –

  • encoding_errors (str, default: 'strict') –

  • decode_responses (bool, default: False) –

  • health_check_interval (float, default: 0) –

  • socket_connect_timeout (Optional[float], default: None) –

  • socket_keepalive (bool, default: False) –

  • socket_keepalive_options (Optional[Mapping[int, Union[int, bytes]]], default: None) –

  • socket_timeout (Optional[float], default: None) –

  • retry (Optional[Retry], default: None) –

  • retry_on_error (Optional[List[Type[Exception]]], default: None) –

  • ssl (bool, default: False) –

  • ssl_ca_certs (Optional[str], default: None) –

  • ssl_ca_data (Optional[str], default: None) –

  • ssl_cert_reqs (str, default: 'required') –

  • ssl_certfile (Optional[str], default: None) –

  • ssl_check_hostname (bool, default: False) –

  • ssl_keyfile (Optional[str], default: None) –

  • protocol (Optional[int], default: 2) –

classmethod from_url(url, **kwargs)[source]#

Return a Redis client object configured from the given URL.

For example:

redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0

Three URL schemes are supported:

The username, password, hostname, path and all querystring values are passed through urllib.parse.unquote in order to replace any percent-encoded values with their corresponding characters.

All querystring options are cast to their appropriate Python types. Boolean arguments can be specified with string values “True”/”False” or “Yes”/”No”. Values that cannot be properly cast cause a ValueError to be raised. Once parsed, the querystring arguments and keyword arguments are passed to Connection when created. In the case of conflicting arguments, querystring arguments are used.

Parameters
  • url (str) –

  • kwargs (Any) –

Return type

RedisCluster

async initialize()[source]#

Get all nodes from startup nodes & creates connections if not initialized.

Return type

RedisCluster

async aclose()[source]#

Close all connections & client if initialized.

Return type

None

close()[source]#

alias for aclose() for backwards compatibility

Return type

None

get_nodes()[source]#

Get all nodes of the cluster.

Return type

List[ClusterNode]

get_primaries()[source]#

Get the primary nodes of the cluster.

Return type

List[ClusterNode]

get_replicas()[source]#

Get the replica nodes of the cluster.

Return type

List[ClusterNode]

get_random_node()[source]#

Get a random node of the cluster.

Return type

ClusterNode

get_default_node()[source]#

Get the default node of the client.

Return type

ClusterNode

set_default_node(node)[source]#

Set the default node of the client.

Raises

DataError – if None is passed or node does not exist in cluster.

Parameters

node (ClusterNode) –

Return type

None

get_node(host=None, port=None, node_name=None)[source]#

Get node by (host, port) or node_name.

Parameters
  • host (Optional[str], default: None) –

  • port (Optional[int], default: None) –

  • node_name (Optional[str], default: None) –

Return type

Optional[ClusterNode]

get_node_from_key(key, replica=False)[source]#

Get the cluster node corresponding to the provided key.

Parameters
  • key (str) –

  • replica (bool, default: False) –

    Indicates if a replica should be returned
    None will returned if no replica holds this key

Raises

SlotNotCoveredError – if the key is not covered by any slot.

Return type

Optional[ClusterNode]

keyslot(key)[source]#

Find the keyslot for a given key.

See: https://redis.ac.cn/docs/manual/scaling/#redis-cluster-data-sharding

Parameters

key (Union[bytes, memoryview, str, int, float]) –

Return type

int

get_encoder()[source]#

Get the encoder object of the client.

Return type

Encoder

get_connection_kwargs()[source]#

Get the kwargs passed to Connection.

Return type

Dict[str, Optional[Any]]

set_response_callback(command, callback)[source]#

Set a custom response callback.

Parameters
  • command (str) –

  • callback (Union[ResponseCallbackProtocol, AsyncResponseCallbackProtocol]) –

Return type

None

async execute_command(*args, **kwargs)[source]#

Execute a raw command on the appropriate cluster node or target_nodes.

It will retry the command as specified by cluster_error_retry_attempts & then raise an exception.

Parameters
  • args (Union[bytes, memoryview, str, int, float]) –

    Raw command args

  • kwargs (Any) –

Raises

RedisClusterException – if target_nodes is not provided & the command can’t be mapped to a slot

Return type

Any

pipeline(transaction=None, shard_hint=None)[source]#

Create & return a new ClusterPipeline object.

Cluster implementation of pipeline does not support transaction or shard_hint.

Raises

RedisClusterException – if transaction or shard_hint are truthy values

Parameters
  • transaction (Optional[Any], default: None) –

  • shard_hint (Optional[Any], default: None) –

Return type

ClusterPipeline

lock(name, timeout=None, sleep=0.1, blocking=True, blocking_timeout=None, lock_class=None, thread_local=True)[source]#

Return a new Lock object using key name that mimics the behavior of threading.Lock.

If specified, timeout indicates a maximum life for the lock. By default, it will remain locked until release() is called.

sleep indicates the amount of time to sleep per loop iteration when the lock is in blocking mode and another client is currently holding the lock.

blocking indicates whether calling acquire should block until the lock has been acquired or to fail immediately, causing acquire to return False and the lock not being acquired. Defaults to True. Note this value can be overridden by passing a blocking argument to acquire.

blocking_timeout indicates the maximum amount of time in seconds to spend trying to acquire the lock. A value of None indicates continue trying forever. blocking_timeout can be specified as a float or integer, both representing the number of seconds to wait.

lock_class forces the specified lock implementation. Note that as of redis-py 3.0, the only lock class we implement is Lock (which is a Lua-based lock). So, it’s unlikely you’ll need this parameter, unless you have created your own custom lock class.

thread_local indicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline:

time: 0, thread-1 acquires my-lock, with a timeout of 5 seconds.

thread-1 sets the token to “abc”

time: 1, thread-2 blocks trying to acquire my-lock using the

Lock instance.

time: 5, thread-1 has not yet completed. redis expires the lock

key.

time: 5, thread-2 acquired my-lock now that it’s available.

thread-2 sets the token to “xyz”

time: 6, thread-1 finishes its work and calls release(). if the

token is not stored in thread local storage, then thread-1 would see the token value as “xyz” and would be able to successfully release the thread-2’s lock.

In some use cases it’s necessary to disable thread local storage. For example, if you have code where one thread acquires a lock and passes that lock instance to a worker thread to release later. If thread local storage isn’t disabled in this case, the worker thread won’t see the token set by the thread that acquired the lock. Our assumption is that these cases aren’t common and as such default to using thread local storage.

Parameters
  • name (Union[bytes, str, memoryview]) –

  • timeout (Optional[float], default: None) –

  • sleep (float, default: 0.1) –

  • blocking (bool, default: True) –

  • blocking_timeout (Optional[float], default: None) –

  • lock_class (Optional[Type[Lock]], default: None) –

  • thread_local (bool, default: True) –

Return type

Lock

ClusterNode (Async)#

class redis.asyncio.cluster.ClusterNode(host, port, server_type=None, *, max_connections=2147483648, connection_class=<class 'redis.asyncio.connection.Connection'>, **connection_kwargs)[source]#

Create a new ClusterNode.

Each ClusterNode manages multiple Connection objects for the (host, port).

Parameters
  • host (str) –

  • port (Union[str, int]) –

  • server_type (Optional[str], default: None) –

  • max_connections (int, default: 2147483648) –

  • connection_class (Type[Connection], default: <class 'redis.asyncio.connection.Connection'>) –

  • connection_kwargs (Any) –

ClusterPipeline (Async)#

class redis.asyncio.cluster.ClusterPipeline(client)[source]#

Create a new ClusterPipeline object.

Usage:

result = await (
    rc.pipeline()
    .set("A", 1)
    .get("A")
    .hset("K", "F", "V")
    .hgetall("K")
    .mset_nonatomic({"A": 2, "B": 3})
    .get("A")
    .get("B")
    .delete("A", "B", "K")
    .execute()
)
# result = [True, "1", 1, {"F": "V"}, True, True, "2", "3", 1, 1, 1]

Note: For commands DELETE, EXISTS, TOUCH, UNLINK, mset_nonatomic, which are split across multiple nodes, you’ll get multiple results for them in the array.

Retryable errors:
Redirection errors:
Parameters

client (RedisCluster) –

Existing RedisCluster client

execute_command(*args, **kwargs)[source]#

Append a raw command to the pipeline.

Parameters
  • args (Union[bytes, str, memoryview, int, float]) –

    Raw command args

  • kwargs (Any) –

Return type

ClusterPipeline

async execute(raise_on_error=True, allow_redirections=True)[source]#

Execute the pipeline.

It will retry the commands as specified by cluster_error_retry_attempts & then raise an exception.

Parameters
  • raise_on_error (bool, default: True) –

    Raise the first error if there are any errors

  • allow_redirections (bool, default: True) –

    Whether to retry each failed command individually in case of redirection errors

Raises

RedisClusterException – if target_nodes is not provided & the command can’t be mapped to a slot

Return type

List[Any]

Connection#

See complete example: here

Connection#

class redis.connection.Connection(host='localhost', port=6379, socket_keepalive=False, socket_keepalive_options=None, socket_type=0, **kwargs)[source]#

Manages TCP communication to and from a Redis server

Connection (Async)#

class redis.asyncio.connection.Connection(*, host='localhost', port=6379, socket_keepalive=False, socket_keepalive_options=None, socket_type=0, **kwargs)[source]#

Manages TCP communication to and from a Redis server

Parameters
  • host (str, default: 'localhost') –

  • port (Union[str, int], default: 6379) –

  • socket_keepalive (bool, default: False) –

  • socket_keepalive_options (Optional[Mapping[int, Union[int, bytes]]], default: None) –

  • socket_type (int, default: 0) –

Connection Pools#

See complete example: here

ConnectionPool#

class redis.connection.ConnectionPool(connection_class=<class 'redis.connection.Connection'>, max_connections=None, **connection_kwargs)[source]#

Create a connection pool. If max_connections is set, then this object raises ConnectionError when the pool’s limit is reached.

By default, TCP connections are created unless connection_class is specified. Use class:.UnixDomainSocketConnection for unix sockets.

Any additional keyword arguments are passed to the constructor of connection_class.

Parameters

max_connections (Optional[int], default: None) –

close()[source]#

Close the pool, disconnecting all connections

Return type

None

disconnect(inuse_connections=True)[source]#

Disconnects connections in the pool

If inuse_connections is True, disconnect connections that are current in use, potentially by other threads. Otherwise only disconnect connections that are idle in the pool.

Parameters

inuse_connections (bool, default: True) –

Return type

None

classmethod from_url(url, **kwargs)[source]#

Return a connection pool configured from the given URL.

For example:

redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[username@]/path/to/socket.sock?db=0[&password=password]

Three URL schemes are supported:

The username, password, hostname, path and all querystring values are passed through urllib.parse.unquote in order to replace any percent-encoded values with their corresponding characters.

There are several ways to specify a database number. The first value found will be used:

  1. A db querystring option, e.g. redis://localhost?db=0

  2. If using the redis:// or rediss:// schemes, the path argument of the url, e.g. redis://localhost/0

  3. A db keyword argument to this function.

If none of these options are specified, the default db=0 is used.

All querystring options are cast to their appropriate Python types. Boolean arguments can be specified with string values “True”/”False” or “Yes”/”No”. Values that cannot be properly cast cause a ValueError to be raised. Once parsed, the querystring arguments and keyword arguments are passed to the ConnectionPool’s class initializer. In the case of conflicting arguments, querystring arguments always win.

get_connection(command_name, *keys, **options)[source]#

Get a connection from the pool

Parameters

command_name (str) –

Return type

Connection

get_encoder()[source]#

Return an encoder based on encoding settings

Return type

Encoder

make_connection()[source]#

Create a new connection

Return type

Connection

release(connection)[source]#

Releases the connection back to the pool

Parameters

connection (Connection) –

Return type

None

ConnectionPool (Async)#

class redis.asyncio.connection.ConnectionPool(connection_class=<class 'redis.asyncio.connection.Connection'>, max_connections=None, **connection_kwargs)[source]#

Create a connection pool. If max_connections is set, then this object raises ConnectionError when the pool’s limit is reached.

By default, TCP connections are created unless connection_class is specified. Use UnixDomainSocketConnection for unix sockets.

Any additional keyword arguments are passed to the constructor of connection_class.

Parameters
  • connection_class (Type[AbstractConnection], default: <class 'redis.asyncio.connection.Connection'>) –

  • max_connections (Optional[int], default: None) –

async aclose()[source]#

Close the pool, disconnecting all connections

Return type

None

can_get_connection()[source]#

Return True if a connection can be retrieved from the pool.

Return type

bool

async disconnect(inuse_connections=True)[source]#

Disconnects connections in the pool

If inuse_connections is True, disconnect connections that are current in use, potentially by other tasks. Otherwise only disconnect connections that are idle in the pool.

Parameters

inuse_connections (bool, default: True) –

async ensure_connection(connection)[source]#

Ensure that the connection object is connected and valid

Parameters

connection (AbstractConnection) –

classmethod from_url(url, **kwargs)[source]#

Return a connection pool configured from the given URL.

For example:

redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[username@]/path/to/socket.sock?db=0[&password=password]

Three URL schemes are supported:

The username, password, hostname, path and all querystring values are passed through urllib.parse.unquote in order to replace any percent-encoded values with their corresponding characters.

There are several ways to specify a database number. The first value found will be used:

  1. A db querystring option, e.g. redis://localhost?db=0

  2. If using the redis:// or rediss:// schemes, the path argument

    of the url, e.g. redis://localhost/0

  3. A db keyword argument to this function.

If none of these options are specified, the default db=0 is used.

All querystring options are cast to their appropriate Python types. Boolean arguments can be specified with string values “True”/”False” or “Yes”/”No”. Values that cannot be properly cast cause a ValueError to be raised. Once parsed, the querystring arguments and keyword arguments are passed to the ConnectionPool’s class initializer. In the case of conflicting arguments, querystring arguments always win.

Parameters

url (str) –

Return type

TypeVar(_CP, bound= ConnectionPool)

async get_connection(command_name, *keys, **options)[source]#

Get a connection from the pool

get_encoder()[source]#

Return an encoder based on encoding settings

make_connection()[source]#

Create a new connection. Can be overridden by child classes.

async release(connection)[source]#

Releases the connection back to the pool

Parameters

connection (AbstractConnection) –