Namespacing
redis-memory supports namespacing through prefixes, allowing multiple applications or tenants to share the same Redis instance without key collisions.
Default Prefix
By default, redis-memory uses memory: as the key prefix:
Custom Prefix via Environment Variable
Set the REDIS_PREFIX environment variable to customize the prefix:
Using .env Files
Create a .env file:
Load it in your application:
from dotenv import load_dotenv
load_dotenv()
from redis_memory import Memory
mem = Memory()
mem.value = "test"
# Uses the prefix from .env
Multi-Tenant Applications
Use different prefixes for different tenants:
import os
from redis_memory import Memory
# Tenant 1
os.environ['REDIS_PREFIX'] = 'tenant1:'
mem1 = Memory()
mem1.data = "tenant 1 data"
# Tenant 2
os.environ['REDIS_PREFIX'] = 'tenant2:'
mem2 = Memory()
mem2.data = "tenant 2 data"
# Data is isolated
print(mem1.data) # "tenant 1 data"
print(mem2.data) # "tenant 2 data"
Environment-Based Prefixes
Use different prefixes for different environments:
import os
from redis_memory import Memory
env = os.getenv('ENVIRONMENT', 'development')
os.environ['REDIS_PREFIX'] = f'{env}:'
mem = Memory()
mem.config = {"env": env}
# development: -> development:config
# staging: -> staging:config
# production: -> production:config
Best Practices
- Use descriptive prefixes:
myapp:prod:is better thanmp: - Include environment:
myapp:dev:,myapp:staging:,myapp:prod: - Add tenant IDs:
myapp:tenant_123:for multi-tenancy - Avoid special characters: Stick to alphanumeric and underscores
- End with colon:
myapp:makes keys more readable
Key Structure Examples
Good prefix patterns:
myapp: # Simple application
myapp:dev: # With environment
myapp:prod:user_123: # With tenant ID
analytics:session:abc123: # Service + session
cache:v2: # With version
Inspecting Keys in Redis
View all keys for a prefix:
Or in Python:
import redis
r = redis.Redis(host='localhost', port=6379)
keys = r.keys('myapp:*')
for key in keys:
print(key.decode('utf-8'))
Cleaning Up a Namespace
Remove all keys for a specific prefix:
# Careful! This deletes all matching keys
redis-cli --scan --pattern "myapp:*" | xargs redis-cli DEL
Or in Python:
import redis
r = redis.Redis(host='localhost', port=6379)
keys = r.keys('myapp:*')
if keys:
r.delete(*keys)
Prefix Isolation
Different prefixes are completely isolated: