{"record":{"id":"ec8c19ab736e3d44","repo":"getgrav/grav","slug":"could-not-select-alternate-redis-database-id","errorCode":null,"errorMessage":"Could not select alternate Redis database ID","messagePattern":"Could not select alternate Redis database ID","errorType":"exception","errorClass":"RedisException","httpStatus":null,"severity":"warning","filePath":"system/src/Grav/Common/Cache.php","lineNumber":400,"sourceCode":"                        $databaseId = $this->config->get('system.cache.redis.database', 0);\n\n                        if ($socket) {\n                            $redis->connect($socket);\n                        } else {\n                            $redis->connect(\n                                $this->config->get('system.cache.redis.server', 'localhost'),\n                                $this->config->get('system.cache.redis.port', 6379)\n                            );\n                        }\n\n                        // Authenticate with password if set\n                        if ($password && !$redis->auth($password)) {\n                            throw new \\RedisException('Redis authentication failed');\n                        }\n\n                        // Select alternate ( !=0 ) database ID if set\n                        if ($databaseId && !$redis->select($databaseId)) {\n                            throw new \\RedisException('Could not select alternate Redis database ID');\n                        }\n\n                        $adapter = new RedisAdapter($redis, $namespace, $defaultLifetime);\n                        $resolved_driver_name = 'redis';\n                    } catch (Throwable $e) {\n                        $this->logCacheFallback($driver_name, 'file', $e->getMessage());\n                        $adapter = $this->createFilesystemAdapter($namespace, $defaultLifetime);\n                        $resolved_driver_name = 'file';\n                    }\n                } else {\n                    $this->logCacheFallback($driver_name, 'file', 'Redis extension not installed');\n                    $adapter = $this->createFilesystemAdapter($namespace, $defaultLifetime);\n                    $resolved_driver_name = 'file';\n                }\n                break;\n\n            case 'array':\n                $adapter = new ArrayAdapter($defaultLifetime, false);","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Common/Cache.php#L382-L418","documentation":"After a successful Redis connection and optional authentication, Grav calls select() when system.cache.redis.database is a truthy value. If Redis refuses the database ID, Grav throws RedisException and its catch block falls back to the filesystem adapter while logging the reason.","triggerScenarios":"Configure system.cache.redis.driver=redis and system.cache.redis.database to an index outside the server's configured range, an index denied by ACL, or a value sent to a cluster/proxy that does not permit SELECT. Cache.php:399-401 throws and lines 405-409 install the file adapter instead.","commonSituations":"Copying a database number from an environment with 32 databases to one using the Redis default of 16, using database 16 or higher, typos such as an unintended positive value, managed Redis that exposes only DB 0, or ACL restrictions.","solutions":["Run redis-cli -h HOST -p PORT -a 'PASSWORD' SELECT N to confirm the exact index is allowed; also check CONFIG GET databases on a self-managed server.","Set system.cache.redis.database to 0 or remove the key when the deployment uses only the default database.","Correct the value to an integer in the permitted 0..databases-1 range and clear Grav's cached configuration.","For managed or clustered Redis that disallows SELECT, keep the database at 0 and use separate instances or key prefixes for isolation.","Restart PHP/FPM after correcting environment-based configuration."],"exampleFix":"# before (user/config/system.yaml)\ncache:\n  driver: redis\n  redis:\n    database: 32 # server has only 16 databases\n\n# after\ncache:\n  driver: redis\n  redis:\n    database: 2 # valid 0..15 on this server","handlingStrategy":"validation","validationCode":"$redis = new Redis();\n$redis->connect($host, $port);\nif ($password) {\n    $redis->auth($password);\n}\nif ($databaseId && !$redis->select((int) $databaseId)) {\n    throw new RuntimeException('Redis rejected database ' . $databaseId . '; use 0 or a permitted index.');\n}\n$redis->close();","typeGuard":null,"tryCatchPattern":"try {\n    $cache = new RedisAdapter($redis, $namespace, $lifetime);\n} catch (RedisException $e) {\n    error_log('Redis database unavailable; using filesystem cache: ' . $e->getMessage());\n    $cache = new FilesystemAdapter($namespace, $lifetime, $cacheDir);\n}","preventionTips":["Check CONFIG GET databases before choosing an index.","Treat managed Redis as database 0 unless its provider documents SELECT support.","Validate cache configuration per environment.","Clear Grav's cached configuration after changing Redis settings.","Alert when a deployment permanently falls back from Redis to file cache."],"tags":["grav","cache","redis","database-index","phpredis","fallback"],"backgroundTag":"redis-db-select-failed","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}