Redis vs Memcached: Which In‑Memory Store Wins for Your Needs?
This article compares Redis and Memcached across data‑type support, memory management, persistence mechanisms, and clustering features, highlighting their architectural differences, performance trade‑offs, and suitable use‑cases to help developers choose the right in‑memory storage solution.
Data Type Support
Redis provides five native data structures—String, Hash, List, Set, and Sorted Set—each with dedicated commands and typical application scenarios, whereas Memcached only supports simple key‑value pairs.
String : commands SET, GET, INCR, DECR, etc.; used for generic key/value storage.
Hash : commands HGET, HSET, HGETALL; ideal for storing objects such as user profiles where fields can be accessed individually.
List : commands LPUSH, RPUSH, LPOP, LRANGE; useful for ordered collections like timelines or queues.
Set : commands SADD, SPOP, SMEMBERS; provides automatic deduplication and membership tests.
Sorted Set : commands ZADD, ZRANGE, ZREM; stores unique members with a score, enabling ordered, non‑duplicate collections such as leaderboards.
Memory Management Mechanisms
Redis stores all values in memory but can swap rarely used values to disk when memory pressure exceeds a threshold, using a calculated "swappability" metric (age × log(size_in_memory)). This swap operation may block the main thread, so configuring an I/O thread pool is recommended for high‑concurrency workloads.
Memcached employs a slab allocation scheme: memory is divided into fixed‑size chunks grouped into slab classes, eliminating fragmentation but potentially wasting space when stored objects do not exactly fit the chunk size.
Persistence Support
Redis offers two persistence strategies:
RDB snapshots : Periodic fork‑based copy‑on‑write snapshots written to disk; configurable by time or write count. Snapshots are atomic but may lose data since the last snapshot.
AOF (Append‑Only File) : Logs every write command; can be rewritten to compact the file. The appendfsync setting controls durability (options: no, everysec, always).
Memcached does not provide any data‑persistence mechanism.
Cluster Management
Memcached lacks native clustering; distributed caching is achieved client‑side via consistent hashing, requiring the client to locate the appropriate node for each key.
Redis includes built‑in clustering (Redis Cluster). It partitions the key space into 4096 hash slots, distributes slots across up to 4096 nodes, and provides automatic failover with master‑slave replication (each master has two slaves). The cluster uses a simple CRC16 hash to map keys to slots.
References
http://www.redisdoc.com/en/latest/
http://memcached.org/
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
