{"record":{"id":"8e260f24d8e38ef3","repo":"phalcon/cphalcon","slug":"redis-server-selected-database-failed","errorCode":null,"errorMessage":"Redis server selected database failed","messagePattern":"Redis server selected database failed","errorType":"exception","errorClass":"Phalcon\\Storage\\Exceptions\\DatabaseSelectionFailed","httpStatus":null,"severity":"error","filePath":"phalcon/Storage/Adapter/Redis.zep","lineNumber":470,"sourceCode":"                    port\n                )\n            );\n        }\n\n        return this;\n    }\n\n    /**\n     * @throws DatabaseSelectionFailed\n     */\n    private function checkIndex(<RedisService> connection) -> <static>\n    {\n        var index;\n\n        let index = this->options[\"index\"];\n\n        if (index > 0 && true !== connection->select(index)) {\n            throw new DatabaseSelectionFailed();\n        }\n\n        return this;\n    }\n\n    /**\n     * Checks the serializer. If it is a supported one it is set, otherwise\n     * the custom one is set.\n     *\n     * @throws BaseException\n     */\n    private function setSerializer(<RedisService> connection) -> void\n    {\n        var serializer;\n        array map;\n\n        let map = [\n            \"redis_none\" : RedisService::SERIALIZER_NONE,","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Storage/Adapter/Redis.zep#L452-L488","documentation":"After connect and auth, the Redis adapter runs SELECT when options['index'] > 0. If the server refuses the database — 'DB index is out of range' (index >= the configured databases, default 16 so valid indexes are 0-15) or an ACL user restricted to other databases — Phalcon throws DatabaseSelectionFailed.","triggerScenarios":"'index' => 16 on a default redis.conf (databases 16, indexes 0-15); copying an index tuned for a server with databases 32 to one with defaults; ACL user lacking permissions on the requested DB; index option accidentally set from an env string like '7' without validation of bounds.","commonSituations":"Sharing one Redis between environments using DB indexes (dev=1, staging=2, prod=3) on servers with different databases settings; a cluster/managed Redis that ignores or restricts SELECT.","solutions":["Check the server limit: redis-cli CONFIG GET databases, then lower 'index' below that value","Drop the 'index' option to stay on DB 0 (no SELECT is issued when index is 0)","For ACL setups, grant the user access to the selected database","Prefer key prefixes over DB indexes when sharing managed Redis instances"],"exampleFix":"// before\nnew Redis($factory, ['index' => 16]); // out of range on default databases=16\n\n// after\nnew Redis($factory, ['index' => 1]); // valid 0-15 by default","handlingStrategy":"validation","validationCode":"// bound-check before the adapter issues SELECT (default databases = 16)\n$index = (int) ($options['index'] ?? 0);\nif ($index < 0 || $index > 15) { // raise the ceiling after CONFIG GET databases\n    throw new InvalidArgumentException(\"Redis DB index {$index} out of range\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    $cache->get('warmup');\n} catch (\\Phalcon\\Storage\\Exceptions\\DatabaseSelectionFailed $e) {\n    // index beyond `databases` or ACL restriction — config error, not transient\n    $logger->critical('Redis SELECT failed for index ' . $options['index']);\n    throw $e;\n}","preventionTips":["Document the server's `databases` value next to every 'index' setting","Omit 'index' for DB 0 — no SELECT is issued and the error cannot occur","Prefer key prefixes over DB indexes on managed Redis offerings"],"tags":["php","phalcon","redis","cache","configuration"],"backgroundTag":"invalid-database-index","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}