Common Redis Use Cases: Caching, Distributed Locks, Counters, Rate Limiting, and More
This article outlines a variety of practical Redis use cases—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmap statistics, shopping carts, timelines, message queues, lotteries, likes, product tagging, filtering, follow/recommendation models, and ranking—demonstrating how Redis can support diverse backend functionalities.
1. Cache
String type. Example: hot data cache (e.g., reports, popular items), object cache, full-page cache, which can improve access to hot data.
2. Distributed Data Sharing
String type, because Redis is a distributed independent service that can be shared among multiple applications.
Example: distributed session.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>3. Distributed Lock
String type using the SETNX command; it succeeds only when the key does not exist, returning true.
public static boolean getLock(String key) {
Long flag = jedis.setnx(key, "1");
if (flag == 1) {
jedis.expire(key, 10);
}
return flag == 1;
}
public static void releaseLock(String key) {
jedis.del(key);
}4. Global ID
Integer type using INCRBY for atomic increment, e.g., INCRBY userid 1000 to allocate a block of IDs for sharding scenarios.
5. Counter
Integer type using INCR , suitable for page views, likes, etc., with eventual synchronization to a database.
6. Rate Limiting
Integer type using INCR on a key composed of the visitor’s IP and other info; if the count exceeds a threshold, the request is rejected.
7. Bit Statistics
String type with BITCOUNT (bitmap) to store bits efficiently; each character is stored as 8 bits.
set k1 a
setbit k1 6 1
setbit k1 7 0
get k1
/* 6 7 represent modifications of the binary bits of ‘a’ (ASCII 97 = 01100001) */Examples: online user statistics, retention statistics.
setbit onlineusers 01
setbit onlineusers 11
setbit onlineusers 20Supports bitwise AND, OR, XOR, NOT operations.
BITOP AND destkey key1 key2 ...
BITOP OR destkey key1 key2 ...
BITOP XOR destkey key1 key2 ...
BITOP NOT destkey keyCalculate users online for 7 consecutive days using BITOP AND .
BITOP "AND" "7_days_both_online_users" "day_1_online_users" "day_2_online_users" ... "day_7_online_users"8. Shopping Cart
String or hash; all operations applicable to strings are also available for hashes.
Key: user ID; field: product ID; value: quantity.
+1: HINCRBY
-1: HDECRBY
Delete: HDEL
All items: HGETALL
Count of items: HLEN
9. User Timeline
Use a Redis list (doubly linked list) as a timeline; insert entries in order.
10. Message Queue
Lists provide blocking pop operations BLPOP and BRPOP with optional timeout, functioning like Java blocking queues.
BLPOP key timeout : removes and returns the first element, blocking until an element is available or timeout.
BRPOP key timeout : removes and returns the last element, similarly blocking.
Queue semantics: FIFO (RPUSH + BLPOP); Stack semantics: LIFO (RPUSH + BRPOP).
11. Lottery
Use Redis set random pop to obtain a random value.
spop myset12. Likes, Check‑in, Punch‑in
Maintain likes with a set keyed by like:postId .
Like: SADD like:t1001 u3001
Unlike: SREM like:t1001 u3001
Check like: SISMEMBER like:t1001 u3001
All likers: SMEMBERS like:t1001
Like count: SCARD like:t1001
13. Product Tags
Use a set tags:productId to store all tags of a product.
SADD tags:i5001 "画面清晰细腻"
SADD tags:i5001 "真彩清晰显示屏"
SADD tags:i5001 "流程至极"
14. Product Filtering
Use set operations to compute intersections, unions, and differences for multi‑criteria filtering.
// Get difference
sdiff set1 set2
// Get intersection
sinter set1 set2
// Get union
sunion set1 set2Example: filter iPhone 11 by brand, OS, screen size, and screen type.
sadd brand:apple iPhone11
sadd brand:ios iPhone11
sadd screensize:6.0-6.24 iPhone11
sadd screentype:lcd iPhone11
sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd15. Follow / Recommendation Model
Use sets user:follow and user:fans to represent following and followers.
Mutual follow example:
sadd 1:follow 2
sadd 2:fans 1
sadd 1:fans 2
sadd 2:follow 1Find users followed by me who also follow them (intersection):
sinter 1:follow 2:fansPotential acquaintances via set difference:
sdiff 2:follow 1:follow // for user 1
sdiff 1:follow 2:follow // for user 216. Ranking
Use a sorted set to store click counts.
zincrby hotNews:20190926 1 n6001 // increment news ID 6001Retrieve top 15 items:
zrevrange hotNews:20190926 0 15 withscoresRedis enables these patterns to build high‑performance backend services.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.