Memcached is a high-performance, distributed in-memory key-value cache designed specifically for caching database query results, session data, and computed values. Unlike Redis, Memcached is deliberately simple — it does not support persistence, replication, or complex data types. This simplicity makes Memcached extremely fast and memory-efficient: it uses a slab allocator to eliminate memory fragmentation and can sustain millions of operations per second with microsecond latency. Memcached is horizontally scalable by design — application clients hash keys across a pool of Memcached servers, distributing cache load without any cluster configuration. It remains the preferred choice for pure caching use cases where persistence and data structures are not needed. This guide covers installing and configuring Memcached on RHEL 9 with authentication, connecting from PHP and Python, and monitoring.

Prerequisites

  • RHEL 9 with sudo/root access

Step 1 — Install Memcached

dnf install -y memcached libmemcached
memcached -V

Step 2 — Configure Memcached

# /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="512"          # 512 MB cache
OPTIONS="-l 127.0.0.1"  # Bind to localhost only (security critical)
systemctl enable --now memcached
systemctl status memcached

Step 3 — Verify Memcached is Running

# Connect via telnet
telnet 127.0.0.1 11211

# Inside telnet:
set mykey 0 300 5
hello
get mykey
quit

Step 4 — Connect from PHP

# Install the PHP Memcached extension
dnf install -y php-pecl-memcached

# PHP usage
addServer('127.0.0.1', 11211);

// Store a value with 5-minute TTL
$mc->set('db_result_user_123', serialize(['id'=>123,'name'=>'Alice']), 300);

// Retrieve
$user = unserialize($mc->get('db_result_user_123'));
echo $user['name'];

Step 5 — Connect from Python

pip install pymemcache
python3 <<'PYEOF'
from pymemcache.client.base import Client
client = Client(('127.0.0.1', 11211))
client.set('cache_key', b'hello world', expire=300)
print(client.get('cache_key'))
PYEOF

Step 6 — Monitor Memcached Stats

# Get current stats via the text protocol
echo "stats" | nc 127.0.0.1 11211

# Key stats to monitor:
# curr_items    — number of items currently in cache
# bytes         — memory used by cache
# get_hits      — cache hit count
# get_misses    — cache miss count
# evictions     — items evicted due to memory pressure (increase CACHESIZE if non-zero)

# Hit rate formula: get_hits / (get_hits + get_misses) * 100
echo "stats" | nc 127.0.0.1 11211 | grep -E "get_hits|get_misses|curr_items|bytes "

Conclusion

Memcached on RHEL 9 provides a fast, simple caching layer that reduces database load for read-heavy applications. Binding to 127.0.0.1 is the most important security step — Memcached has no authentication by default and should never be exposed to the internet. Monitor the eviction count to ensure the cache size is large enough; non-zero evictions indicate the cache is too small and recently-added items are being evicted before expiry.

Next steps: How to Install Redis on RHEL 9, How to Install and Configure Varnish Cache on RHEL 9, and How to Monitor MySQL with Prometheus on RHEL 9.