Common Redis Use Cases and Implementation Patterns
This article presents sixteen practical Redis use cases—including caching, distributed locks, global IDs, counters, rate limiting, bitmaps, shopping carts, timelines, message queues, lotteries, likes, product tagging, filtering, follow relationships, and leaderboards—each explained with data types, commands, and code examples.
Author "架构君" shares a collection of practical Redis usage scenarios, illustrating how different Redis data structures and commands can solve common application problems.
1. Caching
Uses String type for hot data such as reports, object caches, and full-page caches to improve access speed.
2. Distributed Data Sharing
Leverages Redis as a distributed service to share data across applications, e.g., distributed sessions.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>3. Distributed Lock
Implements a lock using SETNX which succeeds only when the key does not exist.
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
Uses INCRBY on an int key to generate atomic incremental IDs, suitable for sharding scenarios.
5. Counter
Employs INCR for counting events such as article views or likes, allowing asynchronous persistence to a database.
6. Rate Limiting
Counts accesses per IP (or other identifier) with INCR ; if the count exceeds a threshold, further requests are rejected.
7. Bit Statistics
Uses bitmap commands like BITCOUNT and SETBIT to store and analyze binary data efficiently, e.g., online user statistics.
set k1 a
setbit k1 6 1
setbit k1 7 0
get k18. Shopping Cart
Models a cart with String or Hash ; key is user ID, field is product ID, value is quantity. Operations use HINCRBY , HDEL , HGETALL , etc.
9. User Timeline
Implements a timeline using a List (doubly linked list) where new entries are inserted in order.
10. Message Queue
Uses List blocking pop commands BLPOP and BRPOP with timeout to build a simple message queue.
11. Lottery
Randomly selects a member from a set using SPOP .
spop myset12. Likes / Check‑in / Clock‑in
Manages likes with sets: SADD like:postId userId , SREM to cancel, SISMEMBER to check, SCARD for count.
13. Product Tags
Stores tags in a set, e.g., SADD tags:i5001 "High definition screen" .
14. Product Filtering
Combines sets using SINTER , SUNION , SDIFF to filter products by brand, OS, screen size, etc.
sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd15. Follow / Recommendation Model
Tracks follows and fans with sets; intersections and differences reveal mutual follows and potential connections.
16. Leaderboard
Uses sorted sets: ZINCRBY hotNews:20190926 1 n6001 to increment score, ZREVRANGE hotNews:20190926 0 15 WITHSCORES to retrieve top items.
The article concludes with a call to share and join the community.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.