Memcached Tutorial: Learn Distributed Caching from Scratch (2026)
I have used Memcached as a caching layer for high-traffic web applications, and its simplicity is its superpower. Memcached is a distributed memory object caching system storing key-value pairs in RAM.
We will cover installation, protocol and commands, eviction policies, client libraries, production patterns, and monitoring.
Installing Memcached and Understanding the Architecture
apt install memcached on Ubuntu. Default config: port 11211, 64 MB RAM, 4 threads. Tune with -m (memory), -t (threads), -I (max item size, default 1 MB).
Slab allocator divides memory into classes of different chunk sizes (64B to 1MB). Eliminates fragmentation but unused classes waste memory.
sudo apt update && sudo apt install memcached -y
memcached -d -m 2048 -t 8 -I 2m -p 11211 -l 0.0.0.0 -c 1024
echo "stats" | nc localhost 11211
Core Commands: Set, Get, Delete, and Expiry
Text-based protocol. SET stores key-value with expiration. GET retrieves one or more keys. DELETE removes. INCR/DECR atomically modify numeric values.
Expiration in seconds. Values up to 30 days are relative; beyond 30 days are Unix timestamps. 0 means no expiration.
set user:alice 0 3600 5
Hello
STORED
get user:alice
VALUE user:alice 0 5
Hello
END
Eviction Policies: LRU and the Slab Allocator
LRU per slab class when memory runs out. Small-item slab can evict before large-item slab with free memory.
-o modern enables automove algorithm rebalancing between slab classes. Monitor evictions with stats command.
echo "stats" | nc localhost 11211 | grep evictions
echo "lru_automove 1" | nc localhost 11211
memcached -I 4m -m 4096
Client Libraries: Node.js, Python, and Go
ASCII protocol is simple enough to implement, but established libraries handle pooling, serialization, and consistent hashing.
Libraries support connection pooling, Ketama consistent hashing, and automatic failover to minimize key redistribution.
const Memcached = require('memcached');
const client = new Memcached(['localhost:11211']);
client.set('user:alice', { name: 'Alice' }, 3600, (err) => {
client.get('user:alice', (err, data) => { console.log(data); });
});
Production Patterns: Cache-Aside, Thundering Herd, Stale Sets
Cache-aside: on miss, load from DB, store in cache. Vulnerable to thundering herd when a key expires.
Prevent with mutex locking: only one request hits DB. Stale sets serve expired data while async refreshing for high-traffic keys.
async function getCached(key, ttl, fetchFn) {
let value = await client.get(key);
if (value !== undefined) return value;
// mutex lock
value = await fetchFn();
await client.set(key, value, ttl);
return value;
}
Monitoring, Stats, and Scaling Memcached
Stats provides curr_items, total_items, evictions, get_hits/misses, bytes. Hit ratio >85% is well-configured.
Client-side sharding with consistent hashing. Add servers for more memory and throughput. No replication—server failure loses data.
echo "stats" | nc localhost 11211
stats slabs
stats items
Frequently Asked Questions
Is Memcached free?
Yes, Memcached is free and open-source under BSD license.
What is the maximum key size?
Maximum key size is 250 bytes. Maximum value size defaults to 1 MB, configurable up to 128 MB with -I.
Does Memcached persist data?
No. Memcached is purely in-memory. There is no disk persistence. Restarting loses all data. Use Redis if persistence is needed.
How does Memcached handle security?
Memcached has no authentication by default. Use firewall rules, SASL, or run on localhost. Never expose to the public internet.
Originally published on Ayodhyyya. Last updated June 1, 2026.