Skip to content

Multple persistent connections to the same Redis host with different databases: database number changes #1920

@binary-data

Description

@binary-data

Expected behaviour

Redis documentation for the SELECT command says that

New connections always use the database 0

So subsequent SELECT commands on different connections should not change the database number for other connections, even if the database 0 was not explicitly selected.

And if the database for connection was changed for any reason, Redis::getDbNum() method should return database number being used.

Actual behaviour

I have the following script which runs in PHP-FPM:

<?php

$redis0 = new Redis();
$redis0->pconnect('redis', 6379, 0.0, 'redis0');
# No explicit select 0 here

$redis1 = new Redis();
$redis1->pconnect('redis', 6379, 0.0, 'redis1');
$redis1->select(1);

$redis2 = new Redis();
$redis2->pconnect('redis', 6379, 0.0, 'redis2');
$redis2->select(2);

echo $redis0->getDbNum();
$redis0->set('REDIS_0', '0');

echo $redis1->getDbNum();
$redis1->set('REDIS_1', '1');

echo $redis2->getDbNum();
$redis2->set('REDIS_2', '2');

On the first and second requests the keys appear in correct databases (MONITOR output from redis-cli):

# Request 1
1611942079.015152 [0 172.18.0.9:45512] "SELECT" "1"
1611942079.016807 [0 172.18.0.9:45514] "SELECT" "2"
1611942079.017168 [0 172.18.0.9:45510] "SET" "REDIS_0" "0"
1611942079.017464 [1 172.18.0.9:45512] "SET" "REDIS_1" "1"
1611942079.017689 [2 172.18.0.9:45514] "SET" "REDIS_2" "2"

1611942219.691163 [2 172.18.0.1:47622] "flushall" # flushall from redis-cli

# Request 1
1611942223.955243 [0 172.18.0.9:45592] "SELECT" "1"
1611942223.955885 [0 172.18.0.9:45594] "SELECT" "2"
1611942223.956078 [0 172.18.0.9:45590] "SET" "REDIS_0" "0"
1611942223.956166 [1 172.18.0.9:45592] "SET" "REDIS_1" "1"
1611942223.956259 [2 172.18.0.9:45594] "SET" "REDIS_2" "2"

Checking the keys in redis-cli:

127.0.0.1:63799> keys *
1) "REDIS_0"
127.0.0.1:63799> select 1
OK
127.0.0.1:63799[1]> keys *
1) "REDIS_1"
127.0.0.1:63799[1]> select 2
OK
127.0.0.1:63799[2]> keys *
1) "REDIS_2"

However, on the third request (without flushall between requests) the "REDIS_0" key appears in the database 2:

1611942329.811933 [2 172.18.0.9:45514] "ECHO" "phpredis:601449b9c6073:1867ae6f"
1611942329.812692 [1 172.18.0.9:45512] "ECHO" "phpredis:601449b9c6586:3eac0768"
1611942329.813386 [1 172.18.0.9:45512] "SELECT" "1"
1611942329.813804 [0 172.18.0.9:45510] "ECHO" "phpredis:601449b9c6a0c:1bb3c7f6"
1611942329.814144 [0 172.18.0.9:45510] "SELECT" "2"
1611942329.814439 [2 172.18.0.9:45514] "SET" "REDIS_0" "0"
1611942329.814647 [1 172.18.0.9:45512] "SET" "REDIS_1" "1"
1611942329.814945 [2 172.18.0.9:45510] "SET" "REDIS_2" "2"

Checking the keys:

127.0.0.1:63799> keys *
1) "REDIS_0"
127.0.0.1:63799> select 1
OK
127.0.0.1:63799[1]> keys *
1) "REDIS_1"
127.0.0.1:63799[1]> select 2
OK
127.0.0.1:63799[2]> keys *
1) "REDIS_2"
2) "REDIS_0"

Meanwhile, during all the requests $redisN->getDbNum(); outputs correct database numbers: 0 1 2.

I'm seeing this behaviour on

  • OS: Linux 5.10.9-201.fc33.x86_64 x86_64 (redis docker image)
  • PHP: 7.4.13
  • Redis: 6.0.8
  • phpredis: 5.3.2

I've checked

  • There is no similar issue from other users
  • Issue isn't fixed in develop branch - not sure how do I check that

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions