Securing a Memcached cache
Memcached has no access control by default. On a public server it must listen privately, or anyone can read your cache and use it to attack others.
Why is an open Memcached a problem?
Because it answers anyone who can reach it. Memcached was designed for trusted internal networks, so by default it has no password and will hand cached data to any client, accept writes from it and even flush the whole cache on request. Over UDP it was also abused for some of the largest reflection attacks ever seen: a tiny spoofed request triggered a response tens of thousands of times larger. Current releases ship with UDP off, but older configurations and some packages still turn it on.
How do I check what is exposed now?
List the sockets Memcached has open. You want to see it on 127.0.0.1 or a Unix socket only, and no UDP line at all.
ss -lntup | grep -E ':11211|memcached' # good: tcp LISTEN 0 1024 127.0.0.1:11211 ... memcached # bad: udp UNCONN 0 0 0.0.0.0:11211 ...
From another machine, nc -vz 192.0.2.40 11211 should fail. If it connects, fix the configuration before doing anything else.
How do I bind it to localhost and disable UDP?
Set the listen address and UDP port in the configuration file. On Debian and Ubuntu it is /etc/memcached.conf; on the Enterprise family, the OPTIONS line in /etc/sysconfig/memcached.
# /etc/memcached.conf -l 127.0.0.1,::1 -U 0 -p 11211 -m 4096 -c 2048
# /etc/sysconfig/memcached OPTIONS="-l 127.0.0.1,::1 -U 0"
Restart with systemctl restart memcached and check the sockets again. For applications on the same host, a Unix socket is even tighter: -s /run/memcached/memcached.sock -a 0770 with the web server user in the socket's group.
What if clients on another server need the cache?
Carry the traffic over an encrypted tunnel rather than opening the port. There is no private network between machines here, so the safe pattern is WireGuard between the servers: Memcached listens on the tunnel address only, and the host firewall allows port 11211 from the tunnel interface alone.
-l 127.0.0.1,10.20.0.1 nft add rule inet hostfw input iifname "wg0" tcp dport 11211 accept
Memcached also supports SASL authentication (start it with -S, binary protocol only) and built-in TLS from version 1.5.13 when compiled with it. Both help, but a tunnel plus a firewall rule is simpler to get right and protects every other internal service at the same time. The Company VPN gateways page shows a WireGuard layout between machines.
Should the firewall block port 11211 as well?
Yes, as a second layer. Even with the correct bind address, a firewall rule that drops 11211 from the outside protects you from a future configuration mistake or a package update that resets the file. With a default-deny rule set like the one in Build an iptables firewall, the port is closed unless you open it explicitly.
How much memory should Memcached get?
Enough to hold the working set, and no more than the machine can spare. The -m value is the maximum cache size in megabytes; Memcached does not grab it all at start, but will grow to it. A dedicated cache host can give it most of its RAM, while a web server that also runs PHP and a database should leave room for both.
| Server role | Example machine | Suggested -m |
|---|---|---|
| Web server with local cache | ESSENTIAL-S, 64 GB DDR5 | 4096 to 8192 |
| Shared cache for several web servers | ESSENTIAL-5, 128 GB DDR4 | 98304 or more |
| Application plus database plus cache | BUSINESS-2 2024, 64 GB DDR5 | 8192, leave the rest to the database |
How do I watch the cache in use?
Ask it for statistics over the local socket. A rising evictions count means the cache is too small for the working set; a low hit ratio means the application caches the wrong things or uses keys that are too short-lived.
printf 'stats\nquit\n' | nc 127.0.0.1 11211 | grep -E 'get_hits|get_misses|evictions|curr_connections|limit_maxbytes'
Raise -c, the connection limit, if listen_disabled_num grows; each idle connection costs little memory.
Does the same advice apply to Redis?
Largely yes. Redis also defaults to trusting local clients; current versions listen on localhost with protected mode on. Keep it that way, set a password with requirepass or ACL users, rename or disable dangerous commands, and reach it from other machines through a tunnel. For a wider view of services that should never face the internet directly, see Hardening a new dedicated server.
Machines for cache and application hosts
| Model | Processor | Cores | Memory | Drives | Port | Per month |
|---|---|---|---|---|---|---|
| ESSENTIAL-S | AMD Ryzen 7 9700X | 8 | 64 GB | 2x 512 GB SSD NVMe | 1 Gbps unmetered | $46.20 |
| ESSENTIAL-5 | AMD EPYC 7413 | 24 | 128 GB | 2x 960 GB SSD NVMe | 1 Gbps unmetered | $120.00 |
| BUSINESS-2 2024 | AMD EPYC 4344P | 8 | 64 GB | 2x 960 GB SSD NVMe | 3 Gbps unmetered | $109.80 |
| ESSENTIAL-8 | AMD EPYC 7532 | 32 | 256 GB | 2x 480 GB SSD SATA | 1 Gbps unmetered | $126.60 |
Related pages
Build an iptables firewall
A default-deny host firewall with iptables or nftables: allow SSH and web, rate-limit new connections, cover IPv6 and keep the rules across reboots.
Read moreCompany VPN gateways
Small, efficient machines for WireGuard and IPsec gateways that connect staff, offices and servers privately.
Read moreHardening a new dedicated server
The first hour on a fresh machine: SSH keys, a firewall, automatic updates, brute force blocking and a habit of checking what listens.
Read moreHigh-traffic websites
Machines with fast cores and guaranteed 3 to 5 Gbps ports for news sites, forums, APIs and busy content platforms.
Read moreQuestions and answers
Is Memcached safe to expose on a public IP?
No. It has no access control by default and UDP can be abused for amplification. Bind it to localhost or a private tunnel address.
How do I disable UDP in Memcached?
Add -U 0 to the configuration and restart. ss -lnup should then show no UDP socket on port 11211.
How can another server use my Memcached securely?
Connect the servers with a WireGuard tunnel, bind Memcached to the tunnel address and allow port 11211 only on that interface.
What does the -m option set?
The maximum cache size in megabytes. Memcached grows up to that size and then evicts the least recently used items.
Can my servers reach each other over a private link?
No. Servers connect over their public ports, so use an encrypted tunnel such as WireGuard for internal services.