Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
Memcached keeps key → value pairs in RAM up to a size limit. When memory is full, it removes the least recently used items (eviction). Data is lost on restart — it is a cache, never the only copy. Clients spread keys over several servers themselves (consistent hashing).
| Item | RHEL / Rocky / Alma | Ubuntu / Debian |
|---|---|---|
| Package | memcached (+ libmemcached tools) | memcached (+ libmemcached-tools) |
| Service | memcached.service | the same |
| Config | /etc/sysconfig/memcached | /etc/memcached.conf |
| Port | 11211/tcp (UDP must be off) | the same |
| Tools | memcached-tool, memcstat | the same |
Memcached or Redis? Memcached: the simplest pure cache, multi-threaded, no persistence. Redis/Valkey: data types, persistence, queues, replication. If you need more than a cache, choose Redis.
Install on RHEL, Rocky, Alma and Fedora
dnf -y install memcached libmemcached-awesome-toolsInstall on Ubuntu and Debian
apt-get update
apt-get install -y memcached libmemcached-toolsFirst configuration: memory, connections, listen only locally
On RHEL:
PORT="11211"
USER="memcached"
MAXCONN="2048"
CACHESIZE="1024"
OPTIONS="-l 127.0.0.1,::1 -U 0 -t 4"On Ubuntu, the same in /etc/memcached.conf (one option per line):
-d
-m 1024
-p 11211
-u memcache
-l 127.0.0.1
-c 2048
-U 0
-t 4
logfile /var/log/memcached.log| Option | Why |
|---|---|
CACHESIZE / -m 1024 | Max memory in MB for items (here 1 GB). Leave RAM for the OS and apps. |
-l 127.0.0.1 | Listen only on localhost (or a private IP when app servers are elsewhere). |
-U 0 | UDP off. Open UDP Memcached servers were used for 1+ Tbps amplification DDoS attacks. |
MAXCONN / -c | Enough connections for all app workers. |
-t 4 | Worker threads (about the number of CPU cores used for it). |
systemctl enable --now memcached
systemctl restart memcached
ss -lntup | grep 11211
tcp LISTEN 0 1024 127.0.0.1:11211 0.0.0.0:* users:(("memcached",pid=1820,fd=26))Only TCP on 127.0.0.1 — that is what you want. If app servers on other hosts need it, listen on the private IP and allow only them:
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" port port="11211" protocol="tcp" accept'
firewall-cmd --reloadFor untrusted networks, Memcached supports SASL authentication (-S) and TLS (-Z, when built with TLS) — or keep it on localhost and avoid the problem.
Connect PHP (sessions and app cache)
dnf -y install php-pecl-memcached
apt-get install -y php-memcachedsession.save_handler = memcached
session.save_path = "127.0.0.1:11211"(Ubuntu: /etc/php/8.3/fpm/conf.d/.) Restart PHP-FPM. For WordPress, a Memcached object-cache plugin uses the same server. With several web servers and sessions in Memcached, users stay logged in on every server — but sessions are lost when Memcached restarts (use Redis if that matters).
Daily use: the commands and the real uses
memcached-tool 127.0.0.1:11211 stats | grep -E 'curr_items|get_hits|get_misses|evictions|bytes |limit_maxbytes|curr_connections'
memcached-tool 127.0.0.1:11211 display
memcstat --servers=127.0.0.1
printf 'stats\r\nquit\r\n' | nc 127.0.0.1 11211 | grep -E 'get_hits|get_misses'
printf 'flush_all\r\nquit\r\n' | nc 127.0.0.1 11211The numbers that matter:
- Hit ratio =
get_hits / (get_hits + get_misses)— 90 %+ is good for most apps. - evictions growing fast = the cache is too small (raise
-m) or keys live too long. - curr_connections near
MAXCONN= raise the limit or fix connection leaks.
flush_all empties the whole cache — the database gets all the load at once; do it only when needed.
Real uses at work:
- Cache database results and rendered fragments in PHP/Python/Java apps.
- Shared sessions for a few web servers (when losing them on restart is OK).
- WordPress / WooCommerce / Magento object cache.
Make it safe
Security checklist:
- Listen on
127.0.0.1(or a private IP with firewall rules) — never public. - UDP off (
-U 0). - No secrets or personal data in the cache unless the network is fully private.
- SASL or TLS if it must cross untrusted networks.
- Monitor evictions, hit ratio and connections (Prometheus
memcached_exporter). - Keep the package updated.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
| App can not connect from another server | Listens on localhost only, or firewall. | Add the private IP to -l; open 11211/tcp for the app subnet. |
Many evictions and low hit ratio | Cache too small or TTLs too long. | Raise memory (-m); shorter TTLs; cache only what helps. |
SERVER_ERROR out of memory storing object | An item larger than the max item size (1 MB default). | Store smaller items, or raise -I (for example -I 2m) carefully. |
| Users logged out after a restart | Sessions were only in Memcached. | Expected; use Redis with persistence for important sessions. |
| Service will not start after editing | A wrong option line. | journalctl -u memcached; on Ubuntu one option per line in memcached.conf. |
| A security scan reports UDP 11211 open | UDP was not turned off. | Add -U 0 and restart. |
Next steps
- The richer cache: Redis / Valkey. Caches at work: Cache and sessions.
- The app side: Nginx, Apache with PHP-FPM.
- Watch it: Prometheus + Grafana.
1Why must UDP be off on Memcached?
Open UDP Memcached servers were abused for huge DDoS amplification attacks.
2What does a fast-growing evictions counter tell you?
The cache is too small (or keys live too long), so useful items are thrown out.