Databases 8 min read

Common Redis Use Cases and Implementation Patterns

This article presents a comprehensive guide to using Redis for caching, distributed data sharing, distributed locks, global IDs, counters, rate limiting, bitmap statistics, shopping carts, timelines, message queues, lotteries, likes, product tagging, filtering, follow relationships, and ranking, complete with code examples.

IT Architects Alliance
IT Architects Alliance
IT Architects Alliance
Common Redis Use Cases and Implementation Patterns

1. Caching

Redis strings are used to cache hot data such as reports, popular items, object caches, and full‑page caches, which can significantly improve read performance for frequently accessed data.

2. Distributed Data Sharing

Because Redis is a distributed, standalone service, it can be shared across multiple applications, for example to store distributed sessions. The Maven dependency for Spring Session with Redis is:

<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. Example implementation:

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

Generate unique incremental IDs using the atomic INCRBY command on an integer key, e.g., INCRBY userid 1000 , suitable for sharding scenarios where a block of IDs is needed.

5. Counter

Simple integer counters can be implemented with INCR , useful for tracking article views, likes, etc., often written to Redis first and later synchronized to a database.

6. Rate Limiting

Use an integer key that increments per visitor IP; if the count exceeds a threshold, the request is rejected.

7. Bit Statistics

Redis bitmaps (e.g., BITCOUNT ) allow compact storage of binary flags. Example commands:

SET k1 a
SETBIT k1 6 1
SETBIT k1 7 0
GET k1

Bit operations such as BITOP AND , OR , XOR , and NOT can be used for large‑scale statistics, e.g., calculating users online for seven consecutive days.

8. Shopping Cart

Use Redis strings or hashes; each user ID is a key, each product ID is a field, and the value is the quantity. Commands: HINCRBY to add/subtract, HDEL to remove, HGETALL to list, HLEN for count.

9. User Timeline

Implement a timeline with a Redis list (a doubly linked list) that maintains ordered entries.

10. Message Queue

Redis lists provide blocking pop operations BLPOP and BRPOP with timeout, functioning like Java blocking queues. RPUSH adds to the tail, BLPOP removes from the head (FIFO), while BRPOP can be used as a stack (LIFO).

11. Lottery

Random selection can be done with the SPOP command on a set.

12. Likes, Check‑ins, Clock‑ins

Maintain a set per post, e.g., like:t1001 , where SADD adds a user, SREM removes, SISMEMBER checks membership, SMEMBERS lists all likers, and SCARD returns the count.

13. Product Tags

Store tags in a set, e.g., SADD tags:i5001 "clear screen" , to associate multiple tags with a product.

14. Product Filtering

Use set operations: SDIFF for difference, SINTER for intersection, SUNION for union. Example to find iPhone 11 products that are Apple, iOS, have a 6.0‑6.24‑inch LCD screen:

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

15. Follow / Recommendation Model

Represent follow relationships with sets: follow and fans . Mutual follows are stored as intersections; potential acquaintances can be derived with set differences.

16. Ranking

Use sorted sets for leaderboards. Example to increment a news article’s click count:

ZINCRBY hotNews:20190926 1 n6001

Retrieve the top 15 items:

ZREVRANGE hotNews:20190926 0 15 WITHSCORES
RediscachingMessageQueueDistributedLockBitmapsDataStructures
IT Architects Alliance
Written by

IT Architects Alliance

Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.

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.