+5 votes
in Technical issues by (242k points)
Redis tutorial: introduction to the in-memory database

1 Answer

+3 votes
by (1.6m points)
Best answer

Step 1: install Redis
Step 2: configure Redis
Step 3: create tickets
Step 4: other options


Redis tutorial: introduction to the in-memory database

The Redis database system has been gaining in popularity in recent years. Its main attractions are the speed and simplicity of the database structure: the data is entered directly into the main memory of the server, so that it can be retrieved much faster than in other databases. This makes it often used as a web page cache, although it is also used by messaging services for faster communication. In our Redis tutorial, you will learn how to install your own Redis database..

  1. Step 1: install Redis
  2. Step 2: configure Redis
  3. Step 3: create tickets
    1. Strings
    2. Lists
    3. Sets
    4. Hashes
  4. Step 4: other options

Step 1: install Redis

Redis is open source , that is, open source, so anyone can download, use and edit the system.


We will describe how to install and configure Redis on Ubuntu below, but it can also be done on Mac and Windows. If at the moment you just want to try Redis without installing it on your computer, we recommend the Try Redis online test environment..

The first step is to download Redis . To do this, we use the Ubuntu package manager, which we will have to update to the most recent version first.

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

Another way to do it is to download the files from the official Redis website. Following this path, however, you have to extract the package files manually, then install them, and finally start Redis:


To check if Redis works correctly, we start the communication interface with the database:


The interface should then display the IP address and port through which Redis is running, to which a test ping can be sent ..> ping PONG  

If Redis responds, the database system has been successfully installed . Now you can also check if you can write text.> set test "OK!"> get test "OK!"  

Do you want to install Redis on your own server? IONOS cloud servers can also be scaled for small projects.

Step 2: configure Redis

Redis is initially installed with the standard configuration , which can then be modified with the corresponding commands.> config get *  

In the list of settings for the configuration, the pairs of elements are broken down into two positions , one below the other: the dbfilename element then has the value dump.rdb . The asterisk that we have used to open the list acts as a placeholder for a specific setting in the list. When only one setting is to be examined, the asterisk is replaced by the name of the element, always using the one in the first position, which is the key for the corresponding setting value.> config get dbfilename 1) "dbfilename" 2) "dump.rdb"  

To change an entry in the configuration file, use the set command . It can be used, for example, to define a password:> config set requirepass "password" OK  

After doing so, if we request the password with the get command , we will be asked to enter it first, after all, we have given it one for something. To enter it, we use the auth command , and then we query the entry in the configuration file as we just did.> auth "password"> config get requirepass 1) "requirepass" 2) "password"  

There are more ways to make your database more secure. On the official Redis website, the developers summarize various points in this regard.

Actually, Redis saves all the data in main memory. With this in mind, to achieve data persistence, a copy ( snapchot ) of the database can be stored on the hard disk , which will be located in the dump.rdb file .> save  

With the save command , a copy is created manually, but it can also be scheduled to be done automatically.> save 60 10  

In this example, we have assigned two parameters to the command: now, a copy will be created every 60 seconds if there have already been 10 changes in that time interval.

However, the save command is not recommended while the system is running, as it prevents clients from accessing the database. In these cases , bgsave is more convenient , which performs the process in the background.

In addition to the possibility of making a copy of the database, there is also the append only file mode , in which Redis saves each action performed in a file. Thanks to this file, if the server crashed unexpectedly, you could find out what was the last thing done. To activate AOF mode, the configuration file must be modified.> config set appendonly yes  

If your data requires maximum security, you should activate the AOF mode and, in addition, make copies regular database: this way it will be practically impossible for you to lose your data. These processes, however, slow down the performance of the database somewhat.

Step 3: create tickets

Once you have configured Redis, you are ready to work with the database. You have several different types of data and commands available for this.


The easiest thing is to create a string , that is, a chain or sequence of elements. To do this, use the set command .


It does not matter whether the values ​​are enclosed in quotes or not. To make the code more readable, you can put the text in quotes and the numeric values ​​without them.> set foo "bar"> set value 1  

