Pythonredisv5.0.8
PythonIntermediate

redis

Python client for Redis — the in-memory data store for caching, pub/sub, and queues.

rediscachepubsubdatabaseasync
The Problem

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.

Mental Model

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.

60-second start

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 user

Use 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).

Alternatives
memcachedSimpler, faster for pure key-value caching, but no persistence or advanced data types.
valkeyOpen-source Redis fork, fully compatible, growing after Redis license change.
aioredisMerged into redis-py 4.x. Use redis.asyncio for async support now.

Ready to explore the full API?

See every class, function, and relationship in an interactive graph.