Skip to content

Multi-Database Support in Cluster Mode #3039

@dolgovas

Description

@dolgovas

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 != 0 may 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

  1. 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
  1. Run Python tests (works correctly):
docker compose exec test-client python /app/test_cluster_dbs.py
  1. Run .NET tests (fails):
docker exec -it dotnet-test-client bash
cd /app/ClusterTests
dotnet build
dotnet run
  1. 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:

  1. Respect the dbId parameter in cluster mode
  2. Route operations to the correct database on the appropriate node
  3. Maintain database isolation when multiple databases are configured
  4. Support server-level commands (DBSIZE, FLUSHDB) with database selection

Metadata

Metadata

Assignees

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