Databases 8 min read

Common Redis Use Cases and Implementation Patterns

This article introduces a collection of practical Redis use cases—including caching, distributed sessions, locks, global IDs, counters, rate limiting, bitmaps, shopping carts, timelines, message queues, lotteries, likes, tags, product filtering, follow relationships, and ranking—explaining their data types, commands, and sample code.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Common Redis Use Cases and Implementation Patterns

Redis is a versatile in‑memory data store that can be applied to many scenarios; the following sections describe typical use cases and the corresponding Redis data structures and commands.

1. Caching – Use string keys to store hot data such as reports or frequently accessed objects, improving read performance.

2. Distributed Data Sharing – Because Redis runs as an independent service, it can be shared across multiple applications, e.g., for distributed session storage.

org.springframework.session
spring-session-data-redis

3. Distributed Lock – Implemented with the SETNX command; a lock is acquired only when the key does not exist.

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 the atomic INCRBY command to generate sequential IDs, e.g., incrby userid 1000 .

5. Counter – Simple integer increment via INCR , suitable for page views, likes, etc.; values can be written to Redis first and later synchronized to a database.

6. Rate Limiting – Store request counts per IP (or other identifier) using INCR ; reject requests when the count exceeds a threshold.

7. Bit Statistics (Bitmaps) – Use BITCOUNT and related bitmap commands to store binary flags efficiently, e.g., for online‑user statistics.

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

Bitwise operations such as BITOP AND , OR , XOR , and NOT enable set‑based analytics, for example calculating users online for seven consecutive days.

BITOP AND "7_days_both_online_users" "day_1_online_users" "day_2_online_users" ... "day_7_online_users"

8. Shopping Cart – Store cart data in a HASH where the key is the user ID, the field is the product ID, and the value is the quantity; operations include HINCRBY , HDEL , HGETALL , etc.

9. User Timeline – Use a LIST (double‑ended queue) to maintain an ordered timeline of events.

10. Message Queue – Redis lists provide blocking pop commands BLPOP and BRPOP for reliable queue consumption.

11. Lottery – Random selection can be performed with the set command SPOP .

spop myset

12. Likes, Check‑ins, and Clock‑ins – Model each post’s likes as a set like:postId ; add, remove, check membership, and count members with SADD , SREM , SISMEMBER , and SCARD .

13. Product Tags – Use a set tags:productId to store all tags associated with a product.

14. Product Filtering – Combine multiple sets with SINTER , SUNION , and SDIFF to filter products by brand, OS, screen size, etc.

15. Follow / Recommendation Model – Represent follow relationships with sets user:follow and user:fans ; intersections and differences reveal mutual follows or potential connections.

16. Leaderboard – Use sorted sets; increment a news item’s score with ZINCRBY and retrieve the top N items with ZREVRANGE including scores.

zincrby hotNews:20190926 1 n6001
zrevrange hotNews:20190926 0 15 withscores
RediscachingMessage QueueDistributed LockRate LimitingleaderboardBitmaps
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

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.