16 Powerful Redis Use Cases Every Backend Engineer Should Know

This article presents a comprehensive guide to 16 practical Redis applications—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmaps, shopping carts, timelines, queues, lotteries, likes, product tagging, filtering, social graphs, and leaderboards—complete with command examples, code snippets, and visual illustrations to help developers implement these patterns efficiently.

Java Architect Handbook
Java Architect Handbook
Java Architect Handbook
16 Powerful Redis Use Cases Every Backend Engineer Should Know

1. Cache

Store hot data such as report results, object caches, or full‑page caches in Redis using String keys. This reduces latency for frequently accessed information.

2. Distributed Data Sharing

Because Redis runs as an independent distributed service, String keys can be shared across multiple applications, enabling scenarios like distributed session storage.

3. Distributed Lock

Implement a lock with the SETNX command, which succeeds only when the key does not already exist. After acquiring the lock, 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); // lock expires after 10 seconds
    }
    return flag == 1;
}

public static void releaseLock(String key) {
    jedis.del(key);
}

4. Global ID

Generate unique incremental IDs atomically with the INCRBY command, useful for sharding scenarios where a block of IDs is allocated at once.

INCRBY userid 1000

5. Counter

Use INCR on integer keys to count events such as article views or likes. The counts can be persisted later to a relational database.

6. Rate Limiting

Implement per‑IP request counters with INCR. If the count exceeds a predefined threshold, reject the request.

7. Bit Statistics

Leverage Redis bitmap operations for compact boolean storage. Example commands set and query bits for online‑user tracking.

SET k1 a
SETBIT k1 6 1   # set bit 6
SETBIT k1 7 0   # clear bit 7
GET k1

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

8. Shopping Cart

Model a cart with a Hash where key = userId, field = productId, and value = quantity. Common operations: HINCRBY – add or subtract quantity HDEL – remove a product HGETALL – list all items HLEN – get the number of distinct products

Shopping cart data structure
Shopping cart data structure

9. User Timeline

Store chronological events in a List (a doubly‑linked list). Push new items to the head to keep the timeline ordered.

10. Message Queue

Use a List with the blocking pop commands BLPOP and BRPOP to implement a simple message queue with optional timeout. BLPOP key timeout – removes and returns the first element, blocking if the list is empty. BRPOP key timeout – removes and returns the last element, blocking if the list is empty.

The semantics map directly to Java’s BlockingQueue implementation.

11. Lottery

Redis sets provide built‑in random element extraction via SPOP, ideal for simple lottery draws.

SPOP myset

12. Likes / Check‑in / Punch‑card

Model likes with a set keyed by like:postId. Adding, removing, checking membership, and counting are O(1) operations. SADD like:t1001 u3001 – user u3001 likes post t1001. SREM like:t1001 u3001 – cancel like. SISMEMBER like:t1001 u3001 – test if liked. SMEMBERS like:t1001 – list all users who liked. SCARD like:t1001 – total like count.

13. Product Tags

Maintain tags for a product using a set, e.g., SADD tags:i5001 "high‑resolution display".

SADD tags:i5001 "high‑resolution display"
SADD tags:i5001 "vivid colors"
SADD tags:i5001 "smooth performance"
Product tag example
Product tag example

14. Product Filtering

Combine multiple tag sets with set operations to filter products. Example: find Apple iPhone 11 devices that run iOS, have a 6.0‑6.24‑inch LCD screen.

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
Product filtering example
Product filtering example

15. Follow / Recommendation Model

Represent follow relationships with sets: userId:follow stores the IDs the user follows, and userId:fans stores followers. Intersections and differences reveal mutual follows, potential acquaintances, or recommendation candidates.

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

# Users I follow who also follow me
SINTER 1:follow 2:fans

# People user 2 follows that I don’t
SDIFF 2:follow 1:follow

16. Leaderboard

Use a sorted set ( ZSET ) to track scores. Increment a news item’s click count with ZINCRBY and retrieve the top N items with ZREVRANGE.

ZINCRBY hotNews:20190926 1 n6001   # increase click count for news 6001
ZREVRANGE hotNews:20190926 0 14 WITHSCORES   # top 15 items
Leaderboard example
Leaderboard example
CacheRedisBitmapDistributed Lockrate limitingShopping Cartleaderboard
Java Architect Handbook
Written by

Java Architect Handbook

Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.

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.