Memcached
1.6.xSummary
In-memory key-value cache with multi-threaded architecture and client-side sharding. No persistence, no replication, no pub/sub, no data structures beyond binary blobs. Designed for maximum single-operation throughput on commodity hardware, with horizontal scaling handled entirely by the client's consistent hashing across nodes.
Primary Use Case
Read-heavy workloads requiring a high-throughput cache layer in front of a relational or document database. Session storage, page fragment caching, and database query result caching where the data structure is a serialized blob and cache invalidation is the consistency mechanism.
Workload Fit
Strengths
Best for
- ·High-throughput read-heavy workloads where maximum single-operation cache throughput is the priority and Redis data structures are not needed
- ·Session storage where loss of cached sessions on node failure is acceptable (or sessions are replicated at the application layer)
- ·Database query result caching where the application controls invalidation and cache-aside is the consistency model
- ·Page fragment caching for rendered HTML or API responses where the blob size fits within 1MB
- ·Environments where operational simplicity matters: Memcached has no persistence, no replication, and almost no configuration surface compared to Redis
Excels when
- ·All cached values are simple serialized blobs and Redis data structures (lists, sets, sorted sets, hashes) are not needed
- ·Cache is purely ephemeral: there is no data that must survive node restarts
- ·Multi-threaded throughput is the bottleneck and Redis's single-threaded event loop is a measured limitation
- ·Client-side sharding is acceptable and the client library used by all application services is consistent
Architectural advantages
- ·Multi-threaded architecture scales write and read throughput with CPU core count: unlike Redis's single-threaded event loop, Memcached scales linearly on multi-core hardware for simple get/set operations
- ·No coordination overhead between nodes: each Memcached instance is fully independent, eliminating consensus overhead and simplifying cluster topology
- ·Facebook's open-source mcrouter provides production-grade request routing, connection pooling, prefix-based routing, and replication for Memcached at extreme scale
- ·Extremely simple operational model: no persistence files, no replication slots, no AOF logs, no cluster bus
When to Avoid
Avoid when
- ·Workload requires data structures (sorted sets, lists, pub/sub, streams): use Redis
- ·Cache data must survive node restarts or process crashes: Memcached is purely volatile; use Redis with AOF/RDB persistence
- ·Replication is required for high availability of cached data: Memcached has no built-in replication; clients must implement their own dual-write logic
- ·Value sizes frequently exceed 1MB: the 1MB limit is a hard default requiring patch or alternative
Common misuses
- ·Using Memcached as a primary data store for values that are not also in a durable backing store: node failure silently destroys all cached data with no recovery path
- ·Relying on Memcached CAS for distributed locking: CAS is not a reliable distributed lock primitive; use Redis SETNX/EXPIRE or a dedicated lock service
- ·Treating cache hit rate as a single metric: monitoring eviction rate per slab class and miss rate per key namespace identifies which specific data is not being cached effectively
Consistency & Transactions
Scaling
Read scalability
Scales by adding nodes; clients use consistent hashing to distribute keys across the pool. Adding a node causes a fraction of keys (proportional to 1/N) to rehash to the new node: a cold cache miss wave occurs on the keys that move. This is a known operational event that must be managed.
Write scalability
Writes (set/add/replace/delete) are distributed across nodes by client-side hashing. Each node is fully independent: no replication, no coordination. Write throughput scales linearly with node count. Node loss causes all cached values on that node to be immediately unavailable.
Failure Behavior
Known failure modes
- ·Node failure causes a cold cache thundering herd: all requests for keys on the lost node simultaneously miss and hit the backing store: the backing store must handle the full uncached traffic volume
- ·Slab allocator fragmentation: Memcached allocates memory in fixed-size slabs; if value sizes change over the application lifetime, pre-allocated slabs for old sizes cannot be reclaimed without restarting the process
- ·Client-side consistent hash ring inconsistency: if different application instances use different library versions or configurations, the same key hashes to different nodes: silent dual-write and stale-read bugs
- ·Connection exhaustion: Memcached holds one OS thread per client connection by default; at very high client counts (>10,000 connections), thread scheduling overhead degrades throughput
- ·Silent eviction under memory pressure: when the LRU pool is full, items are silently evicted without notification; applications that assume cache presence will re-fetch from the backing store without knowing eviction occurred
- ·Value size limit: the default maximum value size is 1MB; storing larger objects (serialized blobs, large HTML fragments) silently fails with NOT_STORED without an error visible to the caller
Bottlenecks
- ·Node failure thundering herd: losing a node floods the backing store with the full uncached request rate for all keys on that node simultaneously
- ·Slab fragmentation: if value sizes are heterogeneous, memory allocated to large-value slabs cannot be reused for small-value slabs: memory efficiency degrades without a restart
- ·Client connection scaling: thread-per-connection model does not scale beyond a few thousand concurrent clients without significant memory overhead from thread stacks
- ·Single-machine memory cap: each node is bounded by its configured -m value; cluster capacity scales by adding nodes but each node is a hard memory boundary
- ·Consistent hash ring stability: any ring topology change (add/remove node) causes a cold miss wave proportional to the keys that rehash to a new node
Degradation patterns
- ·LRU eviction churn: when the working set exceeds pool capacity, high-value items are evicted to make room for less-valuable items: cache effectiveness degrades continuously without pool resizing
- ·Post-deployment cache miss wave: a new application deployment that changes serialization format invalidates all cached values that use the old format: backing store absorbs full traffic until the cache warms
- ·Network saturation: very high get throughput on large values can saturate the NIC on a Memcached node; monitor network bytes/s alongside operation count/s
Recovery considerations
- ·Node failure requires no coordinated recovery action: the client's consistent hash ring automatically redirects requests to the remaining nodes after a configurable timeout
- ·Warming a new or restarted Memcached node requires time for the cache to fill from backing store misses; provisioning a replacement node during a traffic spike is dangerous
- ·If using mcrouter with replication groups, a secondary node can serve reads while the primary is down; replication must be configured before failure occurs
Operational Pitfalls
- ·Not instrumenting cache hit rate per key prefix: a site-wide hit rate hides cold buckets where specific key namespaces have low hit rates, causing concentrated backing store load
- ·Adding a node to a consistent hash ring during a traffic spike: a ring topology change causes a cold miss wave on rehashed keys; schedule node additions during low-traffic periods
- ·Using Memcached for data that requires atomic compare-and-swap or versioned updates: Memcached's CAS operation is limited and not a substitute for transactional consistency
- ·Not sizing the memory pool based on working set analysis: oversizing wastes RAM, undersizing causes high eviction rates; monitor eviction stats per slab class to right-size
- ·Using Memcached for session storage without a replication strategy: a node failure evicts all sessions on that node; users are logged out without warning
Architecture Guidance
Common topology roles
Migration notes
- ·To Redis: most Memcached use cases are satisfied by Redis string operations; migration is typically straightforward since the client API is similar: benefit is Redis persistence, data structures, and built-in replication
- ·From Redis to Memcached: justified only when single-operation throughput benchmarks show Redis's single-threaded model as a bottleneck at the given hardware spec
- ·Cluster topology changes require coordinated client-side consistent hash ring updates across all application instances; use a library with identical hashing behavior to avoid key misrouting
Advisor Guidance
When: scenario requires cache high availability or cache data durability across restarts
Memcached has no persistence or replication; use Redis with AOF persistence and sentinel-based failover if cache data must survive node failure
When: scenario uses Memcached with more than 500 application instances connecting directly
Deploy mcrouter to multiplex application connections into a smaller pool of persistent connections to Memcached; direct connections at scale exhaust Memcached's thread-per-connection model
When: scenario has read_heavy workload with cache hit rate requirements above 95%
Monitor eviction rate and miss rate per key namespace; size the memory pool to hold the full hot working set to avoid LRU eviction of frequently-accessed keys
Comparison Factors
raw throughput
Highest single-operation throughput in the cache category: multi-threaded architecture saturates multi-core hardware without the Redis single-thread bottleneck
operational simplicity
Very low operational surface: no persistence, no replication, no pub/sub, no cluster coordination; easiest cache to run in production
feature richness
Minimal: binary blob storage, LRU eviction, CAS; no data structures, persistence, pub/sub, scripting, or streams compared to Redis
fault tolerance
Low: node failure causes immediate cache loss for all keys on that node with no automatic recovery; thundering herd against backing store is the primary risk
Managed Cloud Options
Enables Patterns
Basis
Memcached's architecture is simple and well-documented; Facebook's published mcrouter architecture validates the at-scale operational model; failure modes are empirically understood