Memcached is a simple, fast, multi-threaded in-memory cache for small chunks of data — database query results, rendered fragments, API responses. It has no persistence and no complex data types; that simplicity makes it extremely efficient. Here’s how to install it safely.
1. Install
sudo apt update
sudo apt install -y memcached libmemcached-tools
systemctl status memcached --no-pager
On AlmaLinux/Rocky: sudo dnf install memcached (config in /etc/sysconfig/memcached).
2. Configure
Debian/Ubuntu: /etc/memcached.conf.
# Memory for items, in MB
-m 256
# Port and listening address: localhost only
-p 11211
-l 127.0.0.1
# Disable UDP (prevents use in amplification attacks)
-U 0
# Max simultaneous connections
-c 1024
# Run as user
-u memcache
sudo systemctl restart memcached
Why -l 127.0.0.1 and -U 0 matter
Memcached has no authentication by default. An instance reachable from the internet can be read and written by anyone — and its UDP protocol has been abused for some of the largest DDoS amplification attacks ever recorded (a tiny request produces a huge response sent to a spoofed victim). Keep it on localhost (or a private network with a firewall), and disable UDP. Recent versions disable UDP by default; set it explicitly anyway.
If other servers must connect, bind to the private IP and allow only them:
sudo ufw allow from 10.0.0.12 to any port 11211 proto tcp
For access over untrusted networks, Memcached supports SASL authentication and TLS (when built with them) — or tunnel the traffic over a VPN.
3. Check it’s running
sudo ss -tlnp | grep 11211
memcstat --servers=127.0.0.1
echo stats | nc -q1 127.0.0.1 11211 | grep -E 'curr_items|get_hits|get_misses|evictions|limit_maxbytes'
Useful counters:
- get_hits / get_misses — hit ratio; low means the cache isn’t helping much (or keys are set with very short TTLs).
- evictions — items removed for space; rising fast means
-mis too small. - curr_connections — compare with
-c.
4. Connect from your application
PHP
sudo apt install -y php8.3-memcached # the "memcached" extension (libmemcached-based)
sudo systemctl restart php8.3-fpm
$m = new Memcached();
$m->addServer('127.0.0.1', 11211);
$m->set('greeting', 'hello', 300); // TTL 300 s
echo $m->get('greeting');
PHP sessions in Memcached:
session.save_handler = memcached
session.save_path = "127.0.0.1:11211"
Python
pip install pymemcache
from pymemcache.client.base import Client
c = Client(("127.0.0.1", 11211))
c.set("greeting", "hello", expire=300)
print(c.get("greeting"))
Frameworks: Django (django.core.cache.backends.memcached.PyMemcacheCache), Laravel (CACHE_STORE=memcached), WordPress object-cache drop-ins.
5. Sizing
Memcached stores items in slabs by size class. Items larger than 1 MB aren’t stored by default (-I raises the limit, at some efficiency cost). Start with 64–256 MB on a small server and adjust from the eviction counter.
6. Flush and restart
echo flush_all | nc -q1 127.0.0.1 11211
sudo systemctl restart memcached # also empties the cache
Everything in Memcached is volatile — never store data you can’t regenerate.
Memcached or Redis?
Redis offers persistence, data structures, queues and pub/sub; Memcached is simpler and very efficient for pure caching. Most new projects choose Redis to run one service for everything — see Redis vs Memcached and installing Redis.