phalcon/cphalcon · error · Phalcon\Storage\Exceptions\DatabaseSelectionFailed

Redis server selected database failed

Error message

Redis server selected database failed

What it means

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.

Source

Thrown at phalcon/Storage/Adapter/Redis.zep:470

                    port
                )
            );
        }

        return this;
    }

    /**
     * @throws DatabaseSelectionFailed
     */
    private function checkIndex(<RedisService> connection) -> <static>
    {
        var index;

        let index = this->options["index"];

        if (index > 0 && true !== connection->select(index)) {
            throw new DatabaseSelectionFailed();
        }

        return this;
    }

    /**
     * Checks the serializer. If it is a supported one it is set, otherwise
     * the custom one is set.
     *
     * @throws BaseException
     */
    private function setSerializer(<RedisService> connection) -> void
    {
        var serializer;
        array map;

        let map = [
            "redis_none" : RedisService::SERIALIZER_NONE,

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Check the server limit: redis-cli CONFIG GET databases, then lower 'index' below that value
  2. Drop the 'index' option to stay on DB 0 (no SELECT is issued when index is 0)
  3. For ACL setups, grant the user access to the selected database
  4. Prefer key prefixes over DB indexes when sharing managed Redis instances

Example fix

// before
new Redis($factory, ['index' => 16]); // out of range on default databases=16

// after
new Redis($factory, ['index' => 1]); // valid 0-15 by default
Defensive patterns

Strategy: validation

Validate before calling

// bound-check before the adapter issues SELECT (default databases = 16)
$index = (int) ($options['index'] ?? 0);
if ($index < 0 || $index > 15) { // raise the ceiling after CONFIG GET databases
    throw new InvalidArgumentException("Redis DB index {$index} out of range");
}

Try / catch

try {
    $cache->get('warmup');
} catch (\Phalcon\Storage\Exceptions\DatabaseSelectionFailed $e) {
    // index beyond `databases` or ACL restriction — config error, not transient
    $logger->critical('Redis SELECT failed for index ' . $options['index']);
    throw $e;
}

Prevention

When it happens

Trigger: '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.

Common situations: 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.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/8e260f24d8e38ef3. Report an issue: GitHub.