{"record":{"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/6a6b581b48225afa0b76912d1028c6035baee932/redis/commands/cluster.py#L456-L492","documentation":"Raised by ClusterManagementCommands.swapdb() in redis/commands/cluster.py:474. SWAPDB swaps two logical databases on a single Redis instance, but Redis Cluster only permits database 0; swapping databases would break the slot-to-node mapping. The method is overridden to fail fast.","triggerScenarios":"Calling rc.swapdb(0, 1) (or any indices) on a RedisCluster client.","commonSituations":"Multi-DB application code reused against a cluster endpoint; tooling that reorganizes databases generically.","solutions":["Do not use multiple databases in cluster mode; keep all data in DB 0.","If you need logical separation, use key prefixes or hash tags instead of DB indexes.","Only call swapdb() on a standalone redis.Redis client."],"exampleFix":"// before\nrc.swapdb(0, 1)\n// after\n# cluster mode supports only db 0; use key namespacing instead\nrc.set('featureA:k', 'v')\nrc.set('featureB:k', 'v')","handlingStrategy":"type-guard","validationCode":"from redis.cluster import RedisCluster\nif not isinstance(rc, RedisCluster):\n    rc.swapdb(0, 1)\n# else: cluster supports only db 0; do nothing or use key namespacing","typeGuard":"from redis.cluster import RedisCluster\ndef is_cluster_client(client) -> bool:\n    return isinstance(client, RedisCluster)","tryCatchPattern":"from redis.exceptions import RedisClusterException\ntry:\n    rc.swapdb(0, 1)\nexcept RedisClusterException:\n    pass  # cluster mode forbids SWAPDB","preventionTips":["Use only DB 0 in cluster mode.","Namespace with key prefixes instead of databases."],"tags":["cluster","database","api-misuse","unsupported-command"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}