Copied!
Node
Redis

Redis Sets in Node.js — Manage Unique Data with Ease

redis-sets-in-node.js
Shahroz Javed
Nov 06, 2025 . 31 views

Table Of Contents

 

Redis Sets are perfect for storing unique, unordered values — such as user tags, categories, or active session IDs. In this tutorial, you'll learn how to use Redis Sets in Node.js to add, check, retrieve, and remove unique items.

💡 Tip: A Redis Set automatically removes duplicates — making it ideal for unique lists or collections.

Introduction

A Redis Set is an unordered collection of unique strings. Unlike Redis Lists, Sets ensure that each item appears only once, no matter how many times you try to add it.

Sets are widely used to manage unique user IDs, hashtags, or categories — anything that shouldn’t repeat.

1. Install Redis Client

Make sure you have the official Redis client for Node.js installed:

npm install redis
    

2. Connect to Redis

Import and initialize the Redis client:

import { createClient } from "redis";

const client = createClient();
await client.connect();
console.log("Connected to Redis!");
    

3. Add Items to a Set

Use sAdd() to insert unique values into a Redis Set. If the same value already exists, Redis will simply ignore the duplicate.

// Add items to set
await client.sAdd("tags", "php", "laravel", "node", "php");
    

Even though "php" is added twice, it will only appear once in the set.

4. Retrieve All Unique Values

To get all items from a set, use sMembers():

const tags = await client.sMembers("tags");
console.log("Tags:", tags);
    

Output:

Tags: [ 'php', 'laravel', 'node' ]
    

5. Check If a Value Exists

To check if a value exists in the set, use sIsMember():

const hasLaravel = await client.sIsMember("tags", "laravel");
console.log("Contains laravel?", hasLaravel);
    

Output:

Contains laravel? true
    

6. Remove a Value from the Set

Use sRem() to remove one or more items:

await client.sRem("tags", "node");
console.log("Removed 'node' from tags");
    

7. Full Example

import { createClient } from "redis";
const client = createClient();
await client.connect();

// Add items to set
await client.sAdd("tags", "php", "laravel", "node", "php");

// Get all unique values
const tags = await client.sMembers("tags");
console.log("Tags:", tags); // php appears only once

// Check if value exists
const hasLaravel = await client.sIsMember("tags", "laravel");
console.log("Contains laravel?", hasLaravel);

// Remove value
await client.sRem("tags", "node");

await client.quit();
    
Redis Set Example Output
Redis Set operations example output in Node.js

Conclusion

Redis Sets provide a simple yet powerful way to handle unique collections of data. They are fast, memory-efficient, and ideal for things like user interests, tags, and categories. Combined with Node.js, Redis Sets help you maintain clean, duplicate-free datasets in real-time apps.

✅ Try combining Sets with Sorted Sets or Hashes to build powerful systems like leaderboards, tag managers, or unique user tracking.

20 Shares

Similar Posts