others-When to spread redis keys with redis databases?

1. Purpose

In this post, I would demonstrate when and how to use redis databases to store your redis keys?



2. The solution

2.1 What is redis databases?

Out of the box, every Redis instance supports 16 databases. The database index is the number you see at the end of a Redis URL: redis://localhost:6379/0 . The default database is 0 but you can change that to any number from 0-15 (and you can configure Redis to support more databases, look in redis

Whether it is a string key, a hash key, or a list key, it will be stored in a container called a database. Because Redis is a key-value pair database server, its database is the same as the hash key introduced before, and the key-value pairs in the database can be indexed according to the name of the key. For example, by using the commands provided by Redis, we can remove the specified key from the database, or move the specified key from one database to another, and so on.

By default, redis support the following redis database operations:

●Specify the database they want to use.

●Obtain all the keys contained in the database at one time, iteratively obtain all the keys contained in the database, or randomly obtain a key in the database.

● Sort according to the value of a given key.

● Check the given one or more keys to see if they exist in the database.

● View the type of a given key.

●Rename the given key.

●Remove the specified key, or move it from one database to another.

●Clear all the keys contained in the database.

● Exchange the given two databases.



2.2 Do we need to store keys among different redis databases?

I don’t think so, we can just use the key pattern [KEY_TAG1]:[KEY_TAG2]…:[REAL_KEY] to store keys, just as follows:

users:user_sessions:user_1234

I collected some quotes about why there exists multiple redis databases :

itamarhaber commented on Aug 23, 2020

As far as I can tell this was done because of two main reasons:

1. The concept of shared/numbered/logical Redis databases was deemed long ago as an anti-pattern in Redis. The general thinking was to deprecate it, so adding it to the cluster (v3) wasn't one of the goals.
2. That said, supporting databases in the cluster would've (probably) made the implementation more complex with all the associated downsides of that.

And this:

Even Salvatore Sanfilippo (creator of Redis) thinks it's a bad idea to use multiple DBs in Redis. See his comment here:

https://groups.google.com/d/topic/redis-db/vS5wX8X4Cjg/discussion

> I understand how this can be useful, but unfortunately I consider Redis multiple database errors my worst decision in Redis design at all... without any kind of real gain, it makes the internals a lot more complex. The reality is that databases don't scale well for a number of reason, like active expire of keys and VM. If the DB selection can be performed with a string I can see this feature being used as a scalable O(1) dictionary layer, that instead it is not.
>
> With DB numbers, with a default of a few DBs, we are communication better what this feature is and how can be used I think. I hope that at some point we can drop the multiple DBs support at all, but I think it is probably too late as there is a number of people relying on this feature for their work.


Conclusion: Multiple databases may be discouraged, and unsupported in the redis-cluster, but they are not deprecated.


3. Summary

In this post, I demonstrated how to understand redis databases, I don’t think it’s necessary for all of us . That’s it, thanks for your reading.