Copied!
Node
Redis

Best Redis Basics Guide for Node.js Developers

best-redis-basics-guide-for-node.js
Shahroz Javed
Nov 04, 2025 . 25 views

Table Of Contents

 

Redis is one of the fastest in-memory data stores, widely used for caching, storing sessions, and improving the performance of backend systems. In this guide, you’ll learn the basic Redis operations using the official Node.js redis package — from installation to connecting, storing, and managing data.

⚠️ Note: Redis must be installed and running locally (default URL: redis://127.0.0.1:6379) before running any Node.js code below.

Introduction

Redis stands for REmote DIctionary Server. It is a high-speed key-value database that stores data in memory instead of disk, making it extremely fast. Developers use Redis for:

💡 Related: Learn how to Set Up MongoDB with Node.js for persistent data storage.

Installing Redis in Node.js

You can install the official Redis client from npm using the command below:

npm install redis
    

Once installed, you can import the package and create a Redis client to start performing basic operations.

Connecting to Redis

Here’s how to connect to Redis in Node.js using the createClient function:

import { createClient } from "redis";

const client = createClient({
  url: "redis://127.0.0.1:6379",
});

client.on("error", (err) => console.log("Redis Error", err));

await client.connect();
    

The createClient() method connects your Node.js app to Redis. If Redis isn’t running, you’ll get a connection error, which is handled by the client.on("error") event listener.

Basic Redis Commands in Node.js

Once connected, you can start performing operations like setting values, retrieving them, and deleting keys.

// Set key with expiry (EX = expiry in seconds)
await client.set('otp:1234', '4567', { EX: 60 });

// Basic set and get
await client.set('name', 'Ali');
console.log(await client.get('name')); // Output: Ali

// Delete key
await client.del('name');

// Set expiry to existing key
await client.expire('name', 60);

// Increment and decrement counters
await client.incr('count');
await client.decr('count');

// Append data to existing key
await client.append('mykey', 'new text');

// Close connection
await client.quit();
    

These commands allow you to perform basic CRUD-like operations in Redis. Since Redis is in-memory, the operations are almost instantaneous, ideal for fast temporary data storage.

Common Redis Use Cases

Redis is not just for caching. Here are some common use cases:

💡 Related: Check out our guide on Building a Rate Limiter in Node.js using Redis.

Output / Results

After running the above example, you can view your stored data in Redis CLI using:

redis-cli
> keys *
> get name
> ttl otp:1234
    
Redis CLI showing data
Redis CLI displaying stored keys and values

Conclusion

Redis is a powerful and lightweight database ideal for caching, real-time analytics, and temporary data storage. With the redis npm package, you can easily integrate it into any Node.js application and enhance performance instantly.

✅ Tip: Always close your Redis connection with client.quit() to prevent memory leaks or open sockets.

23 Shares

Similar Posts