Code
memcached
# --- 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.