10 Powerful Redis Use Cases Beyond Simple Caching

This guide explores ten practical Redis scenarios—including login authentication, counters, fan following, leaderboards, anti‑scraping, message queues, browser history, distributed locks, user sign‑in tracking, and website UV statistics—showcasing commands and patterns to boost performance in real‑world applications.

Senior Tony
Senior Tony
Senior Tony
10 Powerful Redis Use Cases Beyond Simple Caching

Redis is often praised for caching, but its rich data structures enable many other backend solutions. Below are ten common scenarios, each with a brief description, relevant Redis commands, and example outputs.

1. Login Authentication

Store one‑time verification codes and tokens with SETEX to leverage key expiration.

redis> setex captchalogin|13436669876 60 3456
"OK"
redis> get captchalogin|13436669876
"3456"
redis> setex tokencheck|12345 86400 54321
"OK"
redis> get tokencheck|12345
"54321"

Use cases:

Send SMS code, store captchalogin|phone for 60 seconds.

Validate within the window; otherwise the code expires.

After login, store tokencheck|userId for 24 hours.

Subsequent requests verify the token.

2. Counters

High‑concurrency counters (likes, inventory) are efficiently handled with INCR / DECR and their BY variants.

redis> set article1 0
"OK"
redis> incr article1
(integer) 1
redis> decr article1
(integer) 0
redis> incrby article1 2
(integer) 2
redis> decrby article1 2
(integer) 0

3. Fan Following (Set)

Redis SET provides unordered, deduplicated collections with native set operations for common social‑graph queries.

redis> sadd Tony Mary
(integer) 1
redis> sadd Tony Lynn
(integer) 1
redis> smembers Tony
1) "Mary"
2) "Lynn"
redis> sinter Tony Tom
1) "Mary"
redis> sunion Tony Tom
1) "Mary"
2) "Lynn"
3) "Eric"
redis> sdiff Tony Tom
1) "Lynn"
redis> sdiff Tom Tony
1) "Eric"

4. Leaderboards (Sorted Set)

Sorted sets ( ZSET) store a score with each member, ideal for ranking and timelines.

redis> zadd 家电全品类 5.5 海尔
(integer) 1
redis> zadd 家电全品类 4.5 美的
(integer) 1
redis> zcard 家电全品类
(integer) 4
redis> zrevrange 家电全品类 0 -1 withscores
1) "海尔"
2) "5.5"
3) "美的"
4) "4.5"
...

5. Anti‑Scraping (Rate Limiting)

Limit actions per second using SET key "" EX 1 NX to allow only the first request.

redis> set createorder|userid|1234 "" EX 1 NX
"OK"   # first order succeeds
redis> set createorder|userid|1234 "" EX 1 NX
(nil)   # second order within a second is blocked
redis> set createorder|userid|1234 "" EX 1 NX
"OK"   # after a second, order succeeds again

6. Message Queue (List)

Use LPUSH for producers and RPOP for consumers to build a simple FIFO queue.

redis> lpush mybooks java
(integer) 1
redis> lpush mybooks mysql
(integer) 2
redis> rpop mybooks
"java"
redis> rpop mybooks
"mysql"

7. Browser History (Stack via List)

Push visited URLs onto a list and pop them to simulate the back button.

redis> lpush mybrowser sohu
(integer) 1
redis> lpush mybrowser sina
(integer) 2
redis> lpush mybrowser baidu
(integer) 3
redis> lpop mybrowser
"baidu"
redis> lpop mybrowser
"sina"
redis> lpop mybrowser
"sohu"

8. Distributed Lock

Acquire a lock with SET key value EX 10 NX. Release atomically with a Lua script that checks the owner.

redis> set mytasklock "tony" ex 10 nx
"OK"   # lock acquired by tony
redis> set mytasklock "tom" ex 10 nx
(nil)   # lock acquisition fails
redis> del mytasklock   # simple release (Lua needed for safety)

Production systems often use Redisson, which adds re‑entrancy and automatic renewal.

9. User Sign‑In (BitMap)

Store daily sign‑in status as bits; BITCOUNT yields total sign‑in days.

redis> setbit userid|1234|202312 0 1   # Dec 1 sign‑in
(redis) 0
redis> setbit userid|1234|202312 1 1   # Dec 2 sign‑in
(redis) 0
redis> bitcount userid|1234|202312
(integer) 3   # three days signed in

10. Website UV Statistics (HyperLogLog)

HyperLogLog provides approximate distinct counting with ~0.81 % error using only 12 KB.

redis> pfadd page1 user1
(integer) 1
redis> pfadd page1 user1
(integer) 0   # duplicate ignored
redis> pfcount page1
(integer) 4
redis> pfmerge page1and2 page1 page2
"OK"
redis> pfcount page1and2
(integer) 6

These patterns illustrate how Redis can solve a wide range of backend problems beyond simple caching, helping developers ace interview questions and build high‑performance services.

distributed-systemsPerformanceRedisData StructuresCaching Alternatives
Senior Tony
Written by

Senior Tony

Former senior tech manager at Meituan, ex‑tech director at New Oriental, with experience at JD.com and Qunar; specializes in Java interview coaching and regularly shares hardcore technical content. Runs a video channel of the same name.

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.