How to set the number of databases in redis

Time:2022-5-6

Redis sets the number of databases

Today, even the company’s database found that there are 255 dB. I don’t know why. In fact, there are 256 dB, starting from 0

在这里插入图片描述

And I have only 16 of my own

在这里插入图片描述

I’m from windows, and Linux is the same. The directory structure is shown in the figure below:

在这里插入图片描述

Edit redis Conf configuration file, search database and find:

databases 16

As shown in the figure:

在这里插入图片描述

By looking at the brief description, we know that the last sentence means that the initial DB is 0 (the default is 16). You can choose a number as the size of the database, but it can’t be 0. That’s about it… We can modify 16 to achieve what you want = – =

Note: the specified configuration file is required for startup. You can start redis server from the command line and under the current folder exe ./ redis. conf;

I wrote a bat file: start redis server exe redis. Conf is still in the current file

Analysis of redis multi database configuration items

When I read the redis configuration file, I came across databases 16. Then I opened the search introduction door of Baidu and Google. The explanations I learned from reading the article are as follows.

Official explanation: the number of available databases. The default value is 16 and the default database is 0.

At first, I didn’t understand the number of available databases. Isn’t an instance of redis a database. How can I configure multiple databases. This is because in redis, the database is identified by an integer index, not by a database name. It can be understood that configuring different databases takes up different memory space. Different databases do not affect each other and work independently, but they all rely on the living space on this redis instance.

The default total number of databases in redis’s configuration file is 16. By default, it is an array with subscripts from 0 to 15:

databases 16

database.png

Application scenario

Why does redis have such a design scenario? I think it is a concept similar to multi tenant. Just like virtualization and containers, you can virtualize multiple machines and containers, make full use of the hardware configuration of physical machines to work, and achieve the maximum utilization of server performance. Each virtual machine and container run separately, complementary influence and interference. I think the same is true for redis’s multi database.

Scenario:

redisdatabase.png

You may want a redis application (a redis server or a redis server / slave group) to serve multiple client applications. If these client applications are operated separately and write data to redis, it is likely to lead to key conflict (we know that redis is a storage structure with key value structure). In order to separate different applications, you can use different prefixes to distinguish (eg: app_i: XX: YY, app_ii: XX: YY). At this time, you can directly use redis’s mechanism to split these keys: the concept of database.

Each database has its own space, so you don’t have to worry about key conflicts.

In different databases, the same key gets its own value.

flushdb.png

The flush DB command clears the data, which will only clear the data under the current database and will not affect other databases. The flush command will clear the data of this instance. Before executing this command, you should consider clearly. I’m just an experimental environment here. It doesn’t matter.

The number of databases can be configured. By default, it is 16. Modify redis Databases instruction under conf:

databases 64

Redis does not provide any method to associate and identify different databases. Therefore, you need to track what data is stored in which database or what business data is stored in which database. I think zookeeper can be used to solve this problem.

summary

Redis databases, from the perspective of “all client applications are operated separately” and business separation, the concept of database is very applicable. It allows us to have a clear data division and can safely focus on the design of key.

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer.