redis-py
Official Python client for Redis with connection pooling, pipelines, Pub/Sub, and an asyncio variant.
Official Documentation ↗v5. Check the official docs for the latest API surface.Installation
pip install redisMethods
No methods match this filter.
Official Python client for Redis with connection pooling, pipelines, Pub/Sub, and an asyncio variant.
Overview
redis-py maps Redis commands onto Python methods nearly one-to-one, backed by an internal connection pool that clients acquire automatically. Besides strings, hashes, lists, sets, and streams have native methods, and Pub/Sub plus Lua scripting integrate with the same client.
Pipelines cut round trips and can run as MULTI/EXEC transactions. The redis.asyncio module provides the mirrored API for asyncio applications, sharing the same URL configuration and cluster support.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
redis.Redis— Creates a client bound to a pool; every command borrows and returns a pooled connection.get / set / delete— Core string commands; set accepts expiry in seconds, and delete removes keys returning the count.pipeline— Batches commands into one round trip; transaction True wraps them in MULTI/EXEC atomic execution.redis.asyncio— Async variant with the same command surface, awaited per call, for asyncio-based services.
Usage Notes
- Create one client per process and share it; each client owns its pool, and per-request clients exhaust sockets.
- Set decode_responses according to your payload type instead of decoding ad hoc at every call site.
- Use pipelines for bulk reads and writes; per-command round trips dominate latency in loops.
Common Pitfalls
- Missing decode_responses yields bytes and breaks string comparisons silently.
- Expiring keys you intend to persist: ex is per set call and easy to copy-paste across calls.
- The sync client blocks the event loop; inside async code always use redis.asyncio.
FAQ
- Why do I get bytes instead of strings?
- By default responses are bytes. Pass decode_responses=True to the client to get strings, which is the ergonomic choice for text-only workloads; leave bytes on when you store serialized payloads.
- How is this different from the old aioredis library?
- redis-py absorbed aioredis in v4.2 and ships it as redis.asyncio. New async code should use redis.asyncio; the standalone aioredis package is discontinued.
- When do I need transactions versus plain pipelines?
- A plain pipeline batches round trips without atomicity guarantees between commands. transaction=True issues MULTI/EXEC so the block executes as one unit, and WATCH in a retry loop gives optimistic locking for read-modify-write cycles.
Report a problem with this API docOpens a pre-filled issue on GitHub.