Redis is an open-source, in-memory, stateless, distributed datastore. It is heavily used as a distributed cache, as a NoSQL for storing data in memory. Redis can also be used as a message broker, for serving fast in-memory analytics. But simply to get started let’s just use Redis as a cache. Redis is not as hard as it looks or may seem. You can be on your way to using it in a very short time.
The idea for a Redis mini-series started when I started writing about scaling airflow using Redis. Redis blog entries cover the following aspects
- Data Structures – Lists, Sets, Maps etc – Using Redis CLI, Python and Scala
- Using Redis with Apache Spark
- Configuring Redis Cluster
- Using AWS Elastic Cache for Redis with AWS EMR
Let’s get started! – As is the case with any software we need to download and install it. Well with Redis, as usual, there are a couple of ways
- Install it via a repo for your Linux flavour
- Download the source code and compile it.
For the purpose of this blog entry, we will run Redis on Redhat Linux.
Installing via Linux repo
This is probably the simplest way to get Redis up and running.
Step – 1 Fire the following command on linux.
Step – 2 – Check if redis is working
You should see the following on your screen
Using Redis Source code
If you have a situation where you are not able to find a Redis distribution you can always compile the source code and create one for yourself. But first download the source code from Redis website. It is only a couple of MBs.
Step – 1 – Untar the tar file
tar -xvf /home/ec2-user/redis-5.0.5
Step 2 – Make sure the following are installed
sudo yum install gcc
sudo yum install make
Step – 3 – Run the make utility
Navigate to the directory where Redis source code has been unpacked run the following commands
sudo make hiredis jemalloc linenoise lua geohash-int
sudo make install
This would compile and create an installable and install it as well. Simple as that. Once the make command is complete you should be able to find the Redis binaries in /usr/local/bin.
Step – 4 – Check if Redis is working
You should get something like this
We now have a running Redis server!!!!
Accessing Redis cache
Redis can be accessed using Redis CLI utility or using libraries for python, java, scala etc. A very nice list of libraries is available on this link. Just to get a peek into the Redis world let’s start up the Redis CLI.
Start another ssh session and type the following command
Let’s try and put a simple key-value pair into the redis via the cli using the set command
Now let’s try to get the value of the key using the get command
Keep in mind that Redis cache is persistent – what it means is that it stores everything in a file on the disk and when you restart the server you will have access to the same data once again.
Redis has a very exhaustive command-line interface and is pretty well documented here. If the documentation is overwhelming don’t worry we are going to have a look at the various data structures in the subsequent blogs.
Hope you find this helpful. Till next time…byeeeeee!!!