Need to migrate your e-mails to a new server? Free and paid versions of our online tool available.
Hero Image

Memcached Distributed Setup on Debian 12

Memcached Distributed Setup on Debian 12

Memcached itself has no cluster mode — distribution is handled by the client using consistent hashing.

Run multiple instances

memcached -d -l 192.168.1.10 -p 11211 -m 512 -t 4 -u memcache
memcached -d -l 192.168.1.11 -p 11211 -m 512 -t 4 -u memcache

systemd template unit (/etc/systemd/system/[email protected])

[Unit]
Description=Memcached instance %i
[Service]
Type=forking
ExecStart=/usr/bin/memcached -d -l 127.0.0.1 -p %i -m 256 -t 4 -u memcache
PIDFile=/run/memcached/memcached-%i.pid
[Install]
WantedBy=multi-user.target
systemctl enable --now memcached@11211 memcached@11212

PHP consistent hashing

$mc = new Memcached();
$mc->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$mc->addServers([
    ['192.168.1.10', 11211],
    ['192.168.1.11', 11211],
    ['192.168.1.12', 11211],
]);

Monitor all nodes

for h in 192.168.1.10 192.168.1.11 192.168.1.12; do
    echo -n "$h: "; echo "stats" | nc $h 11211 | grep -E 'curr_items|bytes '
done