Unlock 16 Powerful Redis Use Cases to Supercharge Your Applications
This article explores sixteen practical Redis patterns—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmaps, shopping carts, timelines, queues, lotteries, likes, tags, product filtering, follow relationships, and leaderboards—detailing their data types, commands, and code examples for building high‑performance systems.
1. Caching
String type. For example, hot data cache (e.g., reports, trending 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 across multiple applications, such as distributed sessions.
<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, leveraging atomicity (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., where writes go to Redis first and are later synchronized to the database.
6. Rate Limiting
Integer type using INCR with a key based on visitor IP or other attributes; each request increments the count, and exceeding the limit returns false.
7. Bitmaps (Bit Counting)
String type BITCOUNT (bitmap data structure introduced in Redis 1.6.6). Characters are stored as 8‑bit binary values, enabling space‑efficient large‑scale statistics.
SET k1 a
SETBIT k1 6 1
SETBIT k1 7 0
GET k1
/* 6 and 7 represent the modified bits of character 'a' (ASCII 97 -> 01100001) */Examples: online user statistics, retention statistics.
SETBIT onlineusers 01
SETBIT onlineusers 11
SETBIT onlineusers 20Bitwise operations such as AND, OR, XOR, NOT are supported.
BITOP AND destkey key1 key2 ...
BITOP OR destkey key1 key2 ...
BITOP XOR destkey key1 key2 ...
BITOP NOT destkey keyCalculate users online for seven consecutive days:
BITOP AND "7_days_both_online_users" "day_1_online_users" "day_2_online_users" ... "day_7_online_users"8. Shopping Cart
Implemented with String or HASH. All operations possible on strings are also available on hashes.
key: user ID; field: product ID; value: quantity.
Increment: HINCRBY; decrement: custom command; delete: HDEL; retrieve all: HGETALL; count items: HLEN.
9. User Timeline
Use LIST (a doubly linked list) as a timeline; insertions keep order.
10. Message Queue
LISTprovides blocking pop operations BLPOP and BRPOP with timeout, analogous to Java's blocking queue.
Queue (FIFO): RPUSH + BLPOP.
Stack (LIFO): RPUSH + BRPOP.
11. Lottery
Use SPOP to randomly obtain a value from a set.
spop myset12. Likes, Check‑in, Attendance
Assume a post ID t1001 and a user ID u3001. Use a set like:t1001 to store users who liked the post.
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:t100113. Product Tags
Maintain all tags of a product with a set, e.g., SADD tags:i5001 "clear display".
14. Product Filtering
// Get difference
sdiff set1 set2
// Get intersection
sinter set1 set2
// Get union
sunion set1 set2Example: an iPhone 11 product with tags brand:apple, brand:ios, screensize:6.0-6.24, screentype:lcd. Filter with:
sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd15. Follow / Recommendation Model
Use sets: follow for following, fans for followers.
Mutual follow: SADD 1:follow 2 and SADD 2:fans 1.
Find users I follow who also follow me (intersection):
sinter 1:follow 2:fansPotential acquaintances (difference):
sdiff 2:follow 1:follow // for user 1
sdiff 1:follow 2:follow // for user 216. Leaderboard
Increment the score of news ID 6001:
zincrby hotNews:20190926 1 n6001Retrieve top 15 items for 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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
