grass-redis
Redis Sentinel client with mandatory mutual TLS for high-availability Redis clusters.
⚠️ Version 0.3.0 Breaking Change: Credentials file format changed to support per-database Sentinel endpoints. See CHANGELOG.md for migration details.
Features
- Automatic failover via Sentinel with per-database configuration
- Connection pooling and caching
- Read replicas for load distribution
- Mandatory TLS security with client certificates
- Service discovery - automatically find primary and replicas
- Multiple databases - manage multiple Redis instances with different Sentinel clusters
Installation
pip install grass-redis
Quick Start
Using Factory Method (Recommended)
Load configuration from /etc/redis/credentials.env:
from grass_redis import create_client_from_credentials
# One-line client creation
with create_client_from_credentials() as client:
# Connect to database
db = client.get_primary("scrapers-01")
db.set("key", "value")
# Read from replica for load distribution
replica = client.get_replica("scrapers-01")
value = replica.get("key")
The factory method automatically:
- Loads per-database Sentinel endpoints from
/etc/redis/credentials.env - Loads all database passwords
- Configures TLS with
/etc/redis/ca.crt - Returns a ready-to-use client
Manual Configuration
from pathlib import Path
from grass_redis import RedisSentinelClient, SentinelConfig, DatabaseConfig
config = SentinelConfig(
databases={
"scrapers-01": DatabaseConfig(
sentinels=[
("10.0.1.101", 26379),
("10.0.1.102", 26379),
("10.0.1.103", 26379),
],
password="your-password",
),
"serp-dev": DatabaseConfig(
sentinels=[
("10.0.2.104", 26379),
("10.0.2.105", 26379),
("10.0.2.106", 26379),
],
password="another-password",
),
},
certs_dir=Path("/etc/redis"),
)
with RedisSentinelClient(config) as client:
db = client.get_primary("scrapers-01")
db.set("key", "value")
Configuration
Credentials File Format (v0.3.0+)
Deploy to /etc/redis/credentials.env:
# Each database specifies its own Sentinel endpoints
REDIS_SCRAPERS_01_SENTINELS="10.0.1.101:26379,10.0.1.102:26379,10.0.1.103:26379"
REDIS_SCRAPERS_01_PASSWORD="your-password"
# Different databases can use different Sentinels
REDIS_SERP_DEV_SENTINELS="10.0.2.104:26379,10.0.2.105:26379,10.0.2.106:26379"
REDIS_SERP_DEV_PASSWORD="another-password"
Required Files
/etc/redis/ca.crt- CA certificate (required, 644)/etc/redis/credentials.env- Configuration and passwords (600)
Common Operations
Basic Operations with TTL
db = client.get_primary("scrapers-01")
db.set("key", "value", ex=60) # Expires in 60 seconds
value = db.get("key")
Hash Operations
db.hset("user:123", mapping={
"name": "John",
"email": "john@example.com",
"status": "active"
})
data = db.hgetall("user:123")
name = db.hget("user:123", "name")
List Operations
# Push items to list
db.lpush("queue:tasks", "task1", "task2")
# Pop from list
task = db.rpop("queue:tasks")
# Get list length
length = db.llen("queue:tasks")
Set Operations
# Add items to set
db.sadd("processed:urls", "url1", "url2", "url3")
# Check membership
exists = db.sismember("processed:urls", "url1")
# Get all members
urls = db.smembers("processed:urls")
Using Read Replicas
Distribute read load across replicas:
with create_client_from_credentials() as client:
# Write to primary
primary = client.get_primary("redis-instance-01")
primary.set("data", "value")
# Read from replica (automatic load balancing)
replica = client.get_replica("redis-instance-01")
value = replica.get("data")
Service Discovery
Find where databases are currently running:
with create_client_from_credentials() as client:
# Find current primary
host, port = client.discover_primary("redis-instance-01")
print(f"Primary: {host}:{port}")
# Find all replicas
replicas = client.discover_replicas("redis-instance-01")
for host, port in replicas:
print(f"Replica: {host}:{port}")
Error Handling
from redis.exceptions import ConnectionError, TimeoutError
try:
with create_client_from_credentials() as client:
db = client.get_primary("redis-instance-01")
db.set("key", "value")
except FileNotFoundError as e:
print(f"Missing credentials file: {e}")
except ConnectionError as e:
print(f"Cannot connect to Redis: {e}")
except TimeoutError as e:
print(f"Operation timed out: {e}")
except KeyError as e:
print(f"Database not configured: {e}")
API
Factory Method
create_client_from_credentials()- Load from/etc/redis/credentials.env
SentinelConfig
sentinel_hosts- List[(host, port)]databases- Dict[db_name, password]certs_dir- Path (default:/etc/redis)
RedisSentinelClient
get_primary(db)- Connection for writesget_replica(db)- Connection for reads (load balanced)discover_primary(db)- Get current primary addressdiscover_replicas(db)- Get all replica addressestest_connection(db)- PING test
Key Features
- No Hardcoded Endpoints - Everything loaded from configuration files
- Sentinel Discovery - Sentinels automatically find current primary location
- TLS Required - All connections use TLS with CA certificate
- Connection Pooling - Automatically managed, connections reused efficiently
- Automatic Failover - Sentinel promotes replicas if primary fails
Example Scripts
Included example scripts:
example_production_usage.py- Comprehensive production examplesexample_new_setup.py- Simple manual configuration example
Publishing to PyPI
See PUBLISHING.md for complete instructions on publishing this package to PyPI.
License
MIT License - see LICENSE file for details.
Metadata
Release files for grass-redis 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| grass_redis-0.3.0.tar.gz | 7.2 kB | Without provenance | Details for grass_redis-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| grass_redis-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for grass_redis-0.3.0-py3-none-any.whl |
Total release size: 14.3 kB
Release files / grass_redis-0.3.0.tar.gz
| Download URL | grass_redis-0.3.0.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c9833ec2783907ffbc2771d3acd5cb9a46febf376dc8da2f31d0f99ce0815e4
|
|
BLAKE2b-256 checksum How to use checksums |
0d493d58da47f618d79027e9e653a302c6a08a80afbaf0706403723923205594
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / grass_redis-0.3.0-py3-none-any.whl
| Download URL | grass_redis-0.3.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1e6db1e8e902f9f28e8433badf27b4269f3468bff70a3eb7df3a42f4f2d4e28a
|
|
BLAKE2b-256 checksum How to use checksums |
2a22722965231328445b3e56e21b36aa3f0471a313fce27f7e12428a87e81373
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for grass-redis 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.