How to decrease the default number of Database in Redis ?

The default number of Databases in Redis is 16. I know that we can change this using the redis.conf , whose location I checked using info-server command on redis-cli . But When, I am trying to decrease it to 2, its not picking up the change, and , CONFIG GET databases , still gives me 16. So, what is the way to decrease the default number of databases in Redis ?


To change the number of databases:

  1. edit redis.conf
  2. change databases value to 14 (if you want to decrease by 2)
  3. restart redis service, that depends on your operating system and Redis installation, for mine, Ubuntu, I have to run from command line sudo systemctl restart redis

So probably you forgot step 3, if you don't restart Redis service, configuration changes will be not available

Btw, AFAIK you don't distribute your database in 16 parts as you say, if you don't use from 1 to 15 databases, they won't use RAM, you just consume the RAM you need

Update

First of all, is very strange you cannot reduce the number of databases to 2, do you have already some data on databases from 2-15? (remember the first database is 0) Did you save redis.conf file after change database pram = 2? Are you sure you are using the right command to restart Redis service?

Regarding how to confirm redis reserves RAM for unused database, it is something you can deduce from the nature of in-memory redis is and taking a look simply in the definition of select command:

https://redis.io/commands/select

Redis different selectable databases are a form of namespacing: all the databases are anyway persisted together in the same RDB / AOF file. However different databases can have keys having the same name,

So basically Redis uses a kinda of key namespaces which are added to the name of the key to difference keys belonging to different databases, and since Redis just put in memory data when they are saved there is no a previous reservation of memory for empty databases. Probably you are a little bit confusing with other databases manage memory or hard disk when you create a database

Anyway if you are worried about memory leaks, take a look at maxmemory and maxmemory-policy here is this article

http://oldblog.antirez.com/post/redis-as-LRU-cache.html

Also, if you are using Redis v4.0.0+ you can be interested in memory doctor and latency doctor command but you will need some data in your Redis to get some useful info. You can also use this Python console tool to get key usage memory stats:

https://github.com/gamenet/redis-memory-analyzer

Other useful links to understand how Redis manages memory and operate:

https://redis.io/topics/faq

https://redis.io/topics/memory-optimization