Mastering Redis: 16 Real-World Patterns for Caching, Locks, Counters, and More

This article presents sixteen practical Redis use‑cases—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmap statistics, shopping carts, timelines, message queues, lotteries, likes, tags, filtering, follow models, and ranking—explaining their data types, commands, and sample code.

ITFLY8 Architecture Home
ITFLY8 Architecture Home
ITFLY8 Architecture Home
Mastering Redis: 16 Real-World Patterns for Caching, Locks, Counters, and More

1. Cache

Use String values for hot data such as report caches, popular items, or full‑page caches to improve read performance.

2. Distributed Data Sharing

Redis, as an independent distributed service, allows String values to be shared across multiple applications, e.g., a distributed session store.

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

3. Distributed Lock

Leverage the SETNX command: it adds a key only when it does not exist, returning true on success.

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 an int key with INCRBY for atomic ID generation, e.g., INCRBY userid 1000, useful in sharding scenarios.

5. Counter

Apply the INCR command on integer keys for metrics such as article views or likes; data can be written to Redis first and later synchronized to a database.

6. Rate Limiting

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

7. Bit Statistics

Use String bitmaps with BITCOUNT and related commands. Example:

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

Bitmaps are space‑efficient and suitable for large‑scale user‑online statistics.

8. Shopping Cart

Store carts as String or Hash structures. Example key: user ID, field: product ID, value: quantity. Operations include HINCRBY, HDECRBY, HDEL, HGETALL, HLEN.

Shopping cart example
Shopping cart example

9. User Timeline

Implement timelines with List (a doubly‑linked list) to maintain ordered entries.

10. Message Queue

Redis List provides blocking pop commands BLPOP and BRPOP with timeout, mirroring Java's blocking queue semantics. RPUSH / BLPOP implements a FIFO queue; RPUSH / BRPOP can also model a LIFO stack.

11. Lottery

Use SPOP to draw a random member from a set.

SPOP myset

12. Likes, Check‑in, Clock‑in

Maintain a set per post, e.g., like:t1001, with commands: 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 likes

Likes example
Likes example

13. Product Tags

Store tags in a set, e.g., SADD tags:i5001 "clear display".

Product tags
Product tags

14. Product Filtering

Combine sets with SDIFF, SINTER, and SUNION 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
Filtering example
Filtering example

15. Follow & Recommendation Model

Use sets for follows ( 1:follow 2) and fans ( 2:fans 1). Intersection finds mutual follows; difference reveals potential acquaintances.

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

16. Ranking

Employ sorted sets for hot‑news ranking:

ZINCRBY hotNews:20190926 1 n6001
ZREVRANGE hotNews:20190926 0 15 WITHSCORES
Ranking example
Ranking example
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.

rediscachingBitmapMessage Queuedistributed-lockCounter
ITFLY8 Architecture Home
Written by

ITFLY8 Architecture Home

ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.

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.