Redis is a fast, in-memory database mainly used for caching, sessions, queues, and real-time data.
This guide covers:
-
Redis installation on Windows
-
Connecting Redis with Node.js
-
Writing basic test cases
-
Interview questions & tips
What is Redis? ๐ค
Redis stands for Remote Dictionary Server.
It stores data in RAM, so it is very fast compared to databases like MySQL or MongoDB.
Common use cases:
-
Caching API responses
-
Login sessions
-
OTP storage
-
Rate limiting
Step 1: Install Redis
Option 1: Using Redis for Windows (Recommended)
Redis does not officially support Windows, but Memurai / Redis Windows build works perfectly.
Download:
๐ https://github.com/microsoftarchive/redis/releases
-
Download Redis-x64-xxx.msi
-
Install like a normal software
-
Keep default settings
Verify Installation
Open Command Prompt and run:
redis-server
If Redis starts, you’re good โ
Open another terminal:
redis-cli
ping
Output:
PONG
Option 2: Using Redis on Linux (WSL Recommended) ๐ง
If you're on Windows 10/11, the best and most stable way to use Redis is through WSL (Windows Subsystem for Linux).
โ Step 1: Install WSL
Open PowerShell as Administrator:
wsl --install
Restart your system after installation.
โ Step 2: Install Redis inside WSL
Open Ubuntu (WSL terminal) and run:
sudo apt update
sudo apt install redis-server -y
โ Step 3: Start Redis
sudo service redis-server start
โ Step 4: Verify Redis
redis-cli
ping
Output:
PONG
That means Redis is running successfully โ
Step 2: Install Redis Client in Node.js โ๏ธ
Create a Node.js project:
mkdir redis-demo
cd redis-demo
npm init -y
Install Redis package:
npm install redis
Step 3: Connect Redis with Node.js ๐
Create redisClient.js
const redis = require("redis");
const client = redis.createClient({
url: "redis://127.0.0.1:6379"
});
client.on("connect", () => {
console.log("Redis connected โ
");
});
client.on("error", (err) => {
console.error("Redis error:", err);
});
(async () => {
await client.connect();
})();
module.exports = client;
Step 4: Basic Redis Operations (SET / GET) ๐ฆ
Create index.js
const client = require("./redisClient");
async function redisDemo(params) {
// Set Value
await client.set("username", "dharmveer");
// Get Value
const value = await client.get("username");
console.log("Stored Value = ", value);
}
redisDemo();
Run:
node index.js
Output:
Redis connected โ
Stored Value = dharmveer
Step 5: Redis with Expiry (OTP / Cache Example) โฑ๏ธ
Create otpRedis.js
const client = require("./redisClient");
async function otp() {
const key = "otp:123";
const value = "123456";
const expireTimeInSec = 30;
// โ
Use setEx (recommended)
await client.setEx(key, expireTimeInSec, value);
// Check TTL immediately
let ttl = await client.ttl(key);
console.log(`TTL after SET: ${ttl} seconds`);
// Wait 10 seconds
setTimeout(async () => {
ttl = await client.ttl(key);
const val = await client.get(key);
console.log("TTL after 10 sec:", ttl);
console.log("OTP after 10 sec:", val);
}, 10000);
// Wait 35 seconds (expired)
setTimeout(async () => {
ttl = await client.ttl(key);
const val = await client.get(key);
console.log("TTL after 35 sec:", ttl);
console.log("OTP after 35 sec:", val);
}, 35000);
}
otp();
-
30 = seconds
-
Auto-delete after expiry
TTL (Time To Live) defines how long a key will exist in Redis before it automatically expires and gets deleted.
TTL returns:
| Value | Meaning |
| > 0 | Key has expiry |
| -1 | Key exists but NO expiry |
| -2 | Key does NOT exist |
Run:
node otpRedis.js
Output:
Redis connected โ
TTL after SET: 30 seconds
TTL after 10 sec: 20
OTP after 10 sec: 123456
TTL after 35 sec: -2
OTP after 35 sec: null
Step 6: Redis Caching Example (API Cache) ๐
Create redisCaching.js
const client = require("./redisClient");
async function getUser(id) {
const cache = await client.get(`user:${id}`);
if (cache) {
return JSON.parse(cache);
}
const user = { id, name: "Demo User" }; // DB call
await client.setEx(`user:${id}`, 60, JSON.stringify(user));
return user;
}
getUser(21).then((res) => console.log(res));
getUser(22).then((res) => console.log(res));
getUser(23).then((res) => console.log(res));
Run:
node redisCaching.js
Output:
Redis connected โ
{ id: 21, name: 'Demo User' }
{ id: 22, name: 'Demo User' }
{ id: 23, name: 'Demo User' }
Step 7: Basic Testing Cases ๐งช
Install Jest
npm install --save-dev jest
Create Test File redis.test.js
const client = require("./redisClient");
test("Redis SET & GET", async () => {
await client.set("testKey", "dharmveer");
const value = await client.get("testKey");
expect(value).toBe("dharmveer");
});
Run:
npx jest
Output:
console.log
Redis connected โ
at Class.log (redisClient.js:8:11)
PASS ./redis.test.js
โ Redis SET & GET (42 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.881 s
Ran all test suites.
Common Redis Commands (Interview Favorite) โญ
SET key value
GET key
DEL key
EXPIRE key 60
TTL key
FLUSHALL
Redis vs Database (Simple Difference) โ๏ธ
| Redis | Database |
| In-memory | Disk-based |
| Very fast | Slower |
| Temporary data | Permanent data |
| Cache & sessions | Business data |
Interview Questions & Answers ๐ฏ
1. Why use Redis?
๐ For high-speed caching, sessions, and temporary data.
2. Is Redis a database?
๐ Yes, but it is an in-memory key-value store.
3. Does Redis support TTL?
๐ Yes, using EXPIRE or setEx().
4. Redis vs MongoDB?
๐ Redis = speed & cache
๐ MongoDB = long-term storage
5. Where should Redis be used?
๐ OTPs, sessions, rate limiting, API cache
Interview Tips ๐ก
โ
Always explain Redis with real use cases
โ
Mention TTL & caching
โ
Explain performance benefits
โ
Show Node.js example if possible
Final Thoughts ๐
Redis is easy to learn, powerful, and highly demanded in backend interviews.
If you’re building scalable Node.js apps, Redis is a must-have skill.
.webp)






