Redis vs Memcached: A Comprehensive Comparison
This article systematically compares Redis and Memcached across thread models, data structures, eviction policies, pipelining and transactions, persistence, high availability, and clustering, helping developers choose the most suitable in‑memory database for their specific business requirements.
Introduction
Redis and Memcached are both in‑memory databases known for extremely fast access speeds, but choosing between them can be confusing. This article compares them from multiple angles to help you select the best fit for your workload.
Comparison Dimensions
Thread model
Data structures
Eviction policy
Pipelining and transactions
Persistence
High availability
Clustering
Thread Model
Memcached uses a multithreaded model with I/O multiplexing; the main thread accepts requests and distributes them to worker threads, which avoids blocking but incurs context‑switch overhead and lock contention.
Redis also uses I/O multiplexing but processes requests in a single thread, eliminating context switches and lock contention, though a long‑running command blocks the whole server. Redis 6.0 adds optional multithreading for network I/O, improving performance.
Data Structures
Memcached only supports simple string values (max 1 MB) and requires manual serialization, offering a "store‑as‑is" model.
Redis provides a rich set of native data types: string, list, hash, set, sorted set, geo, and HyperLogLog, enabling flexible operations without extra serialization.
Eviction Policies
Memcached enforces a global memory limit and uses an LRU algorithm, which can evict recently written keys due to its memory‑management design.
Redis does not require a fixed memory cap and offers multiple eviction strategies, including volatile‑lru, allkeys‑lru, volatile‑random, allkeys‑random, volatile‑ttl, volatile‑lfu, and allkeys‑lfu, allowing you to tailor eviction to your workload.
Pipelining and Transactions
Redis supports pipelining, allowing a client to batch multiple commands in a single network round‑trip, reducing latency.
Redis also provides a lightweight transaction model (different from strict RDBMS transactions) that works together with pipelining to ensure ordered execution and abort on conflicting key modifications.
Persistence
Memcached does not persist data; a crash results in total data loss.
Redis offers two persistence mechanisms: RDB snapshots (full dumps) and AOF (append‑only file) for incremental logging, which can be combined to maximize data safety.
High Availability
Memcached lacks built‑in replication; it must be deployed as a single node or with custom client‑side sharding.
Redis provides master‑slave replication, Sentinel for automatic failover, and optional read‑write splitting, greatly improving availability.
Clustering
Both systems can run in clusters, but the approaches differ.
Memcached relies on client‑side consistent hashing; when a node fails, other nodes absorb its traffic.
Redis Cluster distributes hash slots across nodes, each node having at least one replica, and requires manual resharding when nodes are added or removed. Third‑party proxy solutions like Codis or Twemproxy also exist.
Summary Table
#
Memcached
Redis
Thread model
Multithreaded
Single‑threaded (optional multithreaded I/O in Redis 6.0)
Data structures
String only, max 1 MB, 30‑day TTL limit
String, list, hash, set, sorted set, geo, HyperLogLog
Eviction policy
LRU
LRU, LFU, random, TTL‑based, etc.
Pipelining & transactions
Not supported
Supported
Persistence
Not supported
RDB & AOF
High availability
Not supported
Master‑slave replication + Sentinel
Clustering
Client‑side consistent hashing
Redis Cluster (hash slots) + replicas
Conclusion
Redis offers a richer feature set with comparable performance to Memcached, making it the preferred choice for most modern applications that need diverse data structures, persistence, and high availability. Memcached remains suitable for simple, high‑throughput get/set workloads with minimal memory usage.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.