Operating System CentOS

How to Install and Configure Memcached on AlmaLinux 9 or Rocky Linux 9

Install Memcached on AlmaLinux 9 or Rocky Linux 9, set memory and connection limits, keep it off the public network and test it.

Memcached is an in-memory key-value cache that web applications use to keep the results of database queries, sessions and rendered fragments close at hand. This guide installs Memcached on AlmaLinux 9 or Rocky Linux 9 (it applies equally to CentOS Stream 9 and RHEL 9), configures its memory and connection limits, keeps it off the public network, and shows how to check that it works.

CentOS 7 reached end of life on 30 June 2024 and no longer receives security updates. AlmaLinux 9 and Rocky Linux 9 are its direct successors; the package is still called memcached and is still configured through /etc/sysconfig/memcached, but it is a much newer release (the 1.6 series) with safer defaults.

Requirements

  • A server running AlmaLinux 9 or Rocky Linux 9.
  • A user with sudo rights.
  • Spare RAM: everything Memcached stores lives in memory, on top of what your application and database already use.

Step 1: Install Memcached

Memcached is in the AppStream repository. Install it together with the client tools from libmemcached-awesome, the maintained continuation of libmemcached that the 9 family ships:

sudo dnf update -y
sudo dnf install -y memcached libmemcached-awesome-tools

On systems with SELinux enabled, dnf also pulls in memcached-selinux, which contains the policy for the service. Start Memcached now and at every boot:

sudo systemctl enable --now memcached
sudo systemctl status memcached

Step 2: Review the configuration

The systemd unit reads its settings from one file:

sudo vi /etc/sysconfig/memcached

The packaged defaults are:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1,::1"
SettingMeaning
PORTTCP port to listen on (-p).
USERAccount the daemon runs as (-u).
MAXCONNMaximum simultaneous client connections (-c).
CACHESIZEMemory for stored items, in megabytes (-m).
OPTIONSAny further command-line options.

Two defaults differ from what older tutorials describe. The service already listens only on the loopback addresses 127.0.0.1 and ::1, so it is not reachable from the network after installation. And Memcached has shipped with its UDP listener switched off since version 1.5.6, so the old advice to add -U 0 against UDP amplification attacks is no longer needed; adding it does no harm.

Step 3: Tune memory and connections

64 MB is small for a production site. Raise CACHESIZE to the amount of RAM you can dedicate to the cache, and MAXCONN if you have many application workers. For example, 512 MB, 2,048 connections and a larger maximum item size of 2 MB (the default is 1 MB):

PORT="11211"
USER="memcached"
MAXCONN="2048"
CACHESIZE="512"
OPTIONS="-l 127.0.0.1,::1 -I 2m"

Apply the change. Restarting Memcached empties the cache, so applications will repopulate it on the next requests:

sudo systemctl restart memcached

Step 4: Verify that it works

Confirm the listening addresses. You should see TCP sockets on 127.0.0.1:11211 and [::1]:11211 and no UDP socket:

sudo ss -plunt | grep memcached

Query the server statistics with the tool from the libmemcached package, or with the script that comes with Memcached itself:

memcstat --servers=127.0.0.1
memcached-tool 127.0.0.1:11211 stats

Check limit_maxbytes (your CACHESIZE in bytes) and max_connections in the output. To test a write and a read by hand, install nmap-ncat and speak the text protocol:

sudo dnf install -y nmap-ncat
printf 'set greeting 0 60 5\r\nhello\r\nget greeting\r\nquit\r\n' | nc 127.0.0.1 11211

The reply is STORED followed by VALUE greeting 0 5, hello and END.

Step 5: Allow access from another server (optional)

If the application runs on the same machine, stop here: loopback access is the safest setup. Memcached has no authentication in its default configuration, so anyone who can reach the port can read and overwrite the cache. Only open it to other hosts over a private network, and only to the hosts that need it.

Add the server's private address to the listen list. In this example the Memcached server is 198.51.100.10 and the application server is 198.51.100.20:

OPTIONS="-l 127.0.0.1,::1,198.51.100.10"

Restart the service, then allow only the application server through firewalld:

sudo systemctl restart memcached
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.20/32" port port="11211" protocol="tcp" accept'
sudo firewall-cmd --reload

Never add port 11211 to a public zone without a source restriction. If traffic has to cross an untrusted network, the packaged build is compiled with SASL and TLS support; see memcached -h and the Memcached documentation for the -S and -Z options.

Step 6: Connect your application

Point the application's cache settings at 127.0.0.1, port 11211 (or at the private address from step 5). Each language needs its own client library, for example the memcached PECL extension for PHP or pymemcache for Python; install it the way you install your other application dependencies. Memcached itself needs no further configuration per application.

Troubleshooting

  • The service fails after changing PORT. SELinux only lets Memcached bind ports labelled memcache_port_t. Install policycoreutils-python-utils and label the new port, for example sudo semanage port -a -t memcache_port_t -p tcp 11311, then restart.
  • Find the reason for a failed start. Run sudo journalctl -u memcached -n 30; a typo in OPTIONS is the usual cause.
  • Items disappear sooner than expected. Look at evictions in the statistics. A growing number means the cache is full and Memcached is discarding the least recently used items; raise CACHESIZE.

Frequently asked questions

How much memory should I give Memcached?

Start with what you can spare after the database and application have their share, then watch evictions and the hit rate (get_hits against get_misses). If evictions stay at zero the cache is larger than it needs to be; if they climb steadily, add memory.

Does Memcached keep its data after a restart?

No. It is a cache, not a database: a restart or reboot starts with an empty store. Never keep data in it that cannot be rebuilt from another source.

Should I use Memcached or Redis?

Memcached is a simple, multithreaded cache for strings and serialised objects and is easy to operate. Redis and its forks add data structures, persistence and replication. If your application only needs a fast volatile cache and supports both, either works; pick the one your framework documents best.

Is the libmemcached package still available?

The original libmemcached is no longer developed. The 9 family ships libmemcached-awesome, a compatible continuation, with the command-line clients in libmemcached-awesome-tools.

Share story:

Continue reading other guides on How VPS.