Repository navigation
Description
stackexchange-redis-multidb-reproduction.zip
Summary
StackExchange.Redis does not properly support multi-database (db 0-4) functionality when connecting to Valkey/Redis 9.0+ cluster with cluster-databases: 5. The GetDatabase(dbId) parameter is either ignored or causes connection errors when trying to access databases other than the default one.
Background
Valkey 9.0 introduced support for multiple databases (0-4) in cluster mode when configured with cluster-databases: N. This feature works correctly with other Redis clients (e.g., redis-py), but StackExchange.Redis fails to utilize this functionality.
Expected Behavior
When calling GetDatabase(dbId) where dbId is between 0 and the configured cluster-databases value (-1), the library should:
- Select the specified database
- Execute all subsequent operations on that database
- Maintain proper isolation between databases
Actual Behavior
When using GetDatabase(dbId) with different dbId values:
- Operations on
dbId != 0may fail with connection errors - Error:
"Endpoint Unspecified/valkey-node-1:6379 serving hashslot X is not reachable" - Only some databases work intermittently (unstable behavior)
- The library appears to ignore the database parameter in cluster mode
Test Results Comparison
Python (redis-py) - Works Correctly
All 5 databases correctly isolated!
All tests passed!
Valkey 9.0 cluster fully supports multiple databases (0-4).
- DB 0, 1, 2, 3, 4: All PASS
- SET/GET operations work correctly on each database
- Database isolation is maintained
- DBSIZE returns correct counts for each database
.NET (StackExchange.Redis) - Fails
DB 0: Exception - Endpoint Unspecified/valkey-node-1:6379 serving hashslot 25 is not reachable
DB 1: Exception - Endpoint Unspecified/valkey-node-1:6379 serving hashslot 25 is not reachable
DB 2: only_in_db_2 (expected 'only_in_db_2') -> PASS // Intermittent
DB 3: Exception - Endpoint Unspecified/valkey-node-1:6379 serving hashslot 25 is not reachable
DB 4: Exception - Endpoint Unspecified/valkey-node-1:6379 serving hashslot 25 is not reachable
- Most databases throw exceptions
- Only DB 2 works occasionally (unstable)
GetDatabase(dbId)parameter appears ignored
Reproduction
Test Environment
- Valkey 9.0.3 cluster with 3 nodes
- Configuration:
cluster-databases: 5 - StackExchange.Redis version: 2.8.24
- .NET version: 9.0
Steps to Reproduce
- Start a Valkey 9.0.3 cluster with multiple databases:
docker compose up -d
docker exec valkey-node-1 valkey-cli --cluster create \
valkey-node-1:6379 valkey-node-2:6379 valkey-node-3:6379 \
--cluster-replicas 0 --cluster-yes
- Run Python tests (works correctly):
docker compose exec test-client python /app/test_cluster_dbs.py
- Run .NET tests (fails):
docker exec -it dotnet-test-client bash
cd /app/ClusterTests
dotnet build
dotnet run
- Observe that Python tests pass all database operations while .NET tests fail for most databases
Impact
- Users cannot use multi-tenancy through database separation in cluster mode
- Migration from standalone Redis with multiple databases to cluster is blocked
- Valkey 9.0's multi-database feature is unusable with StackExchange.Redis in cluster mode
Additional Observations
- The Valkey server correctly supports multi-database operations (proven by successful Python tests)
- The issue is specific to the StackExchange.Redis library
- Hash-tagged keys are used to ensure consistent slot targeting
- Connection to cluster succeeds: 3 endpoints detected
Related Configuration
valkey-node-1:
command: >
valkey-server
--cluster-enabled yes
--cluster-databases 5
--cluster-config-file nodes.conf
--cluster-node-timeout 5000
--protected-mode no
Requested Enhancement
Add proper support for GetDatabase(dbId) in cluster mode to work with Valkey/Redis 9.0+ multi-database functionality. The library should:
- Respect the
dbIdparameter in cluster mode - Route operations to the correct database on the appropriate node
- Maintain database isolation when multiple databases are configured
- Support server-level commands (DBSIZE, FLUSHDB) with database selection
Activity
mgravell commentedon Mar 19, 2026
Yep, totally agree. I had this somewhere in the back of my mind (it came up in a Valkey library maintainers meeting one time), and oddly enough the biggest blocker was the "figure out how to spin up Valkey to test it" (testing hosted Valkey is almost impossible), so your bullet
1has probably unblocked a lot here.I don't think this is a huge piece of work to implement - it is mostly removing some side-effects. Fundamentally the library is already DB-aware - just currently we infer "it is cluster" => "maxdatabases = 1", which is no longer valid.
We might want to specifically detect Valkey, though - which I think we can do by checking the
infooutput forvalkey_version:whateverorserver_name:valkey. I think I prefer the first option because it generalizes more cleanly; for example, Garnet hasgarnet_version:whatever, but it doesn't have aserver_name:garnet.mgravell commentedon Mar 20, 2026
@dolgovas Any chance you can try 2.12.9-g73a3adbad6 from the myget feed https://www.myget.org/F/stackoverflow/api/v3/index.json ?
i.e.
dotnet new nuget.configand add a element likeand in the csproj, bump the version:
dolgovas commentedon Mar 24, 2026
I tested the pre-release version 2.12.9-g73a3adbad6 from the MyGet feed with Valkey 9.0.3 cluster (3 nodes, 5 databases configured).
Test setup:
Results:
After multiple test runs, the multi-DB support appears to be working:
Test 2: Database isolation
DB 0: only_in_db_0 (expected 'only_in_db_0') -> PASS
DB 1: only_in_db_1 (expected 'only_in_db_1') -> PASS
DB 4: (expected '') -> PASS
Different test runs showed successful operations on various databases (DB 0, 1, 2, 3, 4), with proper isolation between them - each database correctly stores and retrieves its own data independently.
Note: I did encounter some connection stability issues in the Docker environment, but these appear to be environmental rather than related to the multi-DB functionality itself. The core multi-DB isolation seems to work correctly when the connection is stable.
Conclusion: From my testing, the fix in PR #3040 looks promising for multi-DB cluster usage on Valkey/Redis 9.0+. The databases are properly isolated and GetDatabase(dbId) works as expected in cluster mode.