redis-py
Redis Python client
The official Python library for connecting to a Redis server, letting you store and retrieve data, use data structures like lists and sets, run pub/sub messaging, and work with Redis clusters in a few lines of code.
redis-py is the official Python library for working with Redis, a fast in-memory data store often used to cache information or pass messages between parts of an application. Redis itself runs as a separate server process that stores data in memory for very quick access. This library is what lets a Python program connect to that server and send it commands.
Using the library is straightforward: you import it, create a connection object pointing at your Redis server, and then call methods on that connection to store and retrieve values. For example, you can store the string "bar" under the key "foo" with one line of code, and read it back with another. The library translates those method calls into the network protocol that Redis understands.
Beyond basic key-value storage, Redis supports many data structures such as lists, sets, hashes, and sorted sets, and the library exposes all of them. It also supports running multiple commands at once in a pipeline to reduce round trips to the server, subscribing to message channels (pub/sub), and working with Redis clusters that span multiple machines.
For Python programs that need to handle many simultaneous operations without waiting, the library includes an async version that works with Python's asyncio system. There is also an optional compiled component called hiredis that speeds up parsing responses from Redis. Installation is a single pip command, and full documentation with examples is available on the project's documentation site.
Where it fits
- Cache expensive database query results in Redis from your Python app so repeated requests are served instantly from memory.
- Use Redis pub/sub to pass messages between different parts of a Python application or between microservices without a message broker.
- Pipeline multiple Redis commands into a single round trip to speed up bulk data operations.
- Use the async client with Python asyncio to handle many Redis operations concurrently in a high-throughput app like FastAPI.