Text vs Binary Protocol & Consistent Hashing
Pick the right protocol and shard across servers without reshuffling.
Code
# --- Text protocol (default port 11211) ---
# Human-readable, telnet-friendly, slightly more overhead
set foo 0 0 3
bar
# STORED
# --- Binary protocol (request opcodes 0x01 set, 0x00 get, ...) ---
# Lower overhead, supports SASL auth, required for some clients
# Enable in client config:
# pymemcache: Client(host, allow_unicode_keys=True, default_noreply=False,
# use_encoding=True)
# libmemcached: MEMCACHED_BEHAVIOR_BINARY_PROTOCOL = 1
# --- Cluster: consistent hashing ---
# Memcached servers don't talk to each other — the CLIENT shards keys.
# Ketama consistent hashing minimizes remapping when servers are added/removed.
# Python (pymemcache with consistent hashing)
from pymemcache.client.hash import HashClient
client = HashClient(
[("cache1.local", 11211), ("cache2.local", 11211), ("cache3.local", 11211)],
use_pooling=True,
hashclient_ketama=True, # consistent hashing (Ketama)
retry_attempts=2,
retry_timeout=1,
dead_timeout=30, # mark failed server dead for 30s
)
client.set("user:42", b"alice", expire=3600)
# Key rule: hash is on the WHOLE key — prefix like "user:" does NOT
# co-locate on one server. Keep keys short and uniform.Explanation
The text protocol is human-readable and easy to debug via telnet/nc; the binary protocol is more efficient, supports SASL auth and quieter noreply semantics — production clients often prefer binary. Memcached is distributed only at the client: servers are independent, no replication, no cluster coordination. Clients use consistent hashing (Ketama) so adding/removing a server only remaps ~1/N of keys rather than everything. Failover is silent: a dead server is marked down for dead_timeout and the client routes to alternates — but writes during the outage are lost (Memcached is a cache, not a store). Keep keys short (<250 chars), avoid spaces, and remember the hash is over the full key.
More Memcached Snippets
set / get — Basic Key-Value
Store and retrieve values by key with expiration and flags.
add / replace / append / prepend
Conditional writes and in-place string concatenation.
cas / gets — Compare-And-Swap
Optimistic locking: update a key only if it hasn't changed since you read it.
stats — Server Metrics & Slabs
Inspect memory, hit rate, evictions and per-slab allocation.
flush_all — Invalidate Everything
Logically invalidate all keys instantly (lazy deletion).
Expiration, Eviction & TTL Strategy
Choose TTLs wisely and understand how Memcached evicts under pressure.