redis
Python client for Redis — the in-memory data store for caching, pub/sub, and queues.
Your app hits the database on every request. Pages are slow, DB load spikes, repeated queries fetch identical data. You need a fast in-memory layer to cache results, share state between processes, or power real-time features like notifications.
Redis is a giant dict in RAM shared across all your processes. redis-py is the Python key to that dict. You get/set strings, hashes, lists, sorted sets, and streams — all in microseconds. Think of it as a supercharged global variable that survives restarts.
Cache a slow function result
import redis, json
r = redis.Redis(host='localhost', decode_responses=True)
def get_user(user_id):
key = f'user:{user_id}'
cached = r.get(key)
if cached:
return json.loads(cached)
user = db.fetch_user(user_id) # slow
r.set(key, json.dumps(user), ex=300) # 5min TTL
return userUse it when
Caching API responses or DB queries, session storage, rate limiting counters, pub/sub messaging, leaderboards (sorted sets), task queues (BLPOP), distributed locks.
Skip it when
Primary persistent storage — Redis is RAM-first and can evict data. Complex relational queries. Large blobs (Redis excels at small, fast values).
Ready to explore the full API?
See every class, function, and relationship in an interactive graph.