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

Could not connect to the Redis server [%s:%s]

Error message

Could not connect to the Redis server [%s:%s]

What it means

The companion of the exception path: when phpredis connect()/pconnect() signals failure by returning false instead of throwing, Phalcon formats the configured host and port into ConnectionFailed('Could not connect to the Redis server [host:port]'). Same network causes, different phpredis failure mode.

Source

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

        }

        /** @var storage_redis_context $connectionOptions */
        try {
            let result = connection->{method}(
                host,
                port,
                timeout,
                parameter,
                retryInterval,
                readTimeout,
                connectionOptions
            );
        } catch \Exception, ex {
            throw new ConnectionFailed(ex->getMessage());
        }

        if !result {
            throw new ConnectionFailed(
                sprintf(
                    "Could not connect to the Redis server [%s:%s]",
                    host,
                    port
                )
            );
        }

        return this;
    }

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

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Read the host:port from the message and diff it against the running server config (defaults are 127.0.0.1:6379)
  2. Confirm from the app container: redis-cli -h <host> -p <port> PING
  3. For persistent connections, change or drop 'persistentId' to force a fresh socket, or set 'retryInterval' so phpredis retries
  4. Handle both ConnectionFailed messages identically in your cache wrapper

Example fix

// before: stale persistent socket after redis restart
new Redis($factory, ['persistent' => true, 'persistentId' => 'worker-1']);

// after: allow reconnect
new Redis($factory, ['persistent' => true, 'persistentId' => 'worker-1', 'retryInterval' => 100]);
Defensive patterns

Strategy: retry

Validate before calling

// normalize config so both failure modes point at the right target
$options['host'] = (string) ($options['host'] ?? '127.0.0.1');
$options['port'] = (int) ($options['port'] ?? 6379);
if ($options['persistent'] ?? false) {
    $options['retryInterval'] = (int) ($options['retryInterval'] ?? 100);
}

Try / catch

try {
    return $cache->get($key);
} catch (\Phalcon\Storage\Exceptions\ConnectionFailed $e) {
    // message contains 'Could not connect to the Redis server [host:port]' — verify that target
    if (str_contains($e->getMessage(), 'Could not connect to the Redis server')) {
        return $default; // false-return mode of phpredis; same handling as 711
    }
    throw $e;
}

Prevention

When it happens

Trigger: Server down or port closed on phpredis builds that return false rather than raising RedisException; TLS timeouts during handshake; pconnect() with a stale 'persistentId' socket after a Redis restart, where reconnect fails silently.

Common situations: Upgrading or downgrading ext-redis changes whether failures throw or return false, so the same outage surfaces with a different message; long-lived queue workers using persistent connections across Redis restarts.

Related errors


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