Databases 9 min read

16 Common Redis Use Cases and How to Implement Them

This article outlines sixteen practical Redis usage scenarios—including caching, distributed sessions, locks, ID generation, counters, rate limiting, bitmaps, shopping carts, timelines, message queues, lotteries, likes, tags, filtering, follow relationships, and leaderboards—explaining each concept and providing example code snippets for implementation.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
16 Common Redis Use Cases and How to Implement Them

In technical interviews, Redis questions appear frequently; while theoretical answers can be memorized, project‑specific questions require a solid understanding of real‑world use cases.

The following sixteen Redis usage scenarios are described, each with practical guidance and code examples.

1. Cache

Redis, as an in‑memory key‑value store, is often used for caching. Store serialized objects as strings, ensure unique short keys (e.g., class name + primary key), and choose an efficient serialization method to reduce memory usage.

2. Distributed Data Sharing

Because Redis runs as an independent service, multiple applications can share data, such as distributed sessions.

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

3. Distributed Lock

Use the SETNX command to acquire a lock only when the key does not exist, then set an expiration time.

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.

INCRBY userid 1000

5. Counter

Simple integer counters can be implemented with INCR , suitable for page views, likes, etc., with eventual consistency by syncing back to a database.

6. Rate Limiting

Combine the visitor’s IP (or other attributes) as the key and increment a counter; reject requests when the count exceeds a threshold.

7. Bitmaps (Bit Statistics)

Redis bitmap commands such as SETBIT and BITCOUNT enable space‑efficient tracking of binary states, e.g., online user statistics.

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

Use STRING or HASH structures; the key is the user ID, the field is the product ID, and the value is the quantity.

Add: HINCRBY cart:userId productId 1

Remove: HDEL cart:userId productId

Get all: HGETALL cart:userId

9. User Timeline

Leverage LIST (a doubly linked list) to store ordered timeline entries; new items are pushed to maintain order.

10. Message Queue

Redis LIST provides blocking pop operations BLPOP and BRPOP with timeout, functioning like a Java blocking queue.

BLPOP key1 timeout – removes and returns the first element, blocking if empty.

BRPOP key1 timeout – removes and returns the last element, blocking if empty.

11. Lottery

Use the set command SPOP to randomly draw a member from a set.

spop myset

12. Likes / Check‑in / Clock‑in

Model likes with a set keyed by the content ID, e.g., like:t1001 for a post.

Like: SADD like:t1001 u3001

Unlike: SREM like:t1001 u3001

Check like: SISMEMBER like:t1001 u3001

All likers: SMEMBERS like:t1001

Like count: SCARD like:t1001

13. Product Tags

Maintain tags with a set, e.g., SADD tags:i5001 tagName .

14. Product Filtering

Combine set operations to filter products by multiple attributes.

// Example sets
SADD brand:apple iPhone11
SADD brand:ios iPhone11
SADD screensize:6.0-6.24 iPhone11
SADD screentype:lcd iPhone11

// Intersection to find Apple iOS phones with the specified screen size and type
SINTER brand:apple brand:ios screensize:6.0-6.24 screentype:lcd

15. Follow / Recommendation Model

Use sets to represent follow relationships ( user:follow ) and fans ( user:fans ); intersections and differences help discover mutual follows and potential connections.

SADD 1:follow 2
SADD 2:fans 1
SINTER 1:follow 2:fans   // mutual follows
SDIFF 2:follow 1:follow   // users 2 follows that user 1 does not

16. Leaderboard

Sorted sets ( ZSET ) store scores; ZINCRBY increments a member’s score, and ZREVRANGE retrieves the top N items.

zincrby hotNews:20190926 1 n6001
zrevrange hotNews:20190926 0 15 withscores

These scenarios demonstrate how Redis can be applied to a wide range of problems beyond simple caching, providing high performance and rich data structures for modern backend systems.

CacheRedisMessage QueueDistributed LockRate LimitingleaderboardID generationBitmaps
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login 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.