Back to results for “SQL/Sequelize)”
Node.js › Caching with Redis

How do you connect a Node.js application to Redis using the official node-redis client?

Beginner

Answer

The node-redis package (v4+) provides a promise-based client -- you create a client with createClient(), specifying the connection URL, call connect() to establish the connection, and then use async methods like get(), set(), and del() to interact with Redis, with the client automatically handling connection pooling internally for you.
const { createClient } = require('redis');

const client = createClient({ url: process.env.REDIS_URL || 'redis://localhost:6379' });
client.on('error', (err) => console.error('Redis error:', err));
await client.connect();

await client.set('greeting', 'hello');
const value = await client.get('greeting');
Real-world example A Node.js API establishes a single shared Redis client at application startup, reusing that one connection across every request handler rather than creating a new connection per request, which would quickly exhaust Redis's available connections under load.

Common follow-ups: Why is it important to attach an 'error' event listener to the Redis client to avoid unhandled errors crashing the process?;How does connection pooling work internally with the node-redis client versus needing to manage a pool manually?