Redis database complete tutorial in [2022]

Redis is an open-source key-value data store that stores the data in the memory. In this tutorial, we will learn about the Redis database and also understand various aspects of the database in redis like database management, creation, etc. So let’s get started.

Redis download

If you are a beginner, then you can follow my tutorial about redis essential to get comfortable with basic redis commands.

There are many ways to download redis in your system, you can either install redis on a standalone VM or you can install redis to run on docker. Follow the below tutorials to download redis on your system as per your need.

NOTE: If you are a beginner and want to do some POC on redis, I will recommend installing redis using docker.

Install redis on docker

Install redis on centos7

Install redis on the other system

Redis database tutorial

In this session, we will learn all about the redis database and understand various aspects of database management. so let’s get started.

What is the redis database?

The database in redis is similar to the database in any RDBMS system. The database is used to hold single/multiple tables. In redis, we generally called databases keyspace.

In redis, the number of databases is fixed and by default, it is set to 16. you can check that configuration in redis.conf file.

cat redis.conf | grep databases
databases 16

You can check all the keyspace present in the redis database by typing the below command> info keyspace
# Keyspace

Since the redis is freshly installed, so there is no keyspace.

The default database for redis is db0 and since we have set up 16 as the maximum database that redis can hold, so the database name in redis will be like


Redis create database

By default when you insert data into the redis, it will insert the data into db0. Let’s try to insert a few data into the redis database> set my_key1 my_value1
OK> set my_key2 my_value2

Now type the below command to verify the database in which the redis data is saved> info keyspace
# Keyspace

Since we have inserted 2 records, so the records get inserted into the db0.

Redis create a new database

As discussed above, we can have 16 databases in redis ranging from db0 to db15 these names are predefined and a user can not change it. use the select command to connect to any db10 redis database> select 10

Now insert a few records into the redis database db10[10]> set my_key1 my_value1
OK[10]> set my_key2 my_value2

Verify the data which gets inserted into redis db10[10]> info keyspace
# Keyspace

Redis database login

By default when you connect to redis, it will get connected with the db0 database.

root@3b8a9eecac83:/data# redis-cli -h -p 6379> 

But if you wish to connect to any specific database of redis then you have to use the –n flag while connecting. for example: type the below command to connect to db10 of redis.

root@3b8a9eecac83:/data# redis-cli -h -p 6379 -n 10[10]>

In a similar way, you can connect to any other redis database.

The user will get the below error if the redis database does not exist.

redis-cli -h -p 6379 -n 20
SELECT 20 failed: ERR DB index is out of range

Redis move data from one database to another

In redis, we can also move data from one database to another. Redis provides swapdb command using which we can easily move the entire redis data.

Let’s verify the existing data in the redis databases.> info keyspace
# Keyspace

Now move the entire db10 data to db12 by typing the below command

swapdb 10 12

Again verify the redis data

info keyspace
# Keyspace

From the above output, it is clear that all data has been successfully migrated to the new database.


I hope you have liked this tutorial about redis databases. In this blog, we have learned how to insert data into specific redis databases and how to connect to any specific redis database. we have also learned how to transfer data from one database to another database.

Please do let me know if you face any issues while following along.

More to Read

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top