Why Redis Dominates Modern Caching: Architecture, Strategies, and Pitfalls
This article provides a comprehensive technical overview of Redis, covering its high‑performance in‑memory design, rich data structures, persistence options, transaction support, eviction policies, common caching patterns, distributed locking techniques, and high‑availability solutions such as Sentinel and Cluster, while also comparing it with alternatives like Memcached, Tair, Guava, EVCache and ETCD.
Why Redis
Redis is an in‑memory key‑value store written in C, using a single‑threaded event loop (epoll) to achieve high throughput with O(1) operations, supporting persistence, Lua scripts, transactions, and various data structures.
Comparison with Other Caches
Compared with Memcached, Tair and Guava, Redis offers richer data types and persistence; Memcached is a simple key/value cache, Tair adds persistence and multiple engines, while Guava provides a local JVM cache with LRU but can increase heap pressure and GC overhead.
Cache Use Cases
Typical scenarios include distributed caching to reduce database load, session storage, fast access to hot data, and serving as a data source for large‑scale reads.
Redis Internals
Core structures include SDS strings, skip‑list for sorted sets, hash tables (dict) for key‑value storage, ziplist, intset, quicklist for lists, listpack and Rax tree for streams.
struct sdshdr{int len;int free;char buf[];}Persistence
Redis supports two persistence mechanisms: RDB snapshots, which are fast to load and have low runtime impact, and AOF logs, which record every write operation for stronger durability but produce larger files and slower recovery.
Transactions and Eviction
Transactions are implemented via MULTI/EXEC/WATCH, providing atomicity and consistency. Eviction policies such as noeviction, allkeys‑lru, allkeys‑random, volatile‑lru, volatile‑ttl, etc., control memory usage when the dataset exceeds the configured limit.
Cache Patterns
Common patterns include Cache‑Aside (lazy loading), Read‑Through/Write‑Through, Write‑Behind (asynchronous writes), and multi‑level caching that combines local JVM caches, remote Redis caches, and tiered storage.
Distributed Locking
Redis can implement optimistic locks with WATCH, simple locks with SETNX, and robust distributed locks using the RedLock algorithm. For strong consistency requirements, CP‑model systems like Zookeeper or etcd are recommended.
// Delay double delete to ensure eventual consistency
public void write(String key, Object data) {
redis.delKey(key);
db.updateData(data);
Thread.sleep(1000);
redis.delKey(key);
} // Acquire lock using SET with NX and EX
String result = jedis.set(lockKey, requestId, "NX", "EX", expireTime);
// Release lock safely with Lua script
String lua = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end";
Object res = jedis.eval(lua, Collections.singletonList(lockKey), Collections.singletonList(requestId));High Availability
Redis provides master‑slave replication, Sentinel for automatic failover, and Cluster mode with hash slots or consistent hashing for sharding and scaling; each mode balances simplicity, read/write separation, and fault tolerance.
Other Technologies
EVCache (Netflix) builds on Memcached for massive scale, ETCD offers CP consistency for configuration data, and Raft is used by Sentinel for leader election and coordinated failover.
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.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
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.
