Redis Use Cases
Redis's speed and rich data structures make it useful for far more than simple caching. In production systems it commonly plays the role of application cache, session store, rate limiter, real-time leaderboard, pub/sub message bus, and lightweight job queue — often several of these at once within the same architecture. Understanding which data structure and expiration strategy fits each use case is the key skill for using Redis effectively.
Cricket analogy: A franchise's IPL support staff includes a physio, a strategist, and a data analyst all working from the same dugout — similarly, one Redis instance can simultaneously serve as cache, session store, and leaderboard engine for an application.
Caching
The most common Redis use case is caching results of expensive operations — a slow SQL join, a third-party API call, or a rendered HTML fragment — so subsequent requests can be served from memory instead of recomputing the result. A typical pattern is cache-aside: the application checks Redis first (GET), and on a miss, queries the database, then stores the result in Redis with an expiration using SETEX or SET with the EX option so stale data eventually falls out automatically.
Cricket analogy: A commentator keeps a pre-calculated table of each batter's career average on a card rather than recalculating it from raw scorecards every time it's mentioned on air, refreshing the card only occasionally — exactly like cache-aside with a TTL.
Session Storage and Real-Time Leaderboards
Web applications often store user session data — login state, cart contents, feature flags — in Redis rather than in the application server's local memory, because Redis is shared across all server instances behind a load balancer, letting any server handle any request for a logged-in user. Separately, Redis's Sorted Set type is a natural fit for real-time leaderboards: ZADD inserts or updates a player's score, ZINCRBY increments it after each game, and ZREVRANK or ZREVRANGE instantly returns a player's rank or the top N players without any recalculation.
Cricket analogy: A player's live tournament standing updates instantly after every match thanks to a running points table, rather than being recalculated from scratch at season's end — the same instant-update mechanic ZINCRBY gives a Redis leaderboard.
# Cache-aside pattern in a Node.js-style pseudocode using node-redis
const cached = await redis.get(`user:${id}:profile`);
if (cached) return JSON.parse(cached);
const profile = await db.query('SELECT * FROM users WHERE id = $1', [id]);
await redis.set(`user:${id}:profile`, JSON.stringify(profile), { EX: 300 }); // 5 min TTL
return profile;
# Leaderboard with a Sorted Set
ZADD game:leaderboard 4200 "player:88"
ZINCRBY game:leaderboard 150 "player:88"
ZREVRANGE game:leaderboard 0 9 WITHSCORES # top 10
ZREVRANK game:leaderboard "player:88" # player's current rankStoring session data or leaderboard state in Redis without configuring persistence or replication means a Redis restart or crash can wipe active user sessions or leaderboard progress. For anything beyond a pure cache, enable AOF persistence and consider Redis Sentinel or Cluster for high availability.
- Redis commonly serves as a cache, session store, rate limiter, leaderboard engine, and pub/sub bus.
- The cache-aside pattern checks Redis first, falls back to the database on a miss, and repopulates Redis with a TTL.
- Session storage in Redis lets any server behind a load balancer serve any logged-in user.
- Sorted Sets (ZADD, ZINCRBY, ZREVRANGE, ZREVRANK) are the natural data structure for real-time leaderboards.
- Rate limiting can be implemented with INCR and EXPIRE to count requests within a sliding or fixed time window.
- Redis Pub/Sub (PUBLISH/SUBSCRIBE) enables lightweight real-time messaging between application components.
- Non-cache use cases (sessions, leaderboards) need persistence and high-availability planning, unlike pure ephemeral caches.
Practice what you learned
1. What is the 'cache-aside' pattern?
2. Which Redis data structure is best suited for a real-time leaderboard?
3. Why is Redis a good fit for session storage in a load-balanced web app?
4. Which pair of commands is commonly used together to implement basic rate limiting in Redis?
5. What is a key risk of using Redis for session storage without persistence or high availability?
Was this page helpful?
You May Also Like
What Is Redis?
An introduction to Redis as an in-memory data structure store, covering what makes it different from disk-based databases and why it's become a staple of modern backend systems.
Redis Data Persistence: RDB and AOF
How Redis persists in-memory data to disk using RDB snapshots and the AOF log, and how to choose (or combine) the right strategy for your workload.
The Redis CLI
How to use redis-cli, the command-line client bundled with Redis, to run commands interactively, inspect keys, monitor traffic, and script common operations.