Databases 8 min read

Unlock Redis: 16 Real‑World Patterns for Caching, Locks, Queues, and More

This article explores sixteen practical Redis use cases—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmaps, shopping carts, timelines, message queues, lotteries, likes, tags, product filtering, follow relationships, and ranking—detailing data types, commands, and code snippets for each scenario.

Programmer DD
Programmer DD
Programmer DD
Unlock Redis: 16 Real‑World Patterns for Caching, Locks, Queues, and More

1. Cache

Use String type for hot‑data caching such as reports, celebrity news, object cache, or full‑page cache 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 sessions.

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

3. Distributed Lock

Use the SETNX command; it succeeds only when the key does not exist, returning 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

Use int type with INCRBY to generate atomic IDs, e.g., INCRBY userid 1000, useful for sharding scenarios.

5. Counter

Use int type with INCR for counters such as article view counts or likes; data can be written to Redis first and synchronized to the database later.

6. Rate Limiting

Also based on int and INCR. Use the visitor's IP as the key; each request increments the counter, and exceeding a threshold returns false.

7. Bit Statistics

Use String type with bitmap commands ( SETBIT, GETBIT, BITCOUNT) to store bits efficiently. Example commands:

set k1 a
setbit k1 6 1
setbit k1 7 0
get k1

Bitwise operations ( BITOP AND, BITOP OR, BITOP XOR, BITOP NOT) enable set‑based analytics such as online‑user statistics.

8. Shopping Cart

Both String and hash can be used; a hash allows HINCRBY, HDECRBY, HDEL, HGETALL, HLEN. Key is the user ID, field is the product ID, value is the quantity.

9. User Timeline

Use a list (doubly linked list) to store timeline entries; insertion maintains order.

10. Message Queue

Redis list provides blocking pop operations BLPOP and BRPOP with configurable timeout, similar to a Java blocking queue.

11. Lottery

Random selection can be done with SPOP on a set.

12. Likes / Check‑in / Punch

Use sets to record likes, e.g., SADD like:t1001 u3001, SREM like:t1001 u3001, SISMEMBER like:t1001 u3001, SMEMBERS like:t1001, SCARD like:t1001.

13. Product Tags

Maintain tags with sets like SADD tags:i5001 "clear screen".

14. Product Filtering

Use set operations: SDIFF (difference), SINTER (intersection), SUNION (union). Example:

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

15. Follow / Recommendation Model

Use sets follow and fans to model relationships. Mutual follow is stored with both SADD 1:follow 2 and SADD 2:follow 1. Recommendations can be derived with SINTER and SDIFF commands.

16. Ranking

Sorted sets ( zset ) handle rankings: ZINCRBY hotNews:20190926 1 n6001 increments a news item’s score, and ZREVRANGE hotNews:20190926 0 15 WITHSCORES retrieves the top 15 items.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Distributed SystemsrediscachingMessage Queue
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.