yiisoft/yii2 · error · InvalidConfigException

The 'host' property must be specified for every memcache ser

Error message

The 'host' property must be specified for every memcache server.

What it means

Thrown by yii\caching\MemCache::addServers() when any configured server entry has a null host property. Server entries are hydrated into MemCacheServer objects; when servers is non-empty, every entry must at least specify host (port defaults exist, host does not). If no servers are configured at all, a localhost default is used — the exception is specifically about partially specified server entries.

Source

Thrown at framework/caching/MemCache.php:138

    /**
     * Add servers to the server pool of the cache specified.
     *
     * @param \Memcache|\Memcached $cache
     * @param MemCacheServer[] $servers
     * @throws InvalidConfigException
     */
    protected function addServers($cache, $servers)
    {
        if (empty($servers)) {
            $servers = [new MemCacheServer([
                'host' => '127.0.0.1',
                'port' => 11211,
            ])];
        } else {
            foreach ($servers as $server) {
                if ($server->host === null) {
                    throw new InvalidConfigException("The 'host' property must be specified for every memcache server.");
                }
            }
        }
        if ($this->useMemcached) {
            $this->addMemcachedServers($cache, $servers);
        } else {
            $this->addMemcacheServers($cache, $servers);
        }
    }

    /**
     * Add servers to the server pool of the cache specified
     * Used for memcached PECL extension.
     *
     * @param \Memcached $cache
     * @param MemCacheServer[] $servers
     */
    protected function addMemcachedServers($cache, $servers)

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Add an explicit host to every server entry: ['host' => '10.0.0.5', 'port' => 11211].
  2. When building servers from env vars, default them: 'host' => getenv('MEMCACHE_HOST') ?: '127.0.0.1'.
  3. Remove empty placeholder entries from the servers array entirely — an empty array triggers the sane localhost default instead of throwing.

Example fix

// before
'cache' => [
    'class' => \yii\caching\MemCache::class,
    'servers' => [
        ['port' => 11211],              // host missing -> throws
        ['host' => '10.0.0.5', 'port' => 11211],
    ],
],

// after
'cache' => [
    'class' => \yii\caching\MemCache::class,
    'servers' => [
        ['host' => '127.0.0.1', 'port' => 11211],
        ['host' => '10.0.0.5', 'port' => 11211],
    ],
],
Defensive patterns

Strategy: validation

Validate before calling

foreach ($servers as $i => $server) {
    if (empty($server['host'])) {
        $servers[$i]['host'] = getenv('MEMCACHE_HOST') ?: '127.0.0.1';
    }
}
// drop entries that still have no host rather than letting MemCache::addServers() throw
$config['servers'] = array_values(array_filter($servers, fn($s) => !empty($s['host'])));

Prevention

When it happens

Trigger: Configuring 'cache' => ['class' => MemCache::class, 'servers' => [['port' => 11211, 'weight' => 1]]] — host key missing; entry arrays built dynamically where the host assignment is skipped for one node; a config template with a placeholder that never got filled; mixing string server values into the array.

Common situations: Copy-pasted multi-node memcached configs with one entry trimmed; environment-specific config where host comes from an env var that is unset in one environment (null passed through); converting an old spyc/yaml config where the host key lost its indentation; provisioning scripts generating server lists conditionally.

Related errors


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/27489f47b2ff88d4. Report an issue: GitHub.