Unlock 16 Powerful Redis Use Cases to Supercharge Your Backend
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, filtering, follow relationships, and ranking—demonstrating how each can boost performance and simplify backend architecture.
1. Caching
Use the String type for hot data such as reports or popular content; object caching and full‑page caching can dramatically improve access speed.
2. Distributed Data Sharing
Because Redis is a standalone distributed service, String keys can be shared across multiple applications, e.g., for distributed sessions.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>3. Distributed Lock
Implement a lock with SETNX (set if not exists); if the lock is acquired, set an expiration to avoid deadlocks.
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
Use the int type with INCRBY to generate atomic sequential IDs, e.g., INCRBY userid 1000, useful for sharding scenarios.
5. Counter
Simple integer counters via INCR can track reads, likes, etc., with eventual synchronization to a relational database.
6. Rate Limiting
Store a counter keyed by IP or other identifier; each request increments the counter, and exceeding a threshold returns false.
7. Bitmaps
Redis String bitmaps enable space‑efficient statistics; BITCOUNT counts set bits.
set k1 a
setbit k1 6 1
setbit k1 7 0
get k1Bitwise operations ( BITOP AND/OR/XOR/NOT) allow complex analytics such as finding users online for seven consecutive days.
BITOP AND destkey key1 key2 ...
BITOP OR destkey key1 key2 ...
BITOP XOR destkey key1 key2 ...
BITOP NOT destkey key8. Shopping Cart
Model a cart with a hash : key = userId, field = productId, value = quantity. Operations: HINCRBY to add/subtract, HDEL to remove, HGETALL to list, HLEN for count.
9. Timeline
Use a list (double‑ended queue) to store ordered events; insertions keep chronological order.
10. Message Queue
Redis list provides blocking pop commands BLPOP and BRPOP with timeout, mirroring Java's blocking queue semantics (FIFO).
11. Lottery
Random selection via SPOP on a set returns a random member.
12. Likes / Check‑ins / Punch‑in
Maintain a set per post, e.g., like:t1001, where SADD adds a user, SREM removes, SISMEMBER checks, SCARD counts.
13. Product Tags
Store tags in a set like tags:i5001; each tag string is added with SADD.
14. Product Filtering
Combine sets using SDIFF (difference), SINTER (intersection), and SUNION (union) to filter products by brand, OS, screen size, etc.
sdiff set1 set2
sinter set1 set2
sunion set1 set215. Follow / Recommendation Model
Represent follows and fans with sets: sadd 1:follow 2, sadd 2:fans 1. Mutual follows are intersections; potential acquaintances are derived via set differences.
sadd 1:follow 2
sadd 2:fans 1
sadd 1:fans 2
sadd 2:follow 1
sinter 1:follow 2:fans
sdiff 2:follow 1:follow
sdiff 1:follow 2:follow16. Ranking
Increment a sorted‑set score with ZINCRBY (e.g., zincrby hotNews:20190926 1 n6001) and retrieve top items via ZREVRANGE with scores.
zincrby hotNews:20190926 1 n6001
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.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.
