To utilize Redis beyond simple key-value caching by leveraging its advanced data structures for high-performance use cases like leaderboards, queues, and real-time analytics.
- Lists/Streams: For message queues or activity feeds.
- Sets: For unique collections (e.g., tags, online users).
- Sorted Sets: For leaderboards or priority queues.
- Hashes: For storing object-like data (e.g., user profiles) efficiently.
- HyperLogLog: For probabilistic counting (e.g., unique visitors) with minimal memory.
Using ioredis (Node.js):
const Redis = require('ioredis');
const redis = new Redis(process.env.REDIS_URL);Use ZADD to add scores and ZREVRANGE to get top users.
// Add user score
await redis.zadd('leaderboard', 100, 'user:1');
await redis.zadd('leaderboard', 150, 'user:2');
// Get top 10
const topUsers = await redis.zrevrange('leaderboard', 0, 9, 'WITHSCORES');Use HSET and HGETALL.
// Store user profile
await redis.hset('user:100', {
name: 'John',
email: 'john@example.com',
login_count: 5
});
// Retrieve
const user = await redis.hgetall('user:100');Simple atomic counter pattern.
const key = `rate_limit:${ip}`;
const current = await redis.incr(key);
if (current === 1) {
await redis.expire(key, 60); // 1 minute window
}
if (current > 100) throw new Error("Rate limit exceeded");- Memory: Redis stores everything in RAM. Use
maxmemorypolicy (e.g.,allkeys-lru) to evict old data if cache fills up. - Persistence: Configure RDB or AOF if data durability is required (not just caching).
Efficient data storage and retrieval patterns tailored to specific application needs, reducing load on the primary database.