{"id":"178480cc78b56df9","repo":"redis/redis-py","slug":"swapdb-is-not-supported-in-cluster-mode","errorCode":null,"errorMessage":"SWAPDB is not supported in cluster mode","messagePattern":"SWAPDB is not supported in cluster mode","errorType":"exception","errorClass":"RedisClusterException","httpStatus":null,"severity":"error","filePath":"redis/commands/cluster.py","lineNumber":474,"sourceCode":"        For more information see https://redis.io/commands/slaveof\n        \"\"\"\n        raise RedisClusterException(\"SLAVEOF is not supported in cluster mode\")\n\n    def replicaof(self, *args, **kwargs) -> NoReturn:\n        \"\"\"\n        Make the server a replica of another instance, or promote it as master.\n\n        For more information see https://redis.io/commands/replicaof\n        \"\"\"\n        raise RedisClusterException(\"REPLICAOF is not supported in cluster mode\")\n\n    def swapdb(self, *args, **kwargs) -> NoReturn:\n        \"\"\"\n        Swaps two Redis databases.\n\n        For more information see https://redis.io/commands/swapdb\n        \"\"\"\n        raise RedisClusterException(\"SWAPDB is not supported in cluster mode\")\n\n    @overload\n    def cluster_myid(\n        self: SyncClientProtocol, target_node: \"TargetNodesT\"\n    ) -> bytes | str: ...\n\n    @overload\n    def cluster_myid(\n        self: AsyncClientProtocol, target_node: \"TargetNodesT\"\n    ) -> Awaitable[bytes | str]: ...\n\n    def cluster_myid(self, target_node: \"TargetNodesT\") -> (bytes | str) | Awaitable[\n        bytes | str\n    ]:\n        \"\"\"\n        Returns the node's id.\n\n        :target_node: 'ClusterNode'","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/commands/cluster.py#L456-L492","documentation":"RedisClusterCommands.swapdb (redis/commands/cluster.py:468) unconditionally raises. SWAPDB swaps two logical databases on a single standalone Redis instance; Redis Cluster only uses database 0 (slots are not multi-database), so swapping databases is meaningless in cluster mode and the command is disabled.","triggerScenarios":"Calling rc.swapdb(0, 1) on a RedisCluster client.","commonSituations":"Code that uses multiple logical databases on standalone redis and is pointed at a cluster. Migration tooling that swaps DBs during deployment. Misunderstanding that cluster mode implies DB 0 only.","solutions":["Stop using multiple databases; cluster mode requires DB 0. Move data into separate key namespaces (prefixes or hash tags) instead of DB indexes.","If you genuinely need SWAPDB, connect a non-cluster client to a single standalone node that is not in cluster mode.","Redesign the data layout to use key namespacing (e.g. 'svcA:k', 'svcB:k') rather than DB numbers."],"exampleFix":"# before\nrc.swapdb(0, 1)  # raises: SWAPDB is not supported in cluster mode\n\n# after\n# use key namespacing instead of DBs in cluster mode\nrc.set('svcA:user', 'alice')\nrc.set('svcB:user', 'bob')","handlingStrategy":"type-guard","validationCode":"from redis.cluster import RedisCluster\n\ndef safe_swapdb(client, i, j):\n    if isinstance(client, RedisCluster):\n        raise ValueError('SWAPDB is unsupported in cluster mode; cluster uses DB 0 only')\n    return client.swapdb(i, j)","typeGuard":"from redis.cluster import RedisCluster\n\ndef is_cluster_client(client) -> bool:\n    return isinstance(client, RedisCluster)","tryCatchPattern":"from redis.exceptions import RedisClusterException\n\ntry:\n    rc.swapdb(0, 1)\nexcept RedisClusterException:\n    # cluster has only DB 0; redesign to use key namespacing instead\n    ...","preventionTips":["Do not rely on multiple databases in cluster mode; use DB 0 with namespaced keys.","Detect RedisCluster clients before issuing SWAPDB in shared code.","Redesign multi-DB layouts into key-prefix or hash-tag namespaces before migrating to cluster."],"tags":["cluster","unsupported-command","database"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}