Python with Redis
Find tutorials, examples and technical articles that will help you to develop with Redis and Python.
Getting Started
The Python community has built many client libraries that you can find here . For your first steps with Python and Redis, this article will show how to use the recommended library: redis-py
- Redis Py
Step 1. Run a Redis server
Redis is an open source, in-memory, key-value data store most commonly used as a primary database, cache, message broker, and queue. Unlike relational databases, Redis database delivers sub-millisecond response times, enabling fast and powerful real-time applications in industries such as gaming, fintech, ad-tech, social media, healthcare, and IoT.
Redis (also called remote dictionary server) is a multi-model database, and provides several built-in data structures/data type such as Lists, Hashes, Geospatial indexes, Strings, Sets etc. You can either run Redis server in a Docker container or directly on your machine. Follow the below command line to setup a Redis server on Mac OS:
brew tap redis-stack/redis-stack
brew install --cask redis-stack
Redis Stack unifies and simplifies the developer experience of the leading Redis modules and the capabilities they provide. Learn more
Ensure that you are able to use the following Redis command to connect to the Redis instance.
redis-cli
127.0.0.1:6379>
Now, you should be able to get Redis data by using Redis commands.
Step 2. Install the Redis client library using pip
The following Python code allows you to connect to the default Redis server instance .
pip3 install redis
Step 2. Write your application code
import redis
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
redis = redis.Redis(connection_pool=pool)
redis.set('mykey', 'Hello from Python!')
value = redis.get('mykey')
print(value)
redis.zadd('vehicles', {'car' : 0})
redis.zadd('vehicles', {'bike' : 0})
vehicles = redis.zrange('vehicles', 0, -1)
print(vehicles)
Find more information about Redis database instances & Redis connections in the "Redis Connect".
More developer resources
Sample Code
Flask Simple Rate limiting Example Application that shows how to do rate limiting using various Redis datastructure.
Technical Articles & Videos
Redis Launchpad
Redis Launchpad is like an “App Store” for Redis sample apps. You can easily find apps for your preferred frameworks and languages. Check out a few of these apps below, or click here to access the complete list.
Redis University
Redis for Python Developers
A complete introduction to Redis for Python developers.