Databases 9 min read

Beyond Caching: 16 Powerful Redis Use Cases

This article explores sixteen practical Redis applications—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmaps, shopping carts, timelines, message queues, lotteries, likes, product tags, filtering, follow relationships, and ranking—demonstrating how Redis can serve as a versatile data store beyond simple caching.

java1234
java1234
java1234
Beyond Caching: 16 Powerful Redis Use Cases

1. Caching

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, it can be shared across multiple applications.

Example: distributed Session.

<dependency>
  <groupId>org.springframework.session</groupId>
  <artifactId>spring-session-data-redis</artifactId>
</dependency>

3. Distributed Lock

String type setnx method, succeeds only if the key does not exist, returns 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

int type, incrby, utilizes atomicity. incrby userid 1000 Useful in sharding scenarios to obtain a range of IDs at once.

5. Counter

int type, incr method.

Examples: article view count, tweet likes; can tolerate some delay by writing to Redis first then synchronizing to the database.

6. Rate Limiting

int type, incr method.

Use visitor IP and other information as the key; each access increments the count, and exceeding the limit returns false.

7. Bit Statistics

String type bitcount (bitmap introduced in Redis 1.6.6).

Characters are stored as 8‑bit binary.

set k1 a
setbit k1 6 1
setbit k1 7 0
get k1
/* 6 7 represent bits of 'a' (ASCII 97) -> 01100001 */

Applications: online user statistics, retention statistics.

setbit onlineusers 01
setbit onlineusers 11
setbit onlineusers 20

Supports bitwise AND, OR, XOR, NOT.

BITOPAND destkey key [key ...]
BITOPOR destkey key [key ...]
BITOPXOR destkey key [key ...]
BITOPNOT destkey key

Calculate 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

String or hash. All operations possible on String are also possible on hash.

购物车数据结构
购物车数据结构

Data structure: key = user id; field = product id; value = quantity.

+1: hincr; -1: hdecr; delete: hdel; all: hgetall; count: hlen.

9. User Timeline

list, a doubly linked list, can be used directly as a timeline; insertion keeps order.

10. Message Queue

List provides two blocking pop operations: blpop/brpop with timeout.

blpop key1 timeout – removes and returns the first element, blocks if the list is empty.

brpop key1 timeout – removes and returns the last element, blocks if the list is empty.

These correspond to Java’s blocking queue. Queue (FIFO): rpush + blpop. Stack (LIFO): rpush + brpop.

11. Lottery

Random value retrieval.

spop myset

12. Likes, Check‑in, Clock‑in

点赞数据结构
点赞数据结构

Assume tweet ID t1001 and user ID u3001. Use the set like:t1001 to maintain all users who liked the tweet.

sadd like:t1001 u3001 – like

srem like:t1001 u3001 – unlike

sismember like:t1001 u3001 – check if liked

smembers like:t1001 – all users who liked

scard like:t1001 – count of likes

This is simpler than using a relational database.

13. Product Tags

商品标签示例
商品标签示例

Use tags:i5001 to maintain all tags of a product.

sadd tags:i5001 "画面清晰细腻"

sadd tags:i5001 "真彩清晰显示屏"

sadd tags:i5001 "流程至极"

14. Product Filtering

// Get difference
sdiff set1 set2
// Get intersection
sinter set1 set2
// Get union
sunion set1 set2
商品筛选示例
商品筛选示例

Example: iPhone11 released.

sadd brand:apple iPhone11
sadd brand:ios iPhone11
sad screensize:6.0-6.24 iPhone11
sad screentype:lcd iPhone11

Filter products that are Apple, iOS, screen size 6.0‑6.24, LCD.

sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd

15. User Follow & Recommendation Model

follow – following, fans – followers.

Mutual follow (intersection):

sadd 1:follow 2
sadd 2:fans 1
sadd 1:fans 2
sadd 2:follow 1

People I follow also follow him (intersection): sinter 1:follow 2:fans Possible acquaintances (difference):

sdiff 2:follow 1:follow   // for user 1
sdiff 1:follow 2:follow   // for user 2

16. Ranking

Increment news click count: zincrby hotNews:20190926 1 n6001 Get top 15 items today:

zrevrange hotNews:20190926 0 15 withscores
排行榜示例
排行榜示例
RedisCachingRankingMessage QueueDistributed LockData StructuresRate LimitingBitmaps
java1234
Written by

java1234

Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.