Memcached on Linux: Production Caching for Web Applications
- Author :Liam K.
- Date :July 03, 2026
- Time :23 minutes
Memcached is a high-performance, distributed memory caching system used by Facebook, Wikipedia, and thousands of web applications to reduce database load. It stores simple key-value pairs in RAM with sub-millisecond access times. While Redis/Valkey has largely replaced Memcached for new projects that need data structures and persistence, Memcached remains an excellent choice for pure caching workloads — especially PHP session storage and HTML fragment caching.
This guide installs Memcached on Ubuntu or Debian, configures memory limits and connection pools, enables SASL authentication, integrates with PHP and Python applications, and sets up monitoring for cache hit rates. The patterns here apply to any Linux VPS or dedicated server running a read-heavy web application.
Memcached vs Redis/Valkey
- Memcached — multithreaded, simple strings only, LRU eviction, lowest latency for pure cache.
- Redis/Valkey — rich data types, persistence, pub/sub, replication, Lua scripting.
- Choose Memcached when you need a dumb, fast cache with minimal memory overhead.
- Choose Redis/Valkey when you need queues, pub/sub, persistence, or complex data structures.
Step 1: Install Memcached
sudo apt update
sudo apt install -y memcached libmemcached-tools
memcached -h
sudo systemctl enable memcachedStep 2: Configure Production Settings
Edit /etc/memcached.conf to set memory allocation, bind address, connection limits, and item size. Never bind to 0.0.0.0 without authentication on a public server.
sudo tee /etc/memcached.conf >/dev/null <<'EOF'
# Memory allocation (MB) — leave headroom for OS
-m 1024
# Bind to localhost only (use private IP for multi-server)
-l 127.0.0.1
# Max simultaneous connections
-c 2048
# Max item size (default 1MB, increase for large HTML fragments)
[...]Step 3: Verify Installation
echo "stats" | nc 127.0.0.1 11211
echo "version" | nc 127.0.0.1 11211
# Set and get a test value:
echo -e "set test_key 0 60 5\r\nhello\r\n" | nc 127.0.0.1 11211
echo "get test_key" | nc 127.0.0.1 11211Step 4: Enable SASL Authentication
When Memcached must be reachable from other servers on a private network, enable SASL to require username/password authentication.
sudo apt install -y sasl2-bin
# Create SASL user:
sudo saslpasswd2 -a memcached -c cacheuser
# Enter password when prompted
# Enable SASL in memcached.conf:
# -S
sudo tee -a /etc/memcached.conf >/dev/null <<'EOF'
-S
[...]Step 5: PHP Integration (Session Storage)
sudo apt install -y php8.3-memcached
# /etc/php/8.3/fpm/conf.d/50-memcached-sessions.ini
session.save_handler = memcached
session.save_path = "127.0.0.1:11211"
sudo systemctl reload php8.3-fpm
# Application-level caching:
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', 11211);
[...]Step 6: Python Integration
pip install pymemcache
from pymemcache.client.base import Client
client = Client(('127.0.0.1', 11211))
client.set('api:users', json.dumps(users), expire=300)
cached = client.get('api:users')Step 7: Node.js Integration
npm install memcached
const Memcached = require('memcached');
const memcached = new Memcached('127.0.0.1:11211');
memcached.set('page:home', htmlContent, 600, (err) => {
if (err) console.error(err);
});
memcached.get('page:home', (err, data) => {
if (data) return res.send(data);
[...]Step 8: Monitor Cache Performance
# Key stats to watch:
echo "stats" | nc 127.0.0.1 11211 | grep -E 'curr_items|total_items|evictions|bytes|cmd_get|cmd_set|get_hits|get_misses'
# Calculate hit rate:
# hit_rate = get_hits / (get_hits + get_misses)
# Target: > 90% for effective caching
# Watch in real time:
watch -n 5 'echo stats | nc 127.0.0.1 11211 | grep -E "get_hits|get_misses|evictions|curr_items"'Caching Patterns
- Cache-aside — app checks cache first, loads from DB on miss, writes to cache.
- Session storage — PHP/Java sessions in Memcached for multi-server deployments.
- HTML fragment cache — cache rendered partials with short TTL (60–300s).
- API response cache — cache JSON responses keyed by URL + query params.
Troubleshooting
- High evictions — increase
-mmemory or reduce TTLs. - Low hit rate — keys may be too granular or TTLs too short.
- Connection refused — check bind address and firewall rules for port 11211.
- Item too large — increase
-Imax item size.
Production Checklist
- Bind to localhost or private IP — never expose 11211 publicly.
- Enable SASL when accessible from multiple servers.
- Monitor hit rate and evictions — alert on hit rate below 80%.
- Set TTLs on every cached item — unbounded caches cause evictions.
- Plan memory at 10–20% of database working set size as a starting point.
"Memcached does one thing — cache strings in RAM — and does it faster than anything else. Use it when that is all you need."
Technical Author

System administrator and technical writer specializing in server infrastructure, security and deployment. Creating comprehensive guides to help you master server administration.