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

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

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

image

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..

Index
  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.

Note

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:

  redis-server  

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

  redis-cli  

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

  127.0.0.1:6397> ping PONG  

If Redis responds, the database system has been successfully installed . Now you can also check if you can write text.

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

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.

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

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

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

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

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 .

  127.0.0.1:6397> save  

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

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

  127.0.0.1:6397> config set appendonly yes  
advice

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.

Strings

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

Note

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.

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

If the foo and value inputs are now requested by 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"  

The command to delete an entry is del .

  127.0.0.1:6397> del foo (integer) 1 127.0.0.1:6397> 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 .

  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

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.

  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 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.

Sets

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

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.

  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 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.

Hashes

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. .

  127.0.0.1:6397> hset user1 name "bob" email "[email protected]" 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) "[email protected]" 5) "password" 6) "rK87_x" 127.0.0.1:6397> hvals user1 1) "bob" 2) "[email protected]" 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) "[email protected]" 127.0.0.1:6397> del user1 (integer) 1 127.0.0.1:6397> 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 .

Note

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 .

  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"  

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 .

  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 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.

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

  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 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 .

  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"  
advice

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


...