Backend Development 8 min read

Redis Practical Use Cases: Caching, Distributed Locks, Global IDs, Counters, Rate Limiting, Bitmaps, Shopping Cart, Timeline, Message Queue, and More

This article presents a comprehensive guide to using Redis for various backend scenarios, including caching hot data, sharing sessions, implementing distributed locks, generating global IDs, counting, rate limiting, bitmap statistics, shopping carts, timelines, message queues, lotteries, likes, tags, product filtering, follow relationships, and ranking, all illustrated with concrete code examples.

Top Architect
Top Architect
Top Architect
Redis Practical Use Cases: Caching, Distributed Locks, Global IDs, Counters, Rate Limiting, Bitmaps, Shopping Cart, Timeline, Message Queue, and More

The article, authored by a senior architect, outlines a collection of practical Redis patterns that can be applied to build high‑performance backend services.

1. Caching

String type is used for hot‑data caching such as reports, celebrity gossip, object cache, and full‑page cache to improve access speed.

2. Distributed Data Sharing

Redis, as an independent distributed service, enables data sharing across multiple applications, e.g., distributed sessions.

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

3. Distributed Lock

Use the SETNX command on a string key; it succeeds only when the key does not exist, returning true .

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 integer key with INCRBY to generate atomic incremental IDs, suitable for sharding scenarios.

5. Counter

Integer type with INCR can track page views, likes, etc.; data can be written to Redis first and later synced to the database.

6. Rate Limiting

Store a counter per visitor IP (or other identifier) using INCR ; if the count exceeds a threshold, reject the request.

7. Bit Statistics

String type BITCOUNT (bitmap) stores bits efficiently; useful for large‑scale statistics such as online user counts.

set k1 a
setbit k1 6 1
setbit k1 7 0
get k1

Bitwise operations like AND, OR, XOR, NOT are also supported.

BITOP AND destkey key1 key2 ...
BITOP OR destkey key1 key2 ...
BITOP XOR destkey key1 key2 ...
BITOP NOT destkey key

8. Shopping Cart

String or hash can model a cart; HINCRBY increments item quantity, HDEL removes items, HGETALL retrieves the whole cart.

9. User Timeline

Use a Redis list (a doubly linked list) to store timeline entries in order.

10. Message Queue

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

11. Lottery

Random selection can be done with SPOP on a set.

spop myset

12. Likes, Check‑in, Clock‑in

Use a set per post, e.g., like:t1001 , to store user IDs who liked the post.

sadd like:t1001 u3001   // like
srem like:t1001 u3001   // unlike
sismember like:t1001 u3001   // check
smembers like:t1001   // all likers
scard like:t1001   // like count

13. Product Tags

Maintain tags with sets, e.g., sadd tags:i5001 "clear display" .

14. Product Filtering

Combine sets with 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; intersections and differences help discover mutual follows and potential connections.

16. Ranking

Sorted sets store scores; ZINCRBY increments a news item's click count, and ZREVRANGE retrieves the top N items.

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

Overall, these patterns demonstrate how Redis can serve as a versatile backend component for caching, synchronization, counting, analytics, and real‑time features.

backend developmentRediscachingMessage QueueDistributed LockRate LimitingBitmaps
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.