Skip to content

Migrating From redis-py

This guide provides a comprehensive comparison of how to migrate from redis-py to Valkey GLIDE, with side-by-side code examples to make the transition as smooth as possible.

Terminal window
pip3 install valkey-glide
  • redis-py offers multiple constructors for different connection configurations
  • Glide uses a configuration object that comes pre-configured with best practices

Glide typically requires minimal configuration changes for:

  • Timeout settings
  • TLS configuration
  • Read from replica settings
  • User authentication (username & password)

For more on advanced configurations, refer to the Python GLIDE Guide.

import redis
# Simple connection
r = redis.Redis(host='localhost', port=6379, db=0)
# With options
r_with_options = redis.Redis(
host='localhost',
port=6379,
username='user',
password='password',
decode_responses=True # Return strings instead of bytes
)
from redis.cluster import RedisCluster
# Simple connection
rc = RedisCluster(
startup_nodes=[
{"host": "127.0.0.1", "port": 6379},
{"host": "127.0.0.1", "port": 6380}
],
decode_responses=True
)
# With options
rc_with_options = RedisCluster(
startup_nodes=[
{"host": "127.0.0.1", "port": 6379},
{"host": "127.0.0.1", "port": 6380}
],
password="password",
decode_responses=True
)

The table below compares redis-py constructors with Glide configuration parameters:

redis-py ParameterEquivalent Glide Configuration
host: straddresses: [NodeAddress(host="host", port=port)]
port: intaddresses: [NodeAddress(host="host", port=port)]
db: int- Use client.select(db) after connection
username: strcredentials: ServerCredentials(username="username")
password: strcredentials: ServerCredentials(password="password")
socket_timeout: floatrequest_timeout: int (in milliseconds)
socket_connect_timeout: floatadvanced_configuration: AdvancedGlideClientConfiguration(connection_timeout=timeout)
decode_responses: boolUse .decode() on returned bytes
retry_on_timeout: boolconnection_backoff: ConnectionBackoffStrategy(number_of_retries=retries, factor=factor, exponent_base=base, jitter_percent=percent)
ssl: booluse_tls: bool
client_name: strclient_name: str
read_from_replicas: boolread_from: ReadFrom.REPLICA / read_from: ReadFrom.PREFER_REPLICA / read_from: ReadFrom.AZ_AFFINITY / read_from: ReadFrom.AZ_AFFINITY_REPLICAS_AND_PRIMARY Read about AZ affinity
lazy_connect: boollazy_connect: bool

Standalone Mode uses GlideClientConfiguration and Cluster Mode uses GlideClusterClientConfiguration, but the usage is similar:

# Standalone mode
client_config = GlideClientConfiguration(
addresses=[NodeAddress(host="localhost", port=6379)],
request_timeout=500 # 500ms timeout
)
client = await GlideClient.create(client_config)
# Cluster mode
cluster_config = GlideClusterClientConfiguration(
addresses=[NodeAddress(host="localhost", port=6379)],
request_timeout=500 # 500ms timeout
)
cluster_client = await GlideClusterClient.create(cluster_config)