If the foo and value inputs are now requested by the get command , the corresponding values ​​will be displayed.> get foo "bar"> get value "1"  

The command to delete an entry is del .> del foo (integer) 1> get foo (nil)  

If you don't want to create many entries using a new row each time, you can use the advanced mset function . To request the values ​​of several fields at the same time, there is also the mget command .> mset foo1 "bar1" foo2 "bar2" foo3 "bar3" OK> mget foo1 foo2 foo3 1) "bar1" 2) "bar2" 3) "bar3"  


With Redis you can also use other types of data. Some of the most popular for working with the database are, for example, lists and sets . They are both sets of values, but while the sets do not have a specific order, the values ​​in the lists are numbered. Entries can be added, requested or deleted in a list.> 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 example we have first added two items to a list ( lpush ) and then requested that they be displayed. The lrange command indicates which segment should be displayed (here from 0 to 10, but negative numbers can also be used). Then, using the linsert command we have added a new value in front of an existing one ( after could also be used ), with which we have changed the numbering. The lrem command allows you to delete entries with a specific value from the list.


For sets , Redis uses other commands, but with very 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"  

With the sadd command , several elements can also be integrated into the set by entering them one after the other in the command. To view the set , just use the smembers command and the name of the set in question. The sismember command also allows you to search for a specific entry. Analogous to the list, with srem you can delete individual entries.

However, Redis also offers users the ability to use sets in a neat 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 used in this case, the command Zadd and score or score. While the values ​​themselves cannot appear more than once, with a score the same value can be indicated several times. The score is not, therefore, a direct numbering within the set , but rather a weighting, so that all entries with score or score 2 will appear after those with score 1 . With the zrange command you can display all the elements or those that are selected.


A special type of data is hashes : individual entries made up of several values, similar to sets and lists, but in which each value is accompanied by a key, thus forming so-called key-value or key-value pairs. .> hset user1 name "bob" email "[email protected]" password "rK87_x" OK> hget user1 name 1) "bob"> hgetall user1 1) "name" 2) "bob" 3) "email" 4) "[email protected]" 5) "password" 6) "rK87_x"> hvals user1 1) "bob" 2) "[email protected]" 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) "[email protected]"> del user1 (integer) 1> hgetall user1 (empty list or set)  

In this example, we have used hset to create a hash with the name user1 and three fields . Through the hget command we can request the value of each field. To show all of them, you can use hgetall . Other options for displaying values ​​are hvals (shows all values ​​stored in the hash ) and hkeys (displays all keys stored in the hash ). With hdel you can delete individual values, while with del , as we have already seen, the entire hash is deleted .


The flushall command is used to erase all entries from the database.

Step 4: other options

Naturally, with Redis, not only can you create entries in a database, but you can also assign specific properties to the data. In this sense, for example, the increase and decrease commands can be very 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 "2"  

With the help of these functions, the values ​​can be increased or decreased by one unit. Sometimes, instead, you want to enter values ​​that only remain in the database for a certain time: for this there is the expire function .> set foo "bar" OK> expire foo 100 (integer) 1> ttl foo (integer) 50> ttl foo (integer) -50> get foo (nil)  

The expire command requires a timestamp in seconds. In this example, we have decided that the entry should be 100 seconds long. After half the time has elapsed, we have used the ttl command to request the time-to-live , that is, the remaining time. If we wait even longer, the TTL will go negative from the moment the entry has already disappeared.

The setex command allows you to assign a TTL to a database entry since its creation.> setex foo 100 "bar" OK  

Once an entry has been created, it can be expanded: the append command adds another value to the one that already exists.> set foo "Hello" OK> append foo " World" (integer) 11> get foo "Hello World"> set bar 5 OK> append bar 10 (integer) 3> get bar "510"  

As you can see, when requesting the corresponding values, the new elements appear simply after those that already were . If the input in question does not yet exist, append then performs the same function as set .

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

There are many other commands to work with Redis. Details of all available commands can be found in the official description.