Memcache vs Memcached: PHP Extension Comparison and Connection Troubleshooting


2 views

When working with Memcached in PHP, you'll encounter two similarly-named extensions with critical differences:

  • memcache (pecl/memcache): Older implementation (v2.2.7 latest), procedural API, less active development
  • memcached (pecl/memcached): Modern implementation (v3.2.0+), object-oriented API, supports newer Memcached features
// memcache example (old)
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$memcache->set('key', 'value', false, 30);

// memcached example (recommended)
$memcached = new Memcached;
$memcached->addServer('localhost', 11211);
$memcached->set('key', 'value', 30);

For the memcached extension (recommended choice):

# RHEL/CentOS
sudo yum install php-pecl-memcached

# Debian/Ubuntu
sudo apt-get install php-memcached

Memcached defaults to port 11211. If you're getting connection refused errors:

  1. Verify service is running: systemctl status memcached
  2. Check listening ports: netstat -tulnp | grep memcached
  3. Configure bind address in /etc/sysconfig/memcached:
    OPTIONS="-l 127.0.0.1"
Feature memcache memcached
Binary protocol No Yes
CAS operations Limited Full support
Compression zlib only Multiple options

Test your Memcached server directly:

telnet localhost 11211
stats

Common PHP configuration issues:

// Check installed extensions
php -m | grep memcached

// Verify php.ini loads the extension
extension=memcached.so

For high-availability setups, use multiple servers:

$servers = [
    ['mem1.example.com', 11211, 33],
    ['mem2.example.com', 11211, 67]
];
$m->addServers($servers);

The confusion stems from two similarly named PHP extensions that interface with memcached servers:

  • Memcache (no 'd'): The older, procedural-style extension that's been around since 2004. Uses the deprecated PHP4-style API.
  • Memcached (with 'd'): The newer, object-oriented extension that's actively maintained and supports more features.

Key differences in practice:

// Memcache example (legacy)
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$memcache->set('key', 'value', false, 3600);

// Memcached example (recommended)
$memcached = new Memcached;
$memcached->addServer('localhost', 11211);
$memcached->set('key', 'value', 3600);

Unless you're maintaining legacy code, always opt for the memcached extension because:

  • Supports newer protocol features like binary protocol
  • Provides better error handling
  • Includes OOP interface and namespacing
  • Offers advanced features like multi-get operations
  • Actively maintained with regular updates

For CentOS/RHEL systems:

# Install the server
sudo yum install memcached

# Install PHP extension
sudo yum install php-pecl-memcached

# Start service
sudo systemctl start memcached
sudo systemctl enable memcached

If you're getting "connection refused" errors:

  1. Verify service status:
    systemctl status memcached
  2. Check binding configuration in /etc/sysconfig/memcached:
    OPTIONS="-l 127.0.0.1"  # Ensure it's listening on localhost
  3. Firewall rules (even for local connections):
    sudo firewall-cmd --add-port=11211/tcp --permanent
    sudo firewall-cmd --reload
  4. Test with basic PHP script:
    <?php
    $m = new Memcached();
    $m->addServer('localhost', 11211);
    $m->set('test', 'OK', 10);
    echo $m->get('test'); // Should output "OK"
    ?>

The memcached extension provides better performance through:

  • Persistent connections (reduces TCP overhead)
  • Better compression handling
  • Multi-threaded operations

For maximum performance, consider using UNIX sockets when connecting locally:

$m->addServer('/var/run/memcached/memcached.sock', 0);