aioredis.sentinel — Sentinel Client Reference

This section contains reference for Redis Sentinel client.

Sample usage:

import aioredis

sentinel = await aioredis.create_sentinel(
   [('sentinel.host1', 26379), ('sentinel.host2', 26379)])

redis = sentinel.master_for('mymaster')
assert await redis.set('key', 'value')
assert await redis.get('key', encoding='utf-8') == 'value'

# redis client will reconnect/reconfigure automatically
#  by sentinel client instance

RedisSentinel

coroutine aioredis.sentinel.create_sentinel(sentinels, *, db=None, password=None, encoding=None, minsize=1, maxsize=10, ssl=None, parser=None, loop=None)

Creates Redis Sentinel client.

Parameters:
  • sentinels (list[tuple]) – A list of Sentinel node addresses.
  • db (int) – Redis database index to select for every master/slave connections.
  • password (str or bytes or None) – Password to use if Redis server instance requires authorization.
  • encoding (str or None) – Codec to use for response decoding.
  • minsize (int) – Minimum number of connections (to master or slave) to initialize and keep in pool. Default is 1.
  • maxsize (int) – Maximum number of connections (to master or slave) that can be created in pool. Default is 10.
  • ssl (ssl.SSLContext or True or None) – SSL context that is passed through to asyncio.BaseEventLoop.create_connection().
  • parser (callable or None) – Protocol parser class. Can be used to set custom protocol reader; expected same interface as hiredis.Reader.
  • loop (EventLoop) – An optional event loop instance (uses asyncio.get_event_loop() if not specified).
Return type:

RedisSentinel

class aioredis.sentinel.RedisSentinel

Redis Sentinel client.

The class provides interface to Redis Sentinel commands as well as few methods to acquire managed Redis clients, see below.

closed

True if client is closed.

master_for(name)

Get Redis client to named master. The client is instantiated with special connections pool which is controlled by SentinelPool. This method is not a coroutine.

Parameters:name (str) – Service name.
Return type:aioredis.Redis
slave_for(name)

Get Redis client to named slave. The client is instantiated with special connections pool which is controlled by SentinelPool. This method is not a coroutine.

Parameters:name (str) – Service name.
Return type:aioredis.Redis
execute(command, *args, **kwargs)

Execute Sentinel command. Every command is prefixed with SENTINEL automatically.

Return type:asyncio.Future
coroutine ping()

Send PING to Sentinel instance. Currently the ping command will be sent to first sentinel in pool, this may change in future.

master(name)

Returns a dictionary containing the specified master’s state. Please refer to Redis documentation for more info on returned data.

Return type:asyncio.Future
master_address(name)

Returns a (host, port) pair for the given service name.

Return type:asyncio.Future
masters()

Returns a list of dictionaries containing all masters’ states.

Return type:asyncio.Future
slaves(name)

Returns a list of slaves for the given service name.

Return type:asyncio.Future
sentinels(name)

Returns a list of Sentinels for the given service name.

Return type:asyncio.Future
monitor(name, ip, port, quorum)

Add a new master to be monitored by this Sentinel.

Parameters:
  • name (str) – Service name.
  • ip (str) – New node’s IP address.
  • port (int) – Node’s TCP port.
  • quorum (int) – Sentinel quorum.
remove(name)

Remove a master from Sentinel’s monitoring.

Parameters:name (str) – Service name
set(name, option, value)

Set Sentinel monitoring parameter for a given master. Please refer to Redis documentation for more info on options.

Parameters:
  • name (str) – Master’s name.
  • option (str) – Monitoring option name.
  • value (str) – Monitoring option value.
failover(name)

Force a failover of a named master.

Parameters:name (str) – Master’s name.
check_quorum(name)

Check if the current Sentinel configuration is able to reach the quorum needed to failover a master, and the majority needed to authorize the failover.

Parameters:name (str) – Master’s name.
close()

Close all opened connections.

coroutine wait_closed()

Wait until all connections are closed.

SentinelPool

Warning

This API has not yet stabilized and may change in future releases.

coroutine aioredis.sentinel.create_sentinel_pool(sentinels, *, db=None, password=None, encoding=None, minsize=1, maxsize=10, ssl=None, parser=None, loop=None)

Creates Sentinel connections pool.

class aioredis.sentinel.SentinelPool

Sentinel connections pool.

This pool manages both sentinel connections and Redis master/slave connections.

closed

True if pool and all connections are closed.

master_for(name)

Returns a managed connections pool for requested service name.

Parameters:name (str) – Service name.
Return type:ManagedPool
slave_for(name)

Returns a managed connections pool for requested service name.

Parameters:name (str) – Service name.
Return type:ManagedPool
execute(command, *args, **kwargs)

Execute Sentinel command.

coroutine discover(timeout=0.2)

Discover Sentinels and all monitored services within given timeout.

This will reset internal state of this pool.

coroutine discover_master(service, timeout)

Perform named master discovery.

Parameters:
  • service (str) – Service name.
  • timeout (float) – Operation timeout
Return type:

aioredis.RedisConnection

coroutine discover_slave(service, timeout)

Perform slave discovery.

Parameters:
  • service (str) – Service name.
  • timeout (float) – Operation timeout
Return type:

aioredis.RedisConnection

close()

Close all controlled connections (both to sentinel and redis).

coroutine wait_closed()

Wait until pool gets closed.