Databases 9 min read

16 Powerful Ways to Leverage Redis in Your Applications

This article presents sixteen practical Redis use cases—from simple caching and distributed sessions to global IDs, rate limiting, bitmaps, shopping carts, timelines, message queues, likes, tags, filtering, follow relationships, and ranking—each illustrated with commands and code snippets for real‑world backend development.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
16 Powerful Ways to Leverage Redis in Your Applications

1. Caching

Use Redis String to cache hot data such as reports, popular items, or full‑page content. This dramatically improves read latency for frequently accessed information.

2. Distributed Data Sharing

Because Redis runs as an independent distributed service, String values can be shared across multiple applications, e.g., for distributed session storage.

org.springframework.session:spring-session-data-redis

3. Distributed Lock

Implement a lock with the SETNX command. It succeeds only when the key does not exist; 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 Generation

Use the atomic INCRBY command on an integer key to generate sequential IDs, useful for sharding scenarios where a block of IDs is needed.

INCRBY userid 1000

5. Counters

Leverage the INCR command for simple counters such as article view counts or likes. The values can be synchronized back to a relational database later.

6. Rate Limiting

Combine a visitor’s IP (or another identifier) as the key and increment a counter on each request. Reject the request when the count exceeds a predefined threshold.

7. Bitmaps (Bit Statistics)

Use SETBIT, GETBIT and BITCOUNT to store binary flags efficiently (1 MB = 8,388,608 bits). Bitwise operations are available via BITOP.

SET k1 a
SETBIT k1 6 1
SETBIT k1 7 0
GET k1
BITOP AND destkey key1 key2 ...
BITOP OR destkey key1 key2 ...
BITOP XOR destkey key1 key2 ...
BITOP NOT destkey key

8. Shopping Cart

Model a cart with a Redis Hash. Use the user ID as the key, product ID as the field, and quantity as the value. HINCRBY – increase or decrease quantity. HDEL – remove a product. HGETALL – retrieve the whole cart. HLEN – count distinct items.

9. User Timeline

Store ordered events in a Redis List (doubly‑linked list). Insert new items at the head to keep the timeline chronologically sorted.

10. Message Queue

Redis List provides blocking pop operations BLPOP and BRPOP with optional timeouts, enabling a simple message queue. BLPOP key timeout – blocks until an element is available or the timeout expires. BRPOP key timeout – same for the tail of the list.

11. Lottery

Use a Redis Set and the SPOP command to draw a random member, implementing a simple lottery mechanism.

SPOP myset

12. Likes / Check‑ins / Punch‑in

Maintain a Set per content item (e.g., like:t1001) to store user IDs that performed the action. SADD like:t1001 u3001 – add a like. SREM like:t1001 u3001 – remove a like. SISMEMBER like:t1001 u3001 – check existence. SMEMBERS like:t1001 – list all likers. SCARD like:t1001 – count total likes.

13. Product Tags

Store tags for a product in a Redis Set, e.g., tags:i5001.

SADD tags:i5001 "clear display"
SADD tags:i5001 "true color"
SADD tags:i5001 "high performance"

14. Product Filtering

Combine multiple tag sets using set operations: SDIFF (difference), SINTER (intersection), and SUNION (union).

SDIFF set1 set2
SINTER set1 set2
SUNION set1 set2

Example – filter iPhone11 by brand, OS, screen size, and screen type:

SADD brand:apple iPhone11
SADD brand:ios iPhone11
SADD screensize:6.0-6.24 iPhone11
SADD screentype:lcd iPhone11
SINTER brand:apple brand:ios screensize:6.0-6.24 screentype:lcd

15. Follow / Recommendation Model

Model follow relationships with Set s: user:follow stores IDs a user follows; user:fans stores their followers.

SADD 1:follow 2
SADD 2:fans 1
SINTER 1:follow 2:fans

– mutual follows. SDIFF 2:follow 1:follow – users followed by 2 but not by 1 (potential new connections).

16. Ranking

Use a sorted set ( ZSET) to maintain scores, such as news click counts. Increment with ZINCRBY and retrieve the top N items with ZREVRANGE (including scores).

ZINCRBY hotNews:20190926 1 n6001
ZREVRANGE hotNews:20190926 0 15 WITHSCORES
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 systemsBackend DevelopmentRediscachingMessage QueueData Structures
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

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.