Redis Sentinel URL

1.0.1 · active · verified Thu Apr 16

Redis-Sentinel-Url is a Python library that provides a parser and connection factory for Redis connections, specifically supporting both standard `redis://` and the custom `redis+sentinel://` URL schemes. It allows applications to connect to Redis instances via Sentinel for high availability. The library is currently at version 1.0.1, with its last major update addressing password handling, and it appears to be in maintenance mode, offering stable functionality rather than active feature development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `redis_sentinel_url.connect()` to establish connections. It attempts to connect using a `redis+sentinel://` URL, retrieving both the Sentinel client and a `StrictRedis` client for the master. It also illustrates setting and getting a key to confirm functionality. Ensure your `REDIS_SENTINEL_URL` environment variable is correctly configured with your Redis Sentinel setup.

import os
from redis_sentinel_url import connect

# Example Sentinel URL. Replace with your actual Sentinel configuration.
# The 'mymaster' is the service name configured in Redis Sentinel.
# Use os.environ.get for sensitive info like passwords.
REDIS_SENTINEL_URL = os.environ.get(
    'REDIS_SENTINEL_URL',
    'redis+sentinel://localhost:26379,otherhost:26479/mymaster/0?password=your_redis_master_password&sentinel_password=your_sentinel_password'
)

try:
    # Connect returns a (sentinel_client, redis_client) tuple
    sentinel_client, redis_client = connect(REDIS_SENTINEL_URL)

    if sentinel_client:
        print(f"Connected to Redis Sentinel service: {sentinel_client.service_name}")
        # You can get the current master and use it
        master = sentinel_client.master_for(sentinel_client.service_name)
        print(f"Master host: {master.connection_pool.connection_kwargs.get('host')}")
        print(f"Master port: {master.connection_pool.connection_kwargs.get('port')}")
        master.set('mykey', 'hello from sentinel')
        print(f"Set 'mykey': {master.get('mykey').decode('utf-8')}")

    if redis_client:
        # If connecting directly to Redis without Sentinel, redis_client will be present and sentinel_client will be None
        print("Connected directly to Redis.")
        redis_client.set('anotherkey', 'hello from direct redis')
        print(f"Set 'anotherkey': {redis_client.get('anotherkey').decode('utf-8')}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →