Does the Redis server emit a notice/warning when the client tries to connect to a redis database of an index higher than the max database index (redis_databases/dbnum)? Because apparently the connection then still succeeds, albeit with the highest possible database index. Can the client detect this database index "wrap-around" by the server?

Comment From: oranagra

which client do you use? can you give an exact example (configuration, input, and output)? you can observe the selected database number with CLIENT INFO

Comment From: strarsis

I am using the WP Redis plugin which itself uses the PhpRedis extension.

The configuration would be in this case a local Redis docker container:

$_SERVER['CACHE_HOST'] = '127.0.0.1';
$_SERVER['CACHE_PASSWORD'] = 'test';
$_SERVER['CACHE_DB'] = 17; // index (0-based!) higher than the default max 16

When connecting with this configuration, the connection will succeed, but database 16 (15 with 0-based index) is used instead. Without salting this causes unexpected cache collisions.

Comment From: ptjm

The short answer is "yes". redis-cli reports an error, but connects to database 0:

$ redis-cli -n 50                  
SELECT 50 failed: ERR DB index is out of range
127.0.0.1:6379> 

redis-py throws an exception

>>> import redis
>>> rds = redis.Redis(db=50,single_connection_client=True)
Traceback (most recent call last):
[...]
redis.exceptions.ResponseError: DB index is out of range

For this to happen, the server must be returning "-DB index is out of range". To connect to the largest-numbered db rather than db 0, your client must be going out of its way.