Unlock 16 Powerful Redis Patterns for Scalable Backend Systems
This guide explores sixteen practical Redis use cases—from caching and distributed locks to rate limiting, bitmaps, shopping carts, timelines, and ranking—detailing data types, commands, and implementation tips that help developers build efficient, scalable backend services.
1. Caching
String type
Examples: hot data cache (e.g., reports, celebrity scandals), object cache, full-page cache, which can improve hot data access.
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 setnx method, succeeds only when the 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
int type, incrby, using atomicity.
incrby userid 1000Used in sharding scenarios to obtain a block of IDs at once.
5. Counter
int type, incr method.
Examples: article view count, microblog likes; can tolerate some delay by writing to Redis first and syncing to DB later.
6. Rate Limiting
int type, incr method.
Use visitor IP and other info as key; increment on each request, return false when limit exceeded.
7. Bit Statistics
String type bitcount (bitmap data structure introduced in Redis 1.6.6). Characters are stored as 8‑bit binary.
set k1 a
setbit k1 6 1
setbit k1 7 0
get k1
/* 6 7 represent the 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.
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 possible on strings are also possible on hashes.
Key: user ID; field: product ID; value: quantity. Increment with hincr, decrement with hdecr, delete with hdel, get all with hgetall, count fields with hlen.
9. User Timeline
list, a doubly linked list, can be used directly as a timeline; insertion keeps order.
10. Message Queue
List provides two blocking pop operations: blpop and brpop, with optional timeout.
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, with similar blocking behavior.
These correspond to Java's blocking queue. Queue: FIFO (rpush + blpop). Stack: LIFO (rpush + brpop).
11. Lottery
Redis provides a random value retrieval.
spop myset12. Likes, Check‑in, Clock‑in
Assume a microblog ID t1001 and user ID u3001.
Use key like:t1001 to store all users who liked the post.
Like a post: sadd like:t1001 u3001
Cancel like: srem like:t1001 u3001
Check if liked: sismember like:t1001 u3001
All users who liked: smembers like:t1001
Like count: scard like:t1001
13. Product Tags
Maintain tags for a product with key tags:i5001.
sadd tags:i5001 "clear display"
sadd tags:i5001 "true color screen"
sadd tags:i5001 "extremely smooth workflow"14. Product Filtering
// Get difference
sdiff set1 set2
// Get intersection
sinter set1 set2
// Get 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, and LCD:
sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd15. User Follow & Recommendation Model
follow for following, fans for followers.
Mutual follow:
sadd 1:follow 2
sadd 2:fans 1
sadd 1:fans 2
sadd 2:follow 1People I follow also follow them (intersection): sinter 1:follow 2:fans Potential acquaintances:
sdiff 2:follow 1:follow // for user 1
sdiff 1:follow 2:follow // for user 216. Ranking
Increment news click count: zincrby hotNews:20190926 1 n6001
Get top 15 items today: zrevrange hotNews:20190926 0 15 withscores
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
