Redis vs Memcached: Which In‑Memory Store Wins on Performance and Features?
Redis and Memcached are compared across network I/O models, supported data structures, memory management, persistence, consistency, and clustering, revealing Redis’s richer feature set and flexibility while highlighting Memcached’s simplicity and speed, helping developers choose the right in‑memory solution for their workloads.
Network I/O Model
Memcached uses a multithreaded, non‑blocking I/O multiplexing model with a listening thread and worker threads; it relies on libevent and incurs lock contention on global structures such as the
statscommand. Redis employs a single‑threaded event‑loop (AeEvent) that wraps epoll/kqueue/select, achieving maximum throughput for pure I/O but can become a bottleneck when CPU‑intensive commands (e.g., sorting, aggregation) block the event loop.
Supported Data Types
Memcached stores only simple key‑value pairs in a large hash table, offering O(1) lookup. Redis supports additional data structures such as list, set, sorted set (zset), and hash, providing richer functionality for a variety of use cases.
Reference: http://blog.csdn.net/u013256816/article/details/51133134
Memory Management Mechanism
Both systems avoid raw
malloc/freedue to fragmentation and overhead. Memcached adopts a slab allocation scheme: memory is pre‑allocated in chunks of fixed sizes (slabs) to eliminate fragmentation, though this can waste space when stored objects do not exactly match chunk sizes.
When a request arrives, Memcached selects the smallest suitable slab class, finds a free chunk, and stores the item. Expired or evicted items free their chunks for reuse.
Redis implements its own allocator (zmalloc) that records the size of each allocation in a header before the returned pointer. This enables easy calculation of the original pointer for
freeand tracking of total used memory via a static
used_memoryvariable.
Redis also maintains an array
zmalloc_allocationsindexed by allocation size, counting how many blocks of each size are allocated.
Data Storage and Persistence
Memcached does not provide persistence; all data resides purely in memory. Redis offers two persistence options: snapshotting (RDB) writes the entire dataset to disk at intervals, and Append‑Only File (AOF) logs every write operation for replay.
Data Consistency
Memcached provides a
cascommand to handle compare‑and‑swap semantics for concurrent updates. Redis lacks
casbut supports transactions (MULTI/EXEC) to guarantee atomic execution of a sequence of commands.
Cluster Management Differences
Memcached is a pure in‑memory cache without built‑in clustering; distributed deployment relies on client‑side consistent hashing. Redis supports native clustering (Redis Cluster) with sharding across up to 4096 hash slots, master‑slave replication for high availability, and automatic failover.
Source: 朱小厮, blog.csdn.net/u013256816/article/details/51146314
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.