Unlocking Redis: 16 Real-World 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, message queues, lotteries, likes, tags, product filtering, follow relationships, and rankings—demonstrating how each can be implemented with simple Redis commands to enhance performance and scalability.
1. Cache
String type. Example: hot data cache (e.g., reports, trending topics), 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.
<code><dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
</code>3. Distributed Lock
String type setnx method, succeeds only when the key does not exist, returns true.
<code>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);
}
</code>4. Global ID
int type, incrby, using atomicity. Example: incrby userid 1000. Used in sharding scenarios to obtain a range of IDs.
5. Counter
int type, incr method. Example: article view count, Weibo likes, allowing some delay by writing to Redis first and syncing to DB later.
6. Rate Limiting
int type, incr method. Use visitor IP as key; each request increments the count, and exceeding the limit returns false.
7. Bit Statistics
String type bitcount (bitmap data structure). Characters are stored as 8‑bit binary.
<code>set k1 a
setbit k1 6 1
setbit k1 7 0
get k1
/* 6 7 represent the binary bits of 'a'
ASCII of 'a' is 97 -> 01100001
ASCII of 'b' is 98 -> 01100010
Bitmaps are space‑efficient (1 MB = 8 388 608 bits) and suitable for large‑scale statistics. */
</code>Examples: online user statistics, retention statistics.
<code>setbit onlineusers 01
setbit onlineusers 11
setbit onlineusers 20
</code>Supports bitwise AND, OR, XOR, NOT operations.
<code>BITOPAND destkey key1 key2 ...
BITOPOR destkey key1 key2 ...
BITOPXOR destkey key1 key2 ...
BITOPNOT destkey key
</code>Calculate users online for 7 consecutive days:
<code>BITOP "AND" "7_days_both_online_users" "day_1_online_users" "day_2_online_users" ... "day_7_online_users"
</code>8. Shopping Cart
String or hash. All operations possible on strings are also possible on hashes.
key: user id; field: product id; value: quantity.
+1: hincr; -1: hdecr; delete: hdel; get all: hgetall; count: hlen.
9. User Timeline
List (doubly linked list) can be used directly as a timeline; insertion maintains order.
10. Message Queue
List provides two blocking pop operations: blpop and brpop, which can set a timeout.
blpop key timeout – removes and returns the first element, blocking until timeout or element appears.
brpop key timeout – removes and returns the last element, blocking similarly.
These correspond to Java's blocking queue semantics (FIFO for queue, LIFO for stack).
11. Lottery
Randomly obtain a value.
<code>spop myset
</code>12. Likes, Check‑in, Clock‑in
Use a set like
like:t1001to maintain all users who liked a specific 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 product tags with sets, e.g.,
tags:i5001.
sadd tags:i5001 "Clear screen"
sadd tags:i5001 "True color display"
sadd tags:i5001 "Excellent workflow"
14. Product Filtering
Use set operations to get differences, intersections, unions.
<code>sdiff set1 set2
sinter set1 set2
sunion set1 set2
</code>Example: add product attributes.
<code>sadd brand:apple iPhone11
sadd brand:ios iPhone11
sad screensize:6.0-6.24 iPhone11
sad screentype:lcd iPhone11
</code>Filter products that are Apple, iOS, screen size 6.0‑6.24, and LCD:
<code>sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd
</code>15. Follow & Recommendation Model
Use sets to represent follow and fan relationships.
sadd 1:follow 2
sadd 2:fans 1
sadd 1:fans 2
sadd 2:follow 1
Mutual follows (intersection):
<code>sinter 1:follow 2:fans
</code>Potential acquaintances (difference):
<code>sdiff 2:follow 1:follow
sdiff 1:follow 2:follow
</code>16. Ranking
Increment news click count:
<code>zincrby hotNews:20190926 1 n6001
</code>Get top 15 items of today:
<code>zrevrange hotNews:20190926 0 15 withscores
</code>macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.