Redis vs Memcached: Data Types, Memory Management, Persistence, and Clustering Comparison
This article compares Redis and Memcached across data‑type support, memory‑usage efficiency, performance, memory‑management mechanisms, persistence options, and clustering capabilities, highlighting the strengths and trade‑offs of each in‑memory data store.
Redis author Salvatore Sanfilippo compared the two in‑memory data storage systems.
Redis supports server‑side data operations: it offers richer data structures and commands, allowing complex modifications without round‑trip to the client, which reduces network I/O compared with Memcached.
Memory‑usage efficiency: for simple key‑value pairs Memcached uses memory more efficiently, but Redis can achieve higher efficiency when using hash structures due to its internal compression.
Performance: Redis runs on a single core and outperforms Memcached per core for small values, while Memcached scales across multiple cores and is faster for large datasets (over 100 KB).
Below are the detailed reasons for these conclusions.
1. Different data‑type support
Unlike Memcached’s simple key‑value model, Redis provides five primary data types: String, Hash, List, Set and Sorted Set. Internally each key/value is represented by a redisObject that stores type, encoding and other metadata.
String
Common commands: SET , GET , INCR , DECR , MGET etc.
Use case: generic key/value storage.
Implementation: stored as a raw string; when numeric operations are performed the encoding switches to int .
Hash
Common commands: HGET , HSET , HGETALL etc.
Use case: storing objects such as a user profile (ID, name, age, birthday) where fields can be accessed individually.
Implementation: internally a hash map; for few fields Redis uses a compact ziplist representation, switching to a real hash table when the number of entries grows.
List
Common commands: LPUSH , RPUSH , LPOP , RPOP , LRANGE etc.
Use case: ordered collections such as follower or timeline lists.
Implementation: a doubly‑linked list, which allows fast insertion/removal at both ends but incurs some memory overhead.
Set
Common commands: SADD , SPOP , SMEMBERS , SUNION etc.
Use case: unordered collections that must be unique; also provides constant‑time membership checks.
Implementation: a hash table where the value is always NULL , enabling fast deduplication.
Sorted Set
Common commands: ZADD , ZRANGE , ZREM , ZCARD etc.
Use case: ordered, unique collections where each member has a score (e.g., timeline sorted by timestamp).
Implementation: combines a hash map (member → score) with a skip‑list to maintain ordering efficiently.
2. Different memory‑management mechanisms
Redis can swap rarely‑used values to disk when memory is exhausted, freeing RAM for new keys. It uses a custom allocator (zmalloc) that stores the allocation size in a header before the returned pointer. Memcached relies on a slab allocation scheme that pre‑divides memory into fixed‑size chunks, eliminating fragmentation but causing internal waste.
3. Persistence support
Redis offers two persistence options: RDB snapshots (fork‑based copy‑on‑write) and AOF (append‑only file) with configurable appendfsync policies (no, everysec, always). Memcached provides no persistence.
4. Cluster management differences
Memcached does not provide native clustering; clients must implement consistent hashing. Redis includes built‑in clustering (Redis Cluster) that shards the keyspace into 4096 slots, supports master‑slave replication, and can tolerate single‑node failures.
References: Redis Documentation , Memcached .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
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.
