Redis-Py-Cluster

2.1.3 · maintenance · verified Thu Apr 09

redis-py-cluster is a Python client library designed for communicating with Redis Clusters, building upon the core functionality of the redis-py library. The current version is 2.1.3. While it was a primary solution for Redis Cluster connectivity, its development has seen an irregular cadence, with the last major release in May 2021, as cluster support has since been integrated directly into the `redis-py` library itself.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a Redis Cluster using `redis-py-cluster`, set a key-value pair, and retrieve it. It highlights the use of `startup_nodes` for initial cluster discovery and `decode_responses=True` for handling string decoding in Python 3. For multi-key commands, ensure keys hash to the same slot, often achieved using Redis hash tags (e.g., `{user1}name`, `{user1}email`).

import os
from rediscluster import RedisCluster

# Configure startup nodes for the Redis Cluster
# In a real scenario, use actual hostnames/IPs and ports
# For local testing, ensure a Redis Cluster is running on these ports
startup_nodes = [
    {"host": os.environ.get('REDIS_CLUSTER_HOST_1', '127.0.0.1'), "port": os.environ.get('REDIS_CLUSTER_PORT_1', '7000')},
    # {"host": "127.0.0.1", "port": "7001"}, # Add more nodes as needed
]

try:
    # Initialize RedisCluster client with decode_responses=True for Python 3 strings
    rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=True)

    # Test connection and basic operations
    rc.set("mykey", "Hello, Redis Cluster!")
    value = rc.get("mykey")
    print(f"Retrieved value: {value}")

    # Example of a multi-key operation (keys must hash to the same slot)
    # This example might fail if 'mykey' and 'myotherkey' don't hash to the same slot.
    # In a real cluster, you'd use Redis hash tags {key}. See documentation.
    # For this quickstart, we'll stick to single key for simplicity.
    # rc.mset({"mykey": "val1", "{mykey}other": "val2"})
    # vals = rc.mget(["mykey", "{mykey}other"])
    # print(f"Retrieved multiple values: {vals}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure a Redis Cluster is running and accessible at the specified startup nodes.")
    print("For example, using Docker: docker run -p 7000:7000 -p 7001:7001 -p 7002:7002 -p 7003:7003 -p 7004:7004 -p 7005:7005 --name redis-cluster-node-1 redislabs/redismod")
    print("Then manually create the cluster if not using a managed service.")

view raw JSON →