Log inSign up

Add a Memcached object cache to your server

Run sudo apt install memcached on Ubuntu or Debian, sudo dnf install memcached on Rocky or Alma, or pkg install memcached on FreeBSD. Set the cache size with -m, keep the listener on 127.0.0.1 with UDP disabled, restart the service, and confirm it answers by sending a stats command to port 11211.

Topic: Databases · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9, FreeBSD 14 · Updated 2026-12-03

When is Memcached the right cache?

Choose Memcached when you want a simple, multi-threaded key and value cache that holds data only in memory and forgets it on restart. It suits page fragments, database query results and PHP sessions that you can rebuild at any time.

Memcached has no persistence, no replication and no data types beyond strings, which keeps it small and predictable. If you need lists, sorted sets, queues or data that survives a reboot, use Redis or Valkey instead. Many applications, including WordPress object cache plugins, Nextcloud and Django, support both, so the decision is easy to change later.

Before you begin, log in as a normal sudo user (see your first SSH session) and update the package lists. Memcached runs happily on the smallest plans because you decide exactly how much memory it may use.

How do you install Memcached on each system?

Every supported system has Memcached in its main repositories, so one package command and one service command are enough.

Ubuntu 24.04, 22.04 and Debian 12, 13

sudo apt update
sudo apt install -y memcached libmemcached-tools netcat-openbsd
sudo systemctl enable --now memcached
memcached -V

Rocky Linux 9 and AlmaLinux 9

sudo dnf install -y memcached nmap-ncat
sudo systemctl enable --now memcached
memcached -V

FreeBSD 14

sudo pkg install -y memcached
sudo sysrc memcached_enable=YES
sudo sysrc memcached_flags="-l 127.0.0.1 -m 128 -U 0"
sudo service memcached start

The service runs as the unprivileged memcache user on Debian family systems and memcached on the Enterprise Linux family.

Where do you change the settings?

On Ubuntu and Debian edit /etc/memcached.conf, where each line is one command-line flag. On Rocky and Alma edit /etc/sysconfig/memcached, which holds shell variables.

Ubuntu and Debian: /etc/memcached.conf

-d
logfile /var/log/memcached.log
-m 256
-p 11211
-u memcache
-l 127.0.0.1
-l ::1
-c 1024
-U 0

Rocky and Alma: /etc/sysconfig/memcached

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="256"
OPTIONS="-l 127.0.0.1,::1 -U 0"

The flags mean: -m memory in megabytes, -c the connection limit, -l listen addresses and -U 0 UDP off. Apply changes with a restart:

sudo systemctl restart memcached
sudo ss -ltnup | grep 11211

You should see TCP listeners on 127.0.0.1 and ::1 only, with no UDP line.

Why must Memcached stay off the public internet?

Memcached has no authentication in its default build, so anyone who can reach the port can read, change or wipe your cache. An open UDP port has also been abused to flood third parties with amplified traffic. Keeping the listener on loopback and UDP disabled closes both problems.

Add a firewall layer anyway, so a later config mistake cannot expose the service. On Ubuntu and Debian with ufw:

sudo ufw deny 11211
sudo ufw status

On Rocky and Alma, firewalld blocks ports that are not explicitly opened, so simply do not add 11211 to any zone:

sudo firewall-cmd --list-all

For an app server that needs a cache on a different machine, connect the two over WireGuard and bind Memcached to the tunnel address only, or use an SSH tunnel. The firewall tutorial explains a default-deny setup that makes this safe.

How do you use a Unix socket or SASL authentication?

Use a Unix socket when the application runs on the same server: it is slightly faster than TCP and file permissions decide who may connect. Replace the -l and -p lines with:

-s /run/memcached/memcached.sock
-a 0770

Create the directory through a systemd drop-in so it exists after every reboot:

sudo systemctl edit memcached
# add:
[Service]
RuntimeDirectory=memcached
RuntimeDirectoryMode=0755

Add your web server user to the Memcached group, for example sudo usermod -aG memcache www-data, then restart both services.

SASL adds a username and password to the binary protocol. It needs the -S flag, a SASL config and the binary protocol on the client side, and many clients do not support it, so treat it as an extra layer for shared networks rather than a reason to expose the port.

How do you check that the cache works?

Send plain text commands with netcat: store a value, read it back and look at the statistics.

printf 'set greeting 0 60 5\r\nhello\r\nget greeting\r\nquit\r\n' | nc 127.0.0.1 11211
printf 'stats\r\nquit\r\n' | nc 127.0.0.1 11211 | grep -E 'curr_items|get_hits|get_misses|limit_maxbytes'

The first command should print STORED and then the value. In the statistics, limit_maxbytes confirms the memory size you set, and the ratio of get_hits to get_misses tells you over time whether the cache is useful. A high evictions count means the cache is too small for the working set.

How do you connect PHP and Python applications?

Install the client extension or library and point it at 127.0.0.1:11211 or the socket path.

# PHP on Ubuntu and Debian
sudo apt install -y php-memcached
sudo systemctl restart php8.3-fpm     # match your PHP version
php -m | grep memcached
# Python in a virtual environment
python3 -m venv ~/venv && . ~/venv/bin/activate
pip install pymemcache

A short Python check:

python3 -c "from pymemcache.client.base import Client; c=Client(('127.0.0.1',11211)); c.set('k','v',expire=60); print(c.get('k'))"

To store PHP sessions in Memcached, set session.save_handler = memcached and session.save_path = "127.0.0.1:11211" in the FPM pool or php.ini. Remember that a restart empties the cache, which logs every user out. The LEMP stack guide shows where those PHP files live.

How do you size, upgrade and troubleshoot it?

Start with a cache that is a modest slice of RAM, watch evictions and hit rate for a week, then grow it if needed. On a 1 GB server, 64 to 128 MB is plenty for one small site; on 4 GB, 512 MB leaves room for the database and PHP.

Updates come with the normal package upgrade. The cache empties on restart, so schedule upgrades at a quiet time. When the service will not start, read the journal:

sudo journalctl -u memcached -n 40 --no-pager

Typical causes are a socket directory that does not exist, a duplicate -l address already in use, and a memory value larger than what is free. If clients time out, check curr_connections against the -c limit in the stats output. When the whole server is short on memory, deploy a bigger plan and move with this migration guide.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1cache for one small site1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A1-2cache plus PHP and a database1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months

Questions about Install Memcached

Does Memcached keep data after a restart?

No. Everything lives in RAM and disappears when the service or server restarts. Applications must treat it as a cache and rebuild missing entries from the database.

What port does Memcached use?

TCP 11211 by default. UDP on the same port is off by default in current releases; keep -U 0 in the config so it stays off.

Is Memcached or Redis faster?

For simple get and set calls on one server both answer in well under a millisecond, so speed rarely decides. Pick Memcached for a plain multi-threaded cache and Redis or Valkey when you need data types or persistence.

How do I flush the whole cache?

Send flush_all: printf 'flush_all\r\nquit\r\n' | nc 127.0.0.1 11211. Items become invalid at once; a service restart has the same effect.

Can several sites share one Memcached instance?

Yes, as long as each site uses its own key prefix. For strict separation, run a second instance on another port or socket with its own memory limit.