This example post is a comprehensive tutorial on Redis Installation and on how to start working with Redis.
Table Of Contents
- 1. Introduction
- 2. License
- 3. Documentation
- 4. Installing Redis
1.1 What is Redis ?
Redis is an in-memory remote database that offers high performance, replication, and a unique data model to produce a platform for solving problems. By supporting five different types of data structures, Redis accommodates a wide variety of problems that can be naturally mapped into what Redis offers, allowing you to solve your problems without having to perform the conceptual gymnastics required by other databases. Additional features like replication, persistence, and client-side sharding allow Redis to scale from a convenient way to prototype a system, all the way up to hundreds of gigabytes of data and millions of requests per second. Typical use cases of redis are session caching, message queue in applications, leaderboards and counting among others. Redis can be leveraged as a database, cache, message broker in a microservices architecture depending on the requirement.
1.2 Redis compared to other databases
Redis is a type of database that’s commonly referred to as No SQL or non-relational. In Redis, there are no tables, and there’s no database-defined or enforced way of relating data in Redis with other data in Redis.
It’s not uncommon to hear Redis compared to memcached, which is a very high-performance, key-value cache server. Like memcached, Redis can also store a mapping of keys to values and can even achieve similar performance levels as memcached. But the similarities end quickly — Redis supports the writing of its data to disk automatically in two different ways, and can store data in four structures in addition to plain string keys as memcached does. These and other differences allow Redis to solve a wider range of problems, and allow Redis to be used either as a primary database or as an auxiliary database with other storage systems.
Below is a table which compares and contrasts redis with other contemporary datastores.
Name Type Data storage options Query types Additional features Redis In-memory
Strings, lists, sets, hashes, sorted sets Commands for each data type for common access patterns, with bulk operations, and partial transaction support Publish/Subscribe, master/slave replication, disk persistence, scripting (stored procedures) memcached In-memory key-value cache Mapping of keys to
Commands for create, read, update, delete, and a few others Multithreaded server for additional performance MySQL Relational database Databases of tables of rows, views over tables, spatial and third-party extensions SELECT, INSERT, UPDATE, DELETE, functions, stored
ACID compliant (with InnoDB), master/slave and master/master replication PostgreSQL Relational database Databases of tables
of rows, views over tables, spatial and third-party extensions, customizable types
SELECT, INSERT, UPDATE, DELETE, built-in functions, custom stored procedures ACID compliant, master/slave replication, multi-master replication (third party) MongoDB On-disk
non-relational document store
Databases of tables of schema-less BSON documents Commands for create, read, update, delete, conditional queries,
Supports map-reduce operations, master/slave replication, sharding, spatial indexes
Redis is open source software released under the terms of the three clause BSD license. More details on official Redis site.
Redis team maintains the complete, very well organized and up-to-date documentation for every single feature or command you may need to know. The general documentation is available at http://redis.io/documentation while commands have own section at http://redis.io/commands.
4. Redis Installation
4.1 Installing Redis on Linux via package manager
Below following are the steps to install Redis on a linux box via apt repository.
As a first step, the apt-get package index files and existing packages need to be updated to the newest versions by using the following terminal commands.
sudo apt-get update
sudo apt-get upgrade
Redis packages are available under the default apt repository. For the installation of Redis, run the below command from the terminal.
sudo apt-get install redis-server
Redis can be enabled to start on system boot by the below command like below
sudo systemctl enable redis-server.service
4.2 Installing Redis on Linux by building it from Source
Redis can as well be installed on a linux box by building it from source. To do so, the sources of the Redis could be downloaded from http://redis.io/download (please be sure to pick the stable release branch). Once the archive is on your machine, please follow the below following steps to install redis.
Download the source using the below terminal command
The downloaded zip can be unpacked using the below command
Once unpacked redis server needs to be built from the redis directory
Make the binaries by using the make command
or (in case you have Linux 32bit installed)
Run tests (you need
Tcl 8.5+ to be installed to run the tests)
root user or with
Post the installation, we need to create a configuration directory. We will use the conventional /etc/redis directory, which can be created by typing
sudo mkdir /etc/redis
Now, copy over the sample Redis configuration file included in the Redis source archive:
sudo cp /tmp/redis-5.0.7/redis.conf /etc/redis
It is important to mention that Redis does not have any special requirement or dependencies, either runtime or compile time, and is compatible with most Linux distributions. The only preinstalled packages you need are gcc and make.
By default, the aliases for Redis binaries will be created in
/usr/local/bin folder. For more insights, the
README file is a great place to look in for advanced details (like changing default installation folder, common build errors troubleshooting, etc).
4.3 Installing Redis on Windows
Redis team does not support official Windows distributions. Luckily, there is an experimental Windows 32/64 port supported by Microsoft Open Tech group and freely available on GitHub: https://github.com/MSOpenTech/redis. One important thing to take into account is that Windows port is always behind latest Redis releases and as such is not as a feature-rich as you may need. At the moment of writing, the latest version of Redis available for Windows was
- Clone the repository (or download it as a
ZIParchive from https://github.com/MSOpenTech/redis if you do not have
git clone https:
- For your convenience, there are pre-built binaries available in repository already. Just unpack them into convenient location.
- Windows 32 bit:
- Windows 64 bit:
- Windows 32 bit:
- However, if you want to, you can build Redis from sources as well. To do that, you need to have Microsoft Visual Studio 2010 or its free available version Microsoft Visual C++ 2010 Express Edition available on Microsoft Visual Studio web site. Once you have it, just open solution file from
msvs/RedisServer.slnand build it. The binaries will be available under
msvs/Release, depending on your build configuration and platform (32bit/64bit).
4.4 Validating the Installation
Once installation has been completed, your Linux box should have following executables located inside
Executable file name Description redis-benchmark Redis benchmarking tool, very useful to simulate running a set of commands by many clients in parallel so to assess your Redis instance configuration (more details at http://redis.io/topics/benchmarks) redis-check-aof * Verifies and fixes the corrupted append only log (AOF log) used by Redis to manage persistence (more details at http://redis.io/topics/persistence) redis-check-dump * Checks Redis database dump (RDB) file (more details at http://redis.io/topics/quickstart) redis-cli Command line interface utility to communicate with Redis server (more details at http://redis.io/topics/quickstart and in First look at Redis CLI section) redis-server Redis server (more details at http://redis.io/topics/quickstart)
* These tools are very useful if you need to recover corrupted data
Windows installation (either built from scratch or extracted from pre-built archive) consists of following executables mirroring the Linux ones:
It will save you a lot of time if the folder that contains those executables is appended to Windows
PATH environment variable.
4.5 Redis Configuration
4.5.1 Configuration options
Redis supports quite sophisticated configuration settings, including persistence, sharding, clustering, replication. Some configuration parameters require server to be restarted but some could be tweaked at runtime, using
But the good thing (for beginners) about Redis configuration is that there is no configuration at all. Redis could be started without single setting provided and will work perfectly fine.
Nevertheless, it would be very useful to glance over some key options. As an example, we will look at
redis.conf file from Redis distribution,
daemonize yes | no(default: no)
By default Redis does not run as a daemon. Use ‘yes’ if you need it. Note that Redis will write a pid file in
pidfile /var/run/redis.pid(default: /var/run/redis.pid)
When running daemonized, Redis writes a pid file in /var/run/redis.pid by default. You can specify a custom pid file location here.
port 6379(default: 6379)
Accept connections on the specified port, default is 6379. If port 0 is specified Redis will not listen on a TCP socket.
(default: commented out, all network interfaces)
bind 192.168.1.100 10.0.0.1
By default Redis listens for connections from all the network interfaces available on the server. It is possible to listen to just one or multiple interfaces using the “bind” configuration directive, followed by one or more IP addresses.
logfile /var/log/redis.log(default: “”)
Specify the log file name. Also the empty string can be used to force. Redis to log on the standard output. Note that if you use standard output for logging but daemonize, logs will be sent to /dev/null
databases 16(default: 16)
Set the number of databases. The default database is DB 0, you can select a different one on a per-connection basis using SELECT <dbid> where dbid is a number between 0 and ‘databases’-1
timeout 0(default: 0)
Close the connection after a client is idle for N seconds (0 to disable)
dbfilename dump.rdb(default: dump.rdb)
The filename where to dump the DB
dir /var/redis(default: ./)
The working directory. The DB will be written inside this directory, with the filename specified above using the ‘dbfilename’ configuration directive. The Append Only File will also be created inside this directory.
By default, redis is accessible from localhost but if you wish to access redis server from a remote location then we need to make some changes in the configuration file. Open the configuration file for the instance /etc/redis.conf & look for ‘bind 127.0.0.1’. We can either replace 127.0.0.1 with 0.0.0.0 or add IP address of our server to it. It should look like
bind 127.0.0.1 192.168.1.100
By and large, those are the most useful configuration settings to start from, continuously tuning your Redis server to get most of it.
For more details and information about various configuration parameters in redis.conf, please refer to the documentation.
4.5.2 Configuring Redis
Now that Redis is installed, we can begin to configure it by editing the redis.conf file to adjust a few items in the configuration:
sudo nano /etc/redis/redis.conf
In the file, the
supervised directive is currently set to
no. Since we are running redis on an operating system that uses the systemd init system, we can change this to
systemd like below.
. . .
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
. . .
Next, find the
dir directory. This option specifies the directory that Redis will use to dump persistent data. We need to pick a location that Redis will have write permission and that isn’t viewable by normal users.
We will use the
/var/lib/redis directory for this and that is mentioned in redis.conf like below
. . .
# The working directory.
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
# The Append Only File will also be created inside this directory.
# Note that you must specify a directory here, not a file name.
. . .
Save and close the file when you are finished.
5. Working with Redis Server
5.1 Starting/Stopping redis server
There are several ways to start your Redis server. The simplest one is just by running redis-server without specifying any configuration. Once it’s started, the full-functional Redis server is ready to handle requests, listening on default port
The picture below shows a typical output on a Linux console when Redis server starts successfully. The first line warns about absence of the configuration file so the default configuration is talking place. And this is yet another way Redis shines: keeping things as simple as possible makes it really easy to get started, adding more advanced configuration options along the way (when you really need it).
A slight variation of the previous flow includes passing configuration file and desired port to listen for incoming connections:
redis-server <conf> --port <port>
The port parameter if specified overrides the one from configuration file. The picture below demonstrates how Redis output may look like in this case.
Redis server started by any of those ways could be stopped by pressing
Advanced Linux users are familiar with init scripts and having Redis server started automatically once system is booted up is quite useful. For this purpose, Redis distribution includes startup script template at
utils/redis_init_script. This script could be used as-is and should be copied into standard
/etc/init.d folder. Please notice that by default startup script will try to look for configuration file at
/etc/redis/6379.conf (to get more details about these recommendations and conventions, please look through http://redis.io/topics/quickstart).
If you would like to start Redis this way (using init scripts), the
/etc/redis/6379.conf file should be modified a bit in order to have a couple of important configuration options set:
daemonizeshould be set to yes (by default it is set to no)
pidfileshould be set to /var/run/redis_6379.pid (which corresponds to the Redis instance port number and configuration file name convention)
logfileshould be set to /var/log/redis_6379.log (following the same conventions as
dirshould be set to /var/redis/6379 (following the same conventions as
Please refer to Basic configuration section in order to get more detailed explanation what those configuration options mean and their implications.
5.2 Enabling init system to manage redis server
Next, we can create a systemd unit file so that the init system can manage the Redis process.
Create and open the
/etc/systemd/system/redis.service file to get started:
sudo nano /etc/systemd/system/redis.service
Inside, we can begin the
[Unit] section by adding a description and defining a requirement that networking be available before starting this service:
Description=Redis In-Memory Data Store
[Service] section, we need to specify the service’s behavior. For security purposes, we should not run our service as
root. We can use a dedicated user and group, which we will call
redis for simplicity.
To start the service, we just need to call the
redis-server binary, pointed at our configuration. To stop it, we can use the Redis
shutdown command, which can be executed with the
redis-cli binary. Also, since we want Redis to recover from failures when possible, we will set the
Restart directive to “always”. Below is a sample /etc/systemd/system/redis.service file with the above settings
Description=Redis In-Memory Data Store
Finally, in the
[Install] section, we can define the systemd target that the service should attach to if enabled (configured to start at boot):
Description=Redis In-Memory Data Store
Save and close the file when you are finished.
6. Redis Client
The best and simplest way to explore Redis in full power is its command line interface,
redis-cli.exe on Windows). It’s super easy to use, plus it has brief help for every Redis command and supports navigation over command’s history (by using
redis-cli starts, it immediately tries to connect to Redis instance guessing it is running on local machine (
127.0.0.1) and default port (
6379). If it’s not the case, the tool says you that.
redis-cli could be used to connect to remote Redis instances when hostname and port are provided as command line arguments:
redis-cli -h hostname -p port
Assuming that our Redis server is running on local machine, let us run
redis-cli and issue our first command to be sure the server is ready to serve the requests.
PING command is the most straightforward, side-effects free way to force Redis server to send
PONG as a response, confirming it is running and ready. To get a bit more details about what
PING command is for,
HELP PING shows a quick summary of that.
Despite its simplicity,
redis-cli is extremely useful. It allows not only sending command to Redis server but also to change configuration, monitor current activity and much, much more.
Check that you can set keys by typing:
127.0.0.1:6379> set test "It's working!"
Now, retrieve the value by typing:
127.0.0.1:6379> get test
You should be able to retrieve the value we stored like below
Exit the Redis prompt to get back to the shell:
As a final test, let’s restart the Redis instance:
sudo systemctl restart redis
Now, connect with the client again and confirm that your test value is still available:
127.0.0.1:6379> get test
Back out into the shell again when you are finished:
6.1 Redis Java Client libraries
To work with Redis from a java application, it is recommended to use one of the open source, community supported client libraries and Jedis is one of them.
Jedis is a blazingly small and sane Redis java client. Jedis was conceived to be EASY to use. Jedis is fully compatible with redis 2.8.x, 3.x.x and above*. Jedis supports all of the redis features like Sorting, Connection handling, Commands operating on any kind of values, Commands operating on data structures (string values, hashes, lists, sets, sorted sets), Transactions, Pipelining, Publish/Subscribe, Persistence control commands, Remote server control commands, Connection pooling, Sharding (MD5, MurmurHash), Key-tags for sharding, Sharding with pipelining, Scripting with pipelining, Redis Cluster.
To use Jedis in your Java application, You can download the latest build at: http://github.com/xetorthio/jedis/releases
Or use it as a maven dependency:
For more information about Jedis library, please visit the github repository.
In this article, we have understood how to install redis using package manager or from the source in both linux and windows operating systems. We have also understood working with redis server by going through the significance of various parameters as part of redis configuration. This article serves to be a comprehensive tutorial to get started with redis. To understand more about the redis commands, please refer the official documentation. To understand more about how redis can be used in microservices architecture then please the article.