Databases 6 min read

Beyond Caching: 10 Powerful Redis Patterns for Distributed Systems

This guide explores ten practical Redis non‑cache patterns—including distributed locks, message queues, leaderboards, real‑time counters, Bloom filters, session stores, distributed counters, rate limiting, unique ID generation, and delay queues—detailing their data structures, key commands, and typical application scenarios.

Ray's Galactic Tech
Ray's Galactic Tech
Ray's Galactic Tech
Beyond Caching: 10 Powerful Redis Patterns for Distributed Systems

1. Distributed Lock

Use Redis to solve resource contention across multiple nodes, such as flash sales or inventory deduction.

SET lock_key unique_value NX PX 5000  # acquire lock with expiration
DEL lock_key                     # release lock

NX guarantees mutual exclusion; PX sets an expiration to avoid deadlocks.

For complex scenarios, consider Redisson or the Redlock algorithm.

2. Message Queue

Redis List, Pub/Sub, and Stream can all implement queue functionality.

List queue example:

LPUSH queue "task1"
BRPOP queue 0   # block and fetch task

Stream queue example:

XADD mystream * key1 value1
XREAD COUNT 1 STREAMS mystream 0

Typical uses: asynchronous task processing and service decoupling.

3. Leaderboard

Maintain ordered sets with scores using Sorted Set.

ZADD game_rank 100 player1
ZADD game_rank 200 player2
ZREVRANGE game_rank 0 9 WITHSCORES   # top 10

Common in gaming rankings and hot lists.

4. Real‑time Systems

Redis excels at real‑time statistics, geolocation queries, and online status tracking.

Counter example:

INCR page_view:user:1001   # increment page view count

Online status (bitmap) example:

SETBIT online_users 1001 1
BITCOUNT online_users

Geolocation example:

GEOADD city 116.40 39.90 "Beijing"
GEORADIUS city 116.40 39.90 10 km

Applications: real‑time analytics, location services, online user tracking.

5. Bloom Filter

Use the RedisBloom module for large‑scale deduplication and cache‑penetration protection.

BF.ADD myfilter user123
BF.EXISTS myfilter user123

Ideal for preventing duplicate entries and shielding caches from miss storms.

6. Session Store

Address distributed session sharing by storing session data in a hash.

HSET session:abcd1234 user_id 1001 last_login "2025-08-31"
HGETALL session:abcd1234

Enables consistent session access across multiple services.

7. Distributed Counter

Handle high‑concurrency counting such as likes or stock levels.

INCRBY counter:like:article:123 1
DECRBY stock:product:1001 1

Useful for like counts, inventory decrement, etc.

8. Rate Limiting

Implement sliding‑window rate limiting using a counter with an expiration.

INCR req:uid:1001
EXPIRE req:uid:1001 60   # limit requests within 60 seconds

Prevents API abuse and protects services from burst traffic.

9. Distributed ID Generator

Generate globally unique IDs using INCR combined with timestamps. INCR order:id Ensures unique identifiers across distributed components.

10. Delay Queue

Use a Sorted Set where the score stores the execution timestamp.

ZADD delay_queue 1693478400 "task1"   # timestamp as score
ZRANGEBYSCORE delay_queue 0 now

Suitable for scheduled tasks and delayed message delivery.

In summary, Redis offers a versatile toolbox beyond simple caching, covering locking, queuing, ranking, real‑time analytics, probabilistic data structures, session management, counting, throttling, unique ID generation, and delayed execution, thereby satisfying most high‑concurrency system requirements.

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.

redisData StructuresNon‑Cache Patterns
Ray's Galactic Tech
Written by

Ray's Galactic Tech

Practice together, never alone. We cover programming languages, development tools, learning methods, and pitfall notes. We simplify complex topics, guiding you from beginner to advanced. Weekly practical content—let's grow together!

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.