In a standard standalone Redis setup, you can select a database by its number using the
SELECT command. However, it's crucial to know that in a Redis Cluster, there is no concept of multiple databases like in a standalone Redis instance. A Redis Cluster strictly supports only database 0.
Here's an example of how you would normally switch databases in a standalone Redis setup:
However, this would not work in a Redis Cluster context. If you try executing the
SELECT command in a Redis Cluster, you will receive an error stating:
(error) ERR SELECT is not allowed in cluster mode.
This limitation comes from the architectural design of the Redis Cluster, which aims to partition data among multiple nodes and does not support multiple database contexts within those nodes. This simplification helps achieve better performance and consistency in a distributed setting.
If you need to partition your data in a Redis Cluster, consider using keys with prefixes or namespaces which can be used to effectively separate different types of data.
In this code snippet, we are using
namespace2 as logical partitions for our data. This way, although Redis Cluster doesn't support multiple databases, you can still manage your data in a structured and separated manner.