Redis Tutorial

In recent years, the Redis database system has become increasingly popular. Its primary advantages are its speed and simple structure. Data is written directly to the server’s memory and can thus be retrieved much faster than with other databases. Therefore, Redis is often used as a cache on the internet. Even messenger services use this database to enable high-speed communication. Read on to find out how to set up your own Redis database.

Step 1: installing Redis

Redis is open source and can thus be downloaded, used and modified by anyone free of charge.


In the following, we will describe how to install and set up Redis on Ubuntu. However, it can also be used on Mac and Windows. If you want to try Redis without installing it on your computer, we recommend the online testing environment Try Redis.

The first step is to download Redis. For this, you will be using the Ubuntu package manager which you will need to update first.

sudo apt-get update
sudo apt-get install redis-server

Alternatively, the files can be downloaded from the official Redis website. You will then need to unpack the package manually before installing it. Afterwards, you will launch Redis using a simple command.


To test whether Redis is running correctly, you can use the interface used to communicate with Redis. First, you start it up with the following command.


The interface will show the IP address and the port Redis is running on. You can now ping it.> ping

Redis responds which indicates that the database system was successfully installed. In addition, you can test whether you can also set content.> set test "OK!"> get test

Are you looking to install Redis on your own server? The IONOS cloud servers can be scaled for small projects.

Step 2: configuring Redis

Redis is initially installed with its standard configuration. This can be displayed using the following command.> config get *

In the list of all the configuration parameters, pairs are always listed one after the other. Accordingly, the value “dump.rdb” corresponds to the parameter “dbfilename”. The asterisk used to retrieve the list is just a placeholder for a specific configuration parameter. If you only want to check a specific parameter, you can replace the asterisk with the name of the parameter. This is always the upper part of the configuration pair - the key for the corresponding value.> config get dbfilename
1) "dbfilename"
2) "dump.rdb"

To change an entry in the configuration file, you use the set command. For example, this can be used to set a password.> config set requirepass "password"

If you want to request the password with get, you will be asked to enter it first, since you just set a security check for Redis. To do so, use the command auth. Then, you can request the configuration entry as described below.> auth "password"> config get requirepass
1) "requirepass"
2) "password"

There are additional ways to secure your database. You can find an overview of these options on the official website, which has been compiled by the developers.

Redis stores all datasets in its memory. However, to ensure data consistency, you can create a snapshot of the database on your hard drive which is saved in the dump.rdb file.> save

Using the save command, you can manually create a snapshot. However, these backups can also be automated.> save 60 10

In this Redis example, we gave the command two parameters. Now, it will save every 60 seconds if there have been 10 modifications during this time.

However, the save command is not so well suited to be used actively because it prevents clients from being able to access the database. It would be better to use bgsave, since this process runs in the background.

In addition to snapshot mode, there is also the Append Only File (AOF) mode. In this mode, Redis saves every operation you have done in a file. Therefore, if the Redis server unexpectedly crashes, you can see the last operation performed. To activate the AOF mode, you have to make a modification to the configuration file.> config set appendonly yes

For maximum data security, you should regularly create snapshots and activate the AOF mode. By doing so, it is practically impossible to lose any data. However, these processes do tend to make the database run more slowly.

Step 3: creating entries

After configuring Redis, you can work with the database. This is done using different data types and commands.


Strings are the easiest to set. To do so, simply use the set command.


You are not required to use quotation marks when entering values. To make the code more readable, you can put text in quotation marks and enter numerical values without them.> set foo "bar"> set value 1

Now, if you request the entries for foo and value with the get command, the corresponding values will be displayed.> get foo
"bar"> get value

To delete an entry, use the del command.> del foo
(integer) 1> get foo

If you do not wish to use an extra line of code to create multiple entries, you can use the extended function mset. You can also retrieve the values of several entries at once using mget.> mset foo1 "bar1" foo2 "bar2" foo3 "bar3"
OK> mget foo1 foo2 foo3
1) "bar1"
2) "bar2"
3) "bar3"


Redis also allows you to use other data types. For example, lists and sets are popular when working with the database. Both of these are collections of values. While sets are unsorted, the values in lists are numbered. You can add, request and delete entries in lists.> lpush mylist foo
(integer) 1> lpush mylist bar
(integer) 2> lrange mylist 0 10
1) "foo"
2) "bar"> linsert mylist before "bar" "test"
(integer) 3> lrange mylist 0 10
1) "foo"
2) "test"
3) "bar"> lrem mylist 0 foo
(integer) 1> lrange mylist 0 10
1) "test"
2) "bar"

