Redis Data Types, Expiration Strategies, Memory Eviction Policies, and a Java LRU Cache Example
This article explains Redis's core data structures—string, hash, list, set, and sorted set—covers why and how data expires, details Redis's periodic and lazy expiration mechanisms, outlines the six memory eviction policies, and provides a Java LinkedHashMap‑based LRU cache implementation.
Redis provides several basic data types for different caching scenarios. The string type is the simplest key‑value pair used for straightforward caching.
The hash type works like a map, allowing you to store a flat object (without nested objects) and manipulate individual fields. Example:
key=150
value={
"id": 150,
"name": "zhangsan",
"age": 20
}Hashes are useful for caching simple objects and updating specific fields without rewriting the whole object.
The list type is an ordered collection that supports many operations such as pushing, popping, range queries, and can be used for use‑cases like fan lists, comment streams, pagination with LRANGE, or a simple message queue. Example:
key=某大v
value=[zhangsan, lisi, wangwu]Lists enable efficient pagination and queue‑like behavior by reading from the head and writing to the tail.
The set type stores unordered, deduplicated elements. It is ideal for global deduplication across multiple machines and supports set operations such as intersection, union, and difference—for example, finding common followers between two users.
The sorted set (zset) adds a score to each member, allowing automatic ordering. It is commonly used for time‑based sorting, leaderboards, and ranking. Example commands:
zadd board 85 zhangsan
zadd board 72 wangwu
zadd board 96 lisi
zadd board 62 zhaoliu
zrevrange board 0 3 # get top 3 users
zrank board zhaoliu # get rank of a userData in Redis expires because it is primarily a cache, not a permanent store. Expiration occurs either when a key is set with a TTL or when memory pressure forces eviction.
Redis uses two expiration strategies: periodic deletion , where every 100 ms a random subset of keys with TTLs is checked and removed if expired; and lazy deletion , where a key is checked for expiration only when accessed.
When many expired keys remain, Redis employs a memory eviction mechanism with six policies: noeviction , allkeys‑lru , allkeys‑random , volatile‑lru , volatile‑random , and volatile‑ttl . Each defines how Redis discards keys when memory is insufficient.
Finally, a simple LRU cache can be implemented in Java using LinkedHashMap with access‑order. The following code demonstrates this approach:
// Based on Java LinkedHashMap implementation
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private final int CACHE_SIZE;
// Constructor sets initial capacity and enables access‑order
public LRUCache(int cacheSize) {
super((int)Math.ceil(cacheSize/0.75)+1, 0.75f, true);
CACHE_SIZE = cacheSize;
}
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
// Remove the eldest entry when size exceeds the limit
return size() > CACHE_SIZE;
}
}This article provides a concise overview of Redis's data structures, expiration handling, eviction policies, and a practical LRU cache example for developers.
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.
Big Data Technology & Architecture
Wang Zhiwu, a big data expert, dedicated to sharing big data technology.
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.
