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.
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.
Redis responds which indicates that the database system was successfully installed. In addition, you can test whether you can also set content.
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.
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.
To change an entry in the configuration file, you use the set command. For example, this can be used to set a 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.
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.
Using the save command, you can manually create a snapshot. However, these backups can also be automated.
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.
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.
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.
Now, if you request the entries for foo and value with the get command, the corresponding values will be displayed.
To delete an entry, use the del command.
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.
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.
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.
- 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.
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.
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.
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.
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.
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.
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.
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.
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.