redis/redis-py · error · ValueError

Given database is not a member of database list

Error message

Given database is not a member of database list

What it means

Raised as ValueError by MultiDBClient.set_active_database() when the Database object passed in is not present in the client's weighted database list. The method intentionally refuses to promote an unknown database because routing/health tracking is keyed off the configured set.

Solutions

  1. Always obtain the target via client.get_databases() and pass one of those entries to set_active_database.
  2. If you need a brand-new endpoint, use await client.add_database(config) first, then promote the returned/added Database.
  3. Before calling, guard with: if db not in {d for d, _ in client.get_databases()}: ... to fail with your own message.
  4. Drop any cached Database references after remove_database() and re-query get_databases().

Example fix

// before
target = Database(client=Redis.from_url("redis://other:6379"), circuit=cb, weight=1)
await client.set_active_database(target)  # ValueError

// after
available = {d for d, _ in client.get_databases()}
target = next(d for d in available if d.weight == max(w for _, w in client.get_databases()))
await client.set_active_database(target)
Defensive patterns

Strategy: validation

Validate before calling

def is_known_database(client, db) -> bool:
    return any(existing is db for existing, _ in client.get_databases())

# before promoting:
if not is_known_database(client, target):
    raise ValueError(f"{target!r} is not in client.get_databases()")

Type guard

from redis.asyncio.multidb.database import AsyncDatabase

def is_async_database(obj) -> bool:
    return isinstance(obj, AsyncDatabase)

Try / catch

try:
    await client.set_active_database(target)
except ValueError as e:
    if "not a member of database list" in str(e):
        # fetch a fresh reference and retry, or surface to the operator
        target = next(d for d, _ in client.get_databases() if d.circuit.state.name == "CLOSED")
        await client.set_active_database(target)
    else:
        raise

Prevention

When it happens

Trigger: Calling await client.set_active_database(db) with a Database instance that was not returned by get_databases() / not added via MultiDbConfig.databases_config or add_database(). Commonly: constructing a brand-new Database(...) by hand, passing a Database belonging to a different MultiDBClient, or passing one obtained before remove_database() was called on it.

Common situations: Manually rebuilding a Database object to 'force' a failover target, holding stale references after topology reconfiguration, copy/paste between two MultiDBClient instances, or unit tests that fabricate a Database instead of pulling it from get_databases().

Related errors


AI-assisted analysis of redis/redis-py@6a6b581b48 (2026-08-10). Data as JSON: /api/errors/f7d257d0557f87fb. Report an issue: GitHub.

Appendix: source

Thrown at redis/asyncio/multidb/client.py:164

    def get_databases(self) -> Databases:
        """
        Returns a sorted (by weight) list of all databases.
        """
        return self._databases

    async def set_active_database(self, database: AsyncDatabase) -> None:
        """
        Promote one of the existing databases to become an active.
        """
        exists = None

        for existing_db, _ in self._databases:
            if existing_db == database:
                exists = True
                break

        if not exists:
            raise ValueError("Given database is not a member of database list")

        await self._check_db_health(database)

        if database.circuit.state == CBState.CLOSED:
            highest_weighted_db, _ = self._databases.get_top_n(1)[0]
            await self.command_executor.set_active_database(
                database, GeoFailoverReason.MANUAL
            )
            return

        raise NoValidDatabaseException(
            "Cannot set active database, database is unhealthy"
        )

    async def add_database(
        self, config: DatabaseConfig, skip_initial_health_check: bool = True
    ):
        """

View on GitHub (pinned to 6a6b581b48)