Redis 101 - Combining Redis with Python, An Introduction For Beginners
Introduction
In this series of articles, I’m going to introduce you to the world’s most popular key-value pair store Redis! It is an open-source data storage system that can be used for various purposes such as in-memory database, message-broker, cache, etc. We will also discuss how you can use Redis with Python in your own project.
Although, all the codes and examples are written in python for simplicity. You can easily grasp the concepts and convert them into code by using any of your preferred programming languages. Let’s get started!
Installation
To demonstrate the power of Redis, I will be using python So, let’s install both Redis Server & Python.
Note: If you are a python 2 developer, you might be able to run this source code without any issue but, I will highly recommend you to switch to python 3 if you didn’t already!
Keep in mind that the support for python2 will be ending in 2020!
Install Redis Server
The officially suggested way of installing Redis is to compile it from the source. Redis doesn’t have any dependency so all you need is a ‘GCC compiler’ with ‘libc’. To download Redis source Visit https://redis.io. They also have a direct download link: http://download.redis.io/redis-stable.tar.gz
If you are using Linux or Mac you can use wget or, curl to fetch the source code from the command line using below command:
1 | wget [http://download.redis.io/redis-stable.tar.gz](http://download.redis.io/redis-stable.tar.gz) |
This will download a compressed tar file of the source code which you can extract using the command:
1 | tar xvzf redis-stable.tar.gz |
Once, extracted a folder named redis-stable
will be available on the current directory. Now, all you have to do is use the make & install commands and that’s it. It will compile and install Redis for you. It might take some time though to compile the binary from Redis source. The commands are given below:
1 2 3 | cd redis-stable make make install |
You might also have to use sudo
if make
shows permission denied error. Go ahead, and test the Redis Server using the command:
1 | redis-server |
It will start the server for you. Use redis-cli like below to test the server is up and running:
1 | redis-cli ping |
Redis-server will respond with a pong to indicate everything is working perfectly.
If you still face any issues, check out the official documentation:
https://redis.io/topics/quickstart
Don’t forget to read the Securing Redis Section of that article, it is always a good idea to tighten up the security from the start. To securely use Redis Server you can follow those best practices.
Install Python
Now that you have installed the Redis server, let’s go ahead and install python as well. Linux & Mac users can skip this portion. By default, python is installed on both Operating Systems! You can test it by opening a terminal and typing:
1 | python3 --version |
It will print the current python3 version if python is already installed. For other Operating systems you can head back to python’s official website and download the binary from there:
Again, I will suggest, install the latest version of python 3. The codes in these articles are intended for Python 3 & also tested using Python 3.
Redis Client
To interact with Redis Server, we will need a python client. We will be using andymccurdy’s redis-py which is a very popular Redis client for python 3
To install redis-py you can use pip like below:
1 | pip install redis |
And that’s it! Now, open up a terminal and type python3. It will bring up the python IDLE let’s test redis-py. Go ahead and type:
1 | import redis |
If redis-py is installed correctly you won’t get any error, otherwise, you might get an ImportError
which means, redis-py is not yet installed.
Conclusion
Now that we have installed all the necessary dependencies and required tools, we can jump right into the Redis exploration! In our next article, we will be discussing about Basic Redis Usage by exploring PUB/SUB pattern! Stay tuned.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started