Aller au contenu principal

Redis with Python

Prerequisites: an active Redis resource, a write application user, Python 3 with pip, and the connection URI plus key prefix returned by Lordbase. This example writes one temporary test key.

Lordbase Redis uses ACL users and a per-resource key namespace on database 0. A valid password does not authorize arbitrary keys. Native clients must add the returned prefix to every key they use.

Install and configure​

python3 -m venv .venv
source .venv/bin/activate
python -m pip install redis

Set REDIS_URL and LORDBASE_REDIS_KEY_PREFIX in your process environment. Copy the exact prefix, including its trailing colon. Use the returned ACL username and your application password. Use rediss:// when TLS is configured; keep certificate verification enabled.

Run a temporary-key check​

Create connect.py:

import os
import uuid
import redis

prefix = os.environ["LORDBASE_REDIS_KEY_PREFIX"]
if not prefix:
raise ValueError("The Lordbase Redis key prefix is required")
client = redis.Redis.from_url(
os.environ["REDIS_URL"],
decode_responses=True,
socket_connect_timeout=5,
socket_timeout=5,
)
key = prefix + "docs-check:" + uuid.uuid4().hex
try:
client.set(key, "connected", ex=60)
assert client.get(key) == "connected"
print("connected")
finally:
client.close()
python connect.py

Expected output: connected. The test key expires after 60 seconds. With a read-only user, read an existing prefixed key instead of running this write test.

Integrate safely​

Centralize prefix construction in your application. Do not prepend the prefix twice. Pass fully prefixed keys to queues, caches and lock libraries too; verify that every command they require is permitted by the managed ACL. Administrative and dangerous commands can be denied even for a privileged database user.

NOPERM can mean a missing prefix or a disallowed command, not an incorrect password. WRONGPASS points to username/password or account state. The dashboard editor adds the prefix for its supported commands; your driver does not.

Before storing irreplaceable data, resolve Redis recovery limitations. Driver details: redis-py connection documentation.