Unlock 16 Powerful Redis Patterns for Scalable Backend Systems
This article presents sixteen practical Redis use cases—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmaps, shopping carts, timelines, queues, lotteries, likes, tags, filtering, follow models, and ranking—illustrating how to leverage Redis data structures for high‑performance, scalable backend applications.
1. Cache
String type caches hot data such as reports or popular items, object cache, full-page cache, improving access speed for hot data.
2. Distributed Data Sharing
String type; Redis is a distributed independent service that can be shared across multiple applications, e.g., distributed session.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>3. Distributed Lock
String type setnx method acquires lock only if key does not exist, returns 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 range of IDs for sharding.
5. Counter
Integer type using incr method, e.g., article view count, likes, allowing slight delay by writing to Redis then syncing to DB.
6. Rate Limiting
Integer type using incr; key composed of visitor IP and other info, increment on each request, return false when limit exceeded.
7. Bit Statistics
String type bitcount (bitmap) stores characters as 8‑bit binary, useful for massive statistics.
set k1 a
setbit k1 6 1
setbit k1 7 0
get k1
/* 6 7 modify bits of 'a' (ASCII 97 -> 01100001) */Examples: online user statistics, retention.
setbit onlineusers 01
setbit onlineusers 11
setbit onlineusers 20Supports bitwise AND, OR, XOR, NOT.
BITOP AND destkey key1 key2 ...
BITOP OR destkey key1 key2 ...
BITOP XOR destkey key1 key2 ...
BITOP NOT destkey keyCalculate users online all 7 days:
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; any operation possible on strings can be done on hashes.
key: user id; field: product id; value: quantity.
+1: hincr, -1: hdecr, delete: hdel, all: hgetall, count: hlen.
9. User Timeline
List (doubly linked list) used as timeline, ordered insertion.
10. Message Queue
List provides blocking pop operations blpop/brpop with timeout.
blpop key timeout – removes and returns first element, blocks if empty.
brpop key timeout – removes and returns last element, blocks if empty.
These correspond to Java blocking queue semantics: queue (FIFO) with rpush + blpop, stack (LIFO) with rpush + brpop.
11. Lottery
Random selection using set pop.
spop myset12. Likes, Check‑in, Clock‑in
Assuming a post ID t1001 and user ID u3001, use like:t1001 to store all users who liked the post.
sadd like:t1001 u3001 – like
srem like:t1001 u3001 – unlike
sismember like:t1001 u3001 – check like
smembers like:t1001 – list all likers
scard like:t1001 – count likes
This is simpler than using a relational database.
13. Product Tags
Maintain tags with tags:i5001 set.
sadd tags:i5001 "clear picture"
sadd tags:i5001 "vivid color display"
sadd tags:i5001 "smooth workflow"
14. Product Filtering
// difference
sdiff set1 set2
// intersection
sinter set1 set2
// union
sunion set1 set2Example: iPhone11 released.
sadd brand:apple iPhone11
sadd brand:ios iPhone11
sad screensize:6.0-6.24 iPhone11
sad screentype:lcd iPhone11Filter products that are Apple, iOS, screen size 6.0‑6.24, LCD.
sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd15. User Follow & Recommendation Model
follow – following, fans – followers.
Mutual follow:
sadd 1:follow 2
sadd 2:fans 1
sadd 1:fans 2
sadd 2:follow 1
Find people I follow who also follow me (intersection):
sinter 1:follow 2:fansPotential acquaintances (difference):
sdiff 2:follow 1:follow – for user1
sdiff 1:follow 2:follow – for user2
16. Ranking
Increment news click count:
zincrby hotNews:20190926 1 n6001Get top 15 items today:
zrevrange hotNews:20190926 0 15 withscoresSigned-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.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.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.
