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=[], ssl=False, ssl_keyfile=None, ssl_certfile=None, ssl_cert_reqs='required', ssl_ca_certs=None, ssl_ca_path=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, username=None, retry=None, redis_connect_func=None)[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.

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

Execute a command and return a parsed response

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]:[password]]@/path/to/socket.sock?db=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.

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 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_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_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.

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.

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.

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

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.

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 redis 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)]
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.

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 cluser.

class redis.cluster.RedisCluster(host=None, port=6379, startup_nodes=None, cluster_error_retry_attempts=3, require_full_coverage=False, reinitialize_steps=10, read_from_replicas=False, url=None, **kwargs)[source]
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]:[password]]@/path/to/socket.sock?db=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.

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.io/topics/cluster-spec

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. :param node: ‘ClusterNode’ :return True if the default node was set, else False

Connection Pools

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.

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.

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]:[password]]@/path/to/socket.sock?db=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.

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

get_encoder()[source]

Return an encoder based on encoding settings

make_connection()[source]

Create a new connection

release(connection)[source]

Releases the connection back to the pool

More connection examples can be found here.

Async Client

This client is used for communicating with Redis, asynchronously.

More connection examples can be found here