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.

Managed Databases
Time-saving database services
  • Enterprise-grade architecture managed by experts
  • Flexible solutions tailored to your requirements
  • Hosted in the UK under strict data protection legislation

Step 1: installing Redis

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

Note

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.

redis-server

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.

redis-cli

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

127.0.0.1:6397> ping
PONG

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

127.0.0.1:6397> set test "OK!"
127.0.0.1:6397> get test
"OK!"
Note

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.

127.0.0.1:6397> 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.

127.0.0.1:6397> 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.

127.0.0.1:6397> config set requirepass "password"
OK

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.

127.0.0.1:6397> auth "password"
127.0.0.1:6397> config get requirepass
1) "requirepass"
2) "password"
Note

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.

127.0.0.1:6397> save

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

127.0.0.1:6397> 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.

127.0.0.1:6397> config set appendonly yes
Tip

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

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

Note

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.

127.0.0.1:6397> set foo "bar"
127.0.0.1:6397> set value 1

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

127.0.0.1:6397> get foo
"bar"
127.0.0.1:6397> get value
"1"

To delete an entry, use the del command.

127.0.0.1:6397> del foo
(integer) 1
127.0.0.1:6397> get foo
(nil)

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.

127.0.0.1:6397> mset foo1 "bar1" foo2 "bar2" foo3 "bar3"
OK
127.0.0.1:6397> mget foo1 foo2 foo3
1) "bar1"
2) "bar2"
3) "bar3"

Lists

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.

127.0.0.1:6397> lpush mylist foo
(integer) 1
127.0.0.1:6397> lpush mylist bar
(integer) 2
127.0.0.1:6397> lrange mylist 0 10
1) "foo"
2) "bar"
127.0.0.1:6397> linsert mylist before "bar" "test"
(integer) 3
127.0.0.1:6397> lrange mylist 0 10
1) "foo"
2) "test"
3) "bar"
127.0.0.1:6397> lrem mylist 0 foo
(integer) 1
127.0.0.1:6397> 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.

Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flexibility with no minimum contract
  • 24/7 expert support included

Sets

Redis has other commands for sets, but they produce similar results.

127.0.0.1:6397> sadd myset "foo"
(integer) 1
127.0.0.1:6397> sadd myset "bar"
(integer) 1
127.0.0.1:6397> smembers myset
1) "bar"
2) "foo"
127.0.0.1:6397> sismember myset "bar"
(integer) 1
127.0.0.1:6397> srem myset "bar"
(integer) 1
127.0.0.1:6397> 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.

127.0.0.1:6397> zadd mysortedset 1 "foo"
(integer) 1
127.0.0.1:6397> zadd mysortedset 2 "bar"
(integer) 1
127.0.0.1:6397> zadd mysortedset 2 "foobar"
(integer) 1
127.0.0.1:6397> 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

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.

127.0.0.1:6397> hset user1 name "bob" email "bob@example.com" password "rK87_x"
OK
127.0.0.1:6397> hget user1 name
1) "bob"
127.0.0.1:6397> hgetall user1
1) "name"
2) "bob"
3) "email"
4) "bob@example.com"
5) "password"
6) "rK87_x"
127.0.0.1:6397> hvals user1
1) "bob"
2) "bob@example.com"
3) "rK87_x"
127.0.0.1:6397> hkeys user1
1) "name"
2) "email"
3) "password"
> hdel user1 password
(integer) 1
127.0.0.1:6397> hgetall user1
1) "name"
2) "bob"
3) "email"
4) "bob@example.com"
127.0.0.1:6397> del user1
(integer) 1
127.0.0.1:6397> 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.

Note

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.

127.0.0.1:6397> set foo 1
OK
127.0.0.1:6397> get foo
"1"
127.0.0.1:6397> incr foo
(integer) 2
127.0.0.1:6397> incr foo
(integer) 3
127.0.0.1:6397> get foo
"3"
127.0.0.1:6397> decr foo
(integer) 2
127.0.0.1:6397> get foo
"2"

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.

127.0.0.1:6397> set foo "bar"
OK
127.0.0.1:6397> expire foo 100
(integer) 1
127.0.0.1:6397> ttl foo
(integer) 50
127.0.0.1:6397> ttl foo
(integer) -50
127.0.0.1:6397> get foo
(nil)

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.

127.0.0.1:6397> setex foo 100 "bar"
OK

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.

127.0.0.1:6397> set foo "Hello"
OK
127.0.0.1:6397> append foo " World"
(integer) 11
127.0.0.1:6397> get foo
"Hello World"
127.0.0.1:6397> set bar 5
OK
127.0.0.1:6397> append bar 10
(integer) 3
127.0.0.1:6397> get bar
"510"

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.

127.0.0.1:6397> set foo 100
OK
127.0.0.1:6397> rename foo bar
OK
127.0.0.1:6397> get foo
(nil)
127.0.0.1:6397> get bar
"100"
Tip

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.

Was this article helpful?
Page top