A modern, high performance Redis client
The redis package (node-redis) is the official Node.js client for Redis, providing comprehensive support for all Redis commands and data structures. With over 7 million weekly downloads, it serves as the de facto standard for integrating Redis into JavaScript applications. The library supports strings, hashes, lists, sets, sorted sets, streams, geospatial indexes, and more through both raw Redis commands and JavaScript-friendly method names.
Version 5.x introduced significant architectural improvements including client-side caching via RESP3 protocol, modular extensions for Redis Stack features (JSON, Bloom filters, time series), and enhanced ergonomics with object-based command modifiers. The package is built around @redis/client as the core, with optional modules like @redis/bloom and @redis/json that extend functionality for Redis Stack deployments.
Developers use this package for caching layers, session stores, rate limiting, real-time analytics, message queues via pub/sub, and leaderboards with sorted sets. It handles connection pooling, automatic command queueing, error recovery, and provides event listeners for connection lifecycle management. The library works seamlessly with Redis Cluster, Redis Sentinel for high availability, and supports Lua scripting, transactions, and pipelining.
import { createClient } from 'redis';
const client = createClient({
url: 'redis://localhost:6379',
socket: {
reconnectStrategy: (retries) => Math.min(retries * 50, 500)
}
});
client.on('error', (err) => console.error('Redis Client Error', err));
await client.connect();
// String operations with expiration
await client.set('user:1001:profile', JSON.stringify({
name: 'Alice',
email: 'alice@example.com'
}), { EX: 3600 });
const profile = JSON.parse(await client.get('user:1001:profile'));
// Hash operations
await client.hSet('session:abc123', {
userId: '1001',
loginTime: Date.now().toString(),
ipAddress: '192.168.1.1'
});
const sessionData = await client.hGetAll('session:abc123');
// Sorted set for leaderboard
await client.zAdd('game:leaderboard', [
{ score: 9500, value: 'player1' },
{ score: 8700, value: 'player2' },
{ score: 12000, value: 'player3' }
]);
const topPlayers = await client.zRangeWithScores('game:leaderboard', 0, 2, { REV: true });
// Pub/sub messaging
const subscriber = client.duplicate();
await subscriber.connect();
await subscriber.subscribe('notifications', (message) => {
console.log('Received:', message);
});
await client.publish('notifications', 'New user registered');
// Cleanup
setTimeout(async () => {
await subscriber.unsubscribe('notifications');
await subscriber.quit();
await client.quit();
}, 5000);API response caching: Store expensive database query results or external API responses in Redis with TTL-based expiration, reducing latency from hundreds of milliseconds to single-digit response times. Use SET with EX option for automatic key expiration.
Session management: Persist user session data across multiple application servers using Redis hashes or JSON documents, enabling horizontal scaling without sticky sessions. The fast read/write performance handles thousands of concurrent user sessions efficiently.
Real-time leaderboards: Implement gaming or application leaderboards using sorted sets (ZADD, ZRANGE) where scores update in real-time and rankings are retrieved in logarithmic time complexity, even with millions of entries.
Rate limiting: Track API request counts per user/IP using incremented counters with expiration windows, implementing sliding window or token bucket algorithms to prevent abuse without external dependencies.
Pub/sub messaging: Build real-time features like chat applications, live notifications, or event broadcasting where publishers send messages to channels and multiple subscribers receive updates instantly without polling databases.
npm install redispnpm add redisbun add redis