Memcached is a high-performance, distributed memory object caching system used to speed up dynamic web applications by reducing database load. On RHEL 8, Memcached is available directly from the AppStream repository, making installation straightforward. In this tutorial you will install Memcached, tune its configuration, restrict network access to trusted hosts only, and verify the setup with basic telnet commands and client code examples. Never expose Memcached publicly — it has no built-in authentication and attackers can read or poison cached data.
Prerequisites
- RHEL 8 server with a non-root sudo user
- Active Red Hat subscription or configured EPEL 8 repository
firewalldrunning (systemctl status firewalld)- PHP 7.4+ or Python 3.6+ if you plan to use client examples
Step 1 — Install Memcached
Memcached is available in the default RHEL 8 AppStream repository. Install it with a single dnf command:
sudo dnf install -y memcached
Confirm the installed version:
memcached --version
Step 2 — Configure Memcached
The main configuration file is /etc/sysconfig/memcached. Open it for editing:
sudo vi /etc/sysconfig/memcached
Set the following values to match your workload. CACHESIZE is in megabytes; adjust it to roughly one-quarter of your available RAM:
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="256"
OPTIONS="-l 127.0.0.1"
The -l 127.0.0.1 option binds Memcached to the loopback interface only. If your application servers are on separate hosts you should use a private network interface IP instead of 0.0.0.0.
Step 3 — Enable and Start the Service
sudo systemctl enable --now memcached
sudo systemctl status memcached
You should see active (running) in the output.
Step 4 — Restrict Access with firewalld
Memcached must never be exposed to the public internet. If application servers access Memcached over the network, allow only their specific IP addresses and deny everything else:
# Allow only a specific trusted application server IP
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.50/32" port port="11211" protocol="tcp" accept'
# Reload to apply
sudo firewall-cmd --reload
# Verify the rule was added
sudo firewall-cmd --list-rich-rules
Do not open port 11211 globally. If Memcached is bound to 127.0.0.1 and all applications run on the same host, no firewall rule is necessary.
Step 5 — Test with Telnet
Install telnet if needed and run a quick functional test:
sudo dnf install -y telnet
telnet localhost 11211
Once connected, run these commands manually:
stats
set testkey 0 120 5
hello
get testkey
quit
A successful get testkey response returns VALUE testkey 0 5 followed by hello, confirming the cache is reading and writing correctly.
Step 6 — Connect from PHP and Python
Install the PHP Memcached extension:
sudo dnf install -y php-pecl-memcached
Basic PHP usage:
addServer('127.0.0.1', 11211);
$mc->set('greeting', 'Hello RHEL 8', 300);
echo $mc->get('greeting');
?>
Install the Python client and test from a script:
pip3 install pymemcache
from pymemcache.client.base import Client
client = Client(('127.0.0.1', 11211))
client.set('greeting', 'Hello RHEL 8', expire=300)
print(client.get('greeting'))
Conclusion
You have installed Memcached on RHEL 8, tuned its memory and connection limits, bound it to a safe interface, restricted network access with firewalld rich rules, and verified caching operations from both the command line and application code. Your database servers will see significantly reduced query load for frequently accessed data.
Next steps: Scaling Memcached with Consistent Hashing, Securing Memcached with SASL Authentication, and Monitoring Memcached with Prometheus.