Book Image

Redis 4.x Cookbook

Book Image

Redis 4.x Cookbook

Overview of this book

Redis is considered the world's most popular key-value store database. Its versatility and the wide variety of use cases it enables have made it a popular choice of database for many enterprises. Based on the latest version of Redis, this book provides both step-by-step recipes and relevant the background information required to utilize its features to the fullest. It covers everything from a basic understanding of Redis data types to advanced aspects of Redis high availability, clustering, administration, and troubleshooting. This book will be your great companion to master all aspects of Redis. The book starts off by installing and configuring Redis for you to get started with ease. Moving on, all the data types and features of Redis are introduced in detail. Next, you will learn how to develop applications with Redis in Java, Python, and the Spring Boot web framework. You will also learn replication tasks, which will help you to troubleshoot replication issues. Furthermore, you will learn the steps that need to be undertaken to ensure high availability on your cluster and during production deployment. Toward the end of the book, you will learn the topmost tasks that will help you to troubleshoot your ecosystem efficiently, along with extending Redis by using different modules.
Table of Contents (21 chapters)
Title Page
Packt Upsell
Windows Environment Setup

Downloading and installing Redis

Redis has an active community on GitHub. Large numbers of pull requests have been made and merged during the years, and the author, Antirez, has always given a timely response on the issues section in GitHub. Hence, the release cycles of Redis are very rapid. From the early versions, 2.6/2.8 to 3.0/3.2, which were widely used, and then to the latest 4.x version, each release of Redis offers some essential enhancements and bug fixes. So using the latest version of Redis, if possible, is one of the best practices. In this book, we are adopting the latest version of Redis 4.0.1.

Redis is an open software written in pure C language so that we can install it by compilation. Major operating systems also include Redis binary packages in their software repository, although the Redis version is often a little out of date.

Getting ready…

You can find the download link and basic installation steps at If you would like to build Redis by compiling source code in Linux/Unix/macOS, both the gcc compiler and C Standard Library libc are needed in your environment. When it comes to OS repository installation, all you need are an internet connection and the correct repository configuration.

How to do it...

We will demonstrate the compilation installation of Redis in Ubuntu 16.04.2 LTS (Xenial Xerus). The downloading and building steps are as follows: 

  1. Set up building tools:
$ sudo apt-get install build-essential 
  1. Create a directory and enter it for Redis:
$ mkdir /redis 
$ cd /redis 
  1. Then, download Redis:
$ wget
  1. Untar it and enter the directory:
$ tar zxvf redis-4.0.1.tar.gz 
$ cd redis-4.0.1
  1. Create a directory for the Redis configuration file and copy the default configuration file into it:
$ mkdir /redis/conf 
$ cp redis.conf /redis/conf/
  1. Building dependencies:
$ cd deps 
$ make hiredis lua jemalloc linenoise 
$ cd .. 


Due to the differences among various operation systems and libraries installed on it, the aforementioned steps will be required when errors occur indicating some dependencies are not satisfied. For example, you may encounter the error message:zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory.This step is not a must for most environments, if nothing about dependencies goes wrong.

  1. Do the compilation:
$ make

If everything goes well, the following message will be shown. It means that the compilation has been done successfully:

It's a good idea to run 'make test' ;) 
make[1]: Leaving directory '/redis/redis-4.0.1/src' 
  1. Install Redis:
$ make PREFIX=/redis install 

The following messages represent the success of installation:

  1. Enter the /redis directory and verify that the Redis binary files have been generated:
$ ls /redis/bin 
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server

Congratulations! You have completed the Redis compilation installation.

Compared to the compilation installation, using apt-get in Ubuntu to install Redis is much easier. Let's take a look:

  1. First, update software repository index:
$ sudo apt-get update 
  1. And then start the installation:
$ sudo apt-get install redis-server 
  1. When it's finished, check if Redis has been set up in your environment:
$ which redis-server 

How it works...

When it comes to the Redis version selection, bear in mind that Redis follows the standard practice of versioning, which is major.minor.patch level. An even-numbered minor stands for a stable release, while an odd-numbered minor means it's an unstable version, although there are a few versions using an odd minor for Redis.

The differences between building Redis by compiling and building via a software repository, are that the former can add optimization or debugging options when compiling, and also own the flexibility of specifying the installation location during installation.

After installation, there are some executable files in the bin directory. Their description and remarks are shown in the following table:

File name




Redis Server



Redis Sentinel

A soft link for redis-server.


Redis Console Tool



Redis RDB Check Tool



Redis Append Only Files (AOF) Check Tool



Redis Benchmarking Tool


There's more...

For Windows, you can obtain the Redis release of Windows, which the Microsoft Open Technologies group used to maintain at:

Just download the .msi executable file and give it a double-click to install, keeping the default configurations.

For macOS, there is no big difference from the procedures in Linux. You can also install Redis by issuing the command, brew install redis on macOS.

See also

  • For the impact of different compilation options on Redis performance, refer to Matt Stancliff's evaluation of Redis performance for different versions by taking different compilation options:
  • For security concerns, a non-root user should be used for Redis and the Securing Redis recipe in Chapter 8, Deploying to a Production Environment will have a detailed discussion
  • You can further refer to for more information