In this Redis example, we first added two elements to a list (lpush) and had them displayed. Using the command lrange, you can define the range to be displayed (0 to 10 here). Negative numbers can also be used in the range. Then, we added another value before an existing one using linsert (also possible to insert it after) and thus changed the numbering. You can use the lrem command to delete specific entries from a list.

IONOS Cloud Compute Engine

Medium-sized and large companies choose the cloud from Germany. IaaS and PaaS are services for champions.

  • Secure
  • Reliable
  • Flexible


Redis has other commands for sets, but they produce similar results.> sadd myset "foo"
(integer) 1> sadd myset "bar"
(integer) 1> smembers myset
1) "bar"
2) "foo"> sismember myset "bar"
(integer) 1> srem myset "bar"
(integer) 1> smembers myset
1) "foo"

Using sadd, you can add multiple items to a set at the same time by adding them one after the other to the command. To display a set, all you need is the command smembers and the name of the desired set. Using the sismember command, you can also search for a specific entry. Similar to the list, you can delete individual entries using srem.

Redis also enables users to use sets in a sorted format.> zadd mysortedset 1 "foo"
(integer) 1> zadd mysortedset 2 "bar"
(integer) 1> zadd mysortedset 2 "foobar"
(integer) 1> zrange mysortedset 0 10
1) "foo"
2) "bar"
3) "foobar"

To add items to this data type, use the command zadd and a score. While the values themselves cannot occur more than once, you can enter the same number multiple times for the score. The score is thus not a direct numbering in the set but a ranking. All entries with a score of 2 come after items with a score of 1. You can display all or a selection of entries using zrange.


Hashes are a special data type. Similar to sets and lists, hashes are single entries that can contain multiple values. However, in terms of key-value pairs, there is also a key for each value.> hset user1 name "bob" email "" password "rK87_x"
OK> hget user1 name
1) "bob"> hgetall user1
1) "name"
2) "bob"
3) "email"
4) ""
5) "password"
6) "rK87_x"> hvals user1
1) "bob"
2) ""
3) "rK87_x"> hkeys user1
1) "name"
2) "email"
3) "password"
> hdel user1 password
(integer) 1> hgetall user1
1) "name"
2) "bob"
3) "email"
4) ""> del user1
(integer) 1> hgetall user1
(empty list or set)

In this Redis example, we used hset to create a hash and named it user1. The hash has three fields. Using the command hget, you can request the value of each field individually. To display all of them at the same time, use hgetall. Additional request options include hvals (displays all values stored in a hash) and hkeys (displays all keys stored in a hash). You can delete individual fields using hdel. To delete the entire hash, you can do so as usual using del.


The command flushall deletes all entries in a database.

Step 4: additional options

Naturally, you can do more with Redis than just add entries to a database. You can also give the data specific properties. For example, incremental increases and decreases can be quite useful.> set foo 1
OK> get foo
"1"> incr foo
(integer) 2> incr foo
(integer) 3> get foo
"3"> decr foo
(integer) 2> get foo

Using these functions, you can increase or decrease values by one. Sometimes, you may want to set values that will only be available in the database for a specific period of time. To do so, use the function expire.> set foo "bar"
OK> expire foo 100
(integer) 1> ttl foo
(integer) 50> ttl foo
(integer) -50> get foo

The expire command requires the period of time to be in seconds. So, in this Redis example, we set the expire time to 100 seconds. At the halfway point, we requested the time to live (i.e. the remaining time) using the command ttl. If you wait any longer than that, the TTL will go into the negative range. At that point, the entry will no longer exist.

Using the command setex, you can link a database entry directly to a TTL when it is created.> setex foo 100 "bar"

You can also expand upon an entry after it has been created. Using the command append, you can add another value to the stored value.> set foo "Hello"
OK> append foo " World"
(integer) 11> get foo
"Hello World"> set bar 5
OK> append bar 10
(integer) 3> get bar

As you will see when you request the corresponding values again, the new components are simply added to the existing value. If the entry does not exist yet, append functions just like set.

In addition, entries can also be renamed using the command rename.> set foo 100
OK> rename foo bar
OK> get foo
(nil)> get bar

There are many more commands you will need to be able to properly work with Redis. You can find all available commands including descriptions of them in the official documentation.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top