10+ Practical Redis Use Cases You Can Implement Today
This article walks through more than ten common Redis scenarios—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmap statistics, shopping carts, timelines, message queues, lotteries, likes, product tagging, filtering, follow/fan relationships, and ranking—showing concrete command examples and code snippets for each.
1. Cache
String type is used for hot data caching such as reports, object caches, or full‑page caches to improve access speed for frequently accessed data.
2. Distributed Data Sharing
Because Redis is a distributed, independent service, String values can be shared across multiple applications, e.g., for distributed session storage.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>3. Distributed Lock
Using the SETNX command on a String key ensures the lock is set only when the key does not exist; the method returns true on success.
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
Using the integer type with INCRBY provides atomic increment operations for generating global identifiers.
INCRBY userid 10005. Counter
Integer type with INCR can count events such as article reads or likes; data can be written to Redis first and later synchronized to a database.
Example: counting article views or Weibo likes with eventual consistency.
6. Rate Limiting
Integer type with INCR tracks request counts per IP; when the count exceeds a threshold, the request is rejected.
7. Bit Statistics
String type BITCOUNT (available since Redis 1.6.6) allows efficient storage of binary flags; each character occupies 8 bits.
SET k1 a
SETBIT k1 6 1
SETBIT k1 7 0
GET k1
# The ASCII code for 'a' is 97 → 01100001 in binary.
# Bitmaps are space‑efficient (1 MB = 8 388 608 bits) and suitable for large‑scale statistics.Typical use cases: online‑user statistics, retention analysis.
8. Shopping Cart
Implemented with String or Hash. In a Hash, the key is the user ID, the field is the product ID, and the value is the quantity.
key: user ID; field: product ID; value: quantity.
Increment: HINCRBY; decrement: HDECRBY; delete: HDEL; get all: HGETALL; count items: HLEN.
9. User Timeline
Using a Redis LIST (a doubly linked list) to store timeline entries in order; insertion maintains chronological order.
10. Message Queue
Redis LIST provides blocking pop operations BLPOP and BRPOP with timeout, functioning like a Java blocking queue.
Queue (FIFO): RPUSH + BLPOP.
Stack (LIFO): RPUSH + BRPOP.
11. Lottery
Redis sets can randomly return a member, useful for simple lottery draws.
SPOP myset12. Likes / Check‑in / Clock‑in
Using a set keyed by like:<post_id> to store user IDs that liked a post.
Add like: SADD like:t1001 u3001 Remove like: SREM like:t1001 u3001 Check like: SISMEMBER like:t1001 u3001 All likers: SMEMBERS like:t1001 Like count:
SCARD like:t100113. Product Tags
Maintain a set tags:<product_id> containing all tags for a product.
SADD tags:i5001 "画面清晰细腻"
SADD tags:i5001 "真彩清晰显示屏"
SADD tags:i5001 "流程至极"14. Product Filtering
Store attribute sets (e.g., brand, OS, screen size) and use set operations to filter.
# Add attributes
SADD brand:apple iPhone11
SADD brand:ios iPhone11
SADD screensize:6.0-6.24 iPhone11
SADD screentype:lcd iPhone11
# Filter products that match all criteria
SINTER brand:apple brand:ios screensize:6.0-6.24 screentype:lcd15. Follow / Recommendation Model
Use sets to represent follow relationships and fans; set intersections and differences enable mutual‑follow detection, recommendation of possible acquaintances, etc.
# Follow relationships
SADD 1:follow 2
SADD 2:fans 1
SADD 1:fans 2
SADD 2:follow 1
# Mutual follow (intersection)
SINTER 1:follow 2:fans
# Possible acquaintances (difference)
SDIFF 2:follow 1:follow # for user 1
SDIFF 1:follow 2:follow # for user 216. Ranking
Sorted sets store scores such as click counts; ZINCRBY increments a member's score, and ZREVRANGE retrieves top‑N items.
ZINCRBY hotNews:20190926 1 n6001 # increment click count
ZREVRANGE hotNews:20190926 0 15 WITHSCORES # top 15 todayIoT Full-Stack Technology
Dedicated to sharing IoT cloud services, embedded systems, and mobile client technology, with no spam ads.
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.
