Databases 9 min read

16 Common Redis Application Scenarios with Code Samples

This article presents sixteen practical Redis use‑cases—including caching, distributed locks, global IDs, rate limiting, bitmaps, shopping carts, timelines, message queues, lotteries, likes, product tagging, filtering, follow/recommendation models and leaderboards—each explained with concise descriptions and ready‑to‑run code snippets.

Top Architect
Top Architect
Top Architect
16 Common Redis Application Scenarios with Code Samples

The author, a senior architect, shares sixteen typical Redis application scenarios that can be directly used in backend systems.

1. Cache

Use string keys to store hot data such as reports or frequently accessed objects. Example: object cache, full‑page cache to improve read performance.

2. Distributed Data Sharing

Because Redis is a standalone distributed service, data can be shared across multiple applications, e.g., distributed session storage.

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

3. Distributed Lock

Use the SETNX command (or set if not exists ) to acquire a 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);
    }
    return flag == 1;
}

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

4. Global ID

Use integer keys with INCRBY to generate atomic incremental IDs, suitable for sharding scenarios.

INCRBY userid 1000

5. Counter

Simple integer counters via INCR , e.g., article view counts or likes, with eventual sync to a relational database.

6. Rate Limiting

Increment a counter keyed by IP or user identifier; reject requests when the count exceeds a threshold.

7. Bitmaps

Use BITCOUNT on string values to store binary flags efficiently (1 bit per user). Useful for online‑user statistics or retention tracking.

SET k1 "a"
SETBIT k1 6 1
SETBIT k1 7 0
GET k1
# 6 and 7 represent the 6th and 7th bits of the character 'a' (ASCII 97)

8. Shopping Cart

Store cart data as STRING or HASH where the key is the user ID, the field is the product ID, and the value is the quantity. Operations: HINCRBY , HDECRBY , HDEL , HGETALL , HLEN .

9. User Timeline

Use a LIST (double‑ended queue) to maintain an ordered timeline; push new events to the head for chronological order.

10. Message Queue

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

Queue (FIFO): RPUSH + BLPOP

Stack (LIFO): RPUSH + BRPOP

11. Lottery

Randomly pick a member from a set using SPOP .

spop myset

12. Like / Check‑in / Clock‑in

Model likes with a set key like like:t1001 . Commands: SADD , SREM , SISMEMBER , SMEMBERS , SCARD .

13. Product Tags

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

14. Product Filtering

Combine multiple tag sets with set operations ( SINTER , SUNION , SDIFF ) to filter products by brand, OS, screen size, etc.

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

Use sets to represent follow and fan relationships, then compute intersections or differences to find mutual follows or potential connections.

sadd 1:follow 2
sadd 2:fans 1
sinter 1:follow 2:fans   # mutual follows
sdiff 2:follow 1:follow   # possible acquaintances

16. Leaderboard

Use sorted sets ( ZINCRBY , ZREVRANGE ) to maintain hot news or score rankings.

zincrby hotNews:20190926 1 n6001   # increment score for news 6001
zrevrange hotNews:20190926 0 15 WITHSCORES   # top 15 items

These patterns demonstrate how Redis can serve as a versatile backend component for caching, synchronization, counting, ranking, and many other common features in modern applications.

RediscachingDistributed LockRate LimitingGlobal IDleaderboard
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.