How to Use Redis in Node.js: Simple Guide with Examples
To use
redis in Node.js, install the redis package, create a client with createClient(), then connect and use commands like set and get. Always handle connection errors and close the client when done.Syntax
Here is the basic syntax to use Redis in Node.js:
import { createClient } from 'redis';- imports the Redis client creator.const client = createClient();- creates a new Redis client instance.await client.connect();- connects the client to the Redis server.await client.set(key, value);- sets a value for a key.const value = await client.get(key);- retrieves the value for a key.await client.quit();- closes the connection.
javascript
import { createClient } from 'redis'; async function run() { const client = createClient(); client.on('error', (err) => console.log('Redis Client Error', err)); await client.connect(); await client.set('key', 'value'); const value = await client.get('key'); console.log(value); await client.quit(); } run();
Output
value
Example
This example shows how to connect to Redis, set a key, get its value, and then close the connection.
javascript
import { createClient } from 'redis'; async function example() { const client = createClient(); client.on('error', (err) => console.error('Redis Client Error', err)); await client.connect(); await client.set('greeting', 'Hello, Redis!'); const message = await client.get('greeting'); console.log(message); // Output the stored value await client.quit(); } example();
Output
Hello, Redis!
Common Pitfalls
Common mistakes when using Redis in Node.js include:
- Not awaiting
client.connect()before running commands, causing errors. - Ignoring connection errors by not listening to the
errorevent. - Forgetting to close the client with
client.quit(), which can keep the Node.js process running. - Using callbacks or older Redis clients instead of the modern
redispackage with promises.
javascript
/* Wrong way: Missing await on connect and no error handling */ import { createClient } from 'redis'; const client = createClient(); client.set('key', 'value'); // This will fail because client is not connected /* Right way: */ async function correct() { const client = createClient(); client.on('error', (err) => console.error('Redis Client Error', err)); await client.connect(); await client.set('key', 'value'); const val = await client.get('key'); console.log(val); await client.quit(); } correct();
Output
value
Quick Reference
| Command | Description |
|---|---|
| createClient() | Creates a new Redis client instance |
| client.connect() | Connects the client to Redis server |
| client.set(key, value) | Sets a string value for a key |
| client.get(key) | Gets the string value of a key |
| client.quit() | Closes the connection to Redis |
| client.on('error', callback) | Handles connection or command errors |
Key Takeaways
Always await client.connect() before running Redis commands in Node.js.
Use the official redis package with promises for modern, clean code.
Listen for 'error' events to catch connection or command issues.
Close the Redis client with client.quit() to free resources.
Use async/await syntax for clear and readable Redis operations.