EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Redis Tutorial Redis SADD
 

Redis SADD

Updated February 18, 2023

Redis SADD

 

 

Introduction to Redis SADD

Redis sadd command is basically used to add members to set which was stored in the key. If a member already exists, it will be ignored when adding a new member. If the key of a member does not exist, the set will be created and members will be added to it. If the value we have stored for this key is not set, an error is returned.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Key Takeaways

  • Redis sadd is used to add the members to the key which we have stored. The return value of redis sadd is an integer.
  • The number of integer elements that we are adding to the set, the same elements are not included in the elements which were present in the set.

What is Redis SADD?

As we know that redis is implemented by using C and it contains an event-driven model similar to the nodejs. The basic difference between nodejs and redis is that nodejs is used libuv and redis contain its own wrapper. The libae will wrap the epoll or select as per our platform or it will also depend on the compile configuration.

Below is the syntax of redis sadd as follows:

Syntax:

SADD name_of_key val1 val2 ….. valN

In the above syntax, the sadd is nothing but the redis sadd which we are using to add the members. Name of the key is defined as the key name which we are using at the time of adding members. Value is nothing but the value which we have defined for the member.

How to Create Redis SADD and Add Elements?

The below steps show how we are creating the redis sadd and adding the elements as follows. In the first step, we are starting the redis server.

1. For creating the redis sadd and add elements first we are starting the server by using the following command as follows.

Code:

redis-server

Output:

Redis SADD - Add elements

2. After starting the redis server now in this step we are checking the status of the server.

Code:

ps -ef | grep -i redis

Output:

Redis SADD - Server Status

3. After checking the status of the server now in this step we are logging into the redis cli for creating redis sadd and add elements.

Code:

redis-cli

Output:

Creating Redis SADD

4. After logging in redis cli in the below example we are creating it by adding 3 members as follows. We are creating the three members set. We are defining the name of set as redis_set and giving the name of members as mem1, mem2, mem3.

Code:

SADD redis_set mem1 mem2 mem3

Output:

Redis SADD - Defining the name

5. After creating it now in this step we are adding a single element to the already created redis sadd. We are adding mem4 to the already created sadd.

Code:

SADD redis_set mem4
SMEMBERS redis_set

Output:

Redis SADD - Single Element

6. In the below example we are adding three members in a single command to the already created sadd. We are adding mem5, mem6, and mem7 members to the redis_set sadd.

Code:

SADD redis_set mem5 mem6 mem7
SMEMBERS redis_set

Output:

Redis SADD - Adding members

How to Use Redis SADD Multiple Elements?

We’re using redis sadd to add multiple elements. We can add multiple elements when creating a new sadd, or we can add new elements after the sadd has been created. In the example below, we can see that we are adding multiple elements when creating a new sadd. In the below example we are creating a new sad name as redis_sadd and adding the elements to the sadd name as sadd1, sadd2, sadd3, and sadd4.

Code:

SADD redis_sadd sadd1 sadd2 sadd3 sadd4
SMEMBERS redis_sadd

Output:

Redis SADD - Multiple Elements

In the above example, we have added multiple elements at the time of creating the new sadd now in the below example we are adding multiple elements into the already created sadd. In the below example, we are adding elements into the redis_sadd and adding the element’s names as sadd5, sadd6, sadd7, and sadd8.

Code:

SADD redis_sadd sadd5 sadd6 sadd7 sadd8
SMEMBERS redis_sadd

Output:

Redis SADD - Element names

In the below example we are adding only two elements into the redis_sadd. We are adding elements named sadd9 and sadd10.

Code:

SADD redis_sadd sadd9 sadd10
SMEMBERS redis_sadd

Output:

Redis SADD - Two elements

Redis SADD Set Command

Redis set is referring to the value collection which was given into the key. Each item in the set is known as a member. So we can say that set is now allowing duplicate values. For creating set in redis we are using the sadd command. In the below example, we are creating the set name as redis_set and defining the member of set as set1, set2 and set3.

Code:

SADD redis_set set1 set2 set3

Output:

Set name

In the redis set command while adding list type set members in the group it will return an error.

Code:

LPUSH redis_key "SQL"
SADD redis_key DB

Output:

return an error

Basically, the redis set does not contain duplicate values; if we give it a duplicate value, it will take the first one and ignore the others.

Code:

SADD redis_set set4 set5 set5 set6 set4
SMEMBERS redis_set

Output:

Duplicate Value

To get all members from the redis set we are using the following command as follows. We are using the SMEMBERS command.

Code:

SMEMBERS redis_set

Output:

Redis SADD Members

To get the random members from the set we need to specify the number which set we have required as follows.

Code:

SRANDMEMBER redis_set 5

Output:

Specify the number

We are removing members from set by using the SREM command. In the below example, we are removing mem1 member as follows.

Code:

SREM redis_set mem1
SMEMBERS redis_set

Output:

removing members from set

Using the SPOP command, we can remove the random member from the set, it will remove the member that we defined.

Code:

SPOP redis_set 3
SMEMBERS redis_set

Output:

remove the random member

Examples of Redis SADD

Given below are the examples mentioned:

Example #1

In the below example, we are creating a redis sadd name as color and we are adding red, black, and white colors into it.

Code:

SADD color red black white
SMEMBERS color

Output:

Redis SADD Name

Example #2

In the below example, we are adding the pink, blue, and green colors into the sadd name as color.

Code:

SADD color pink blue green
SMEMBERS color

Output:

adding pink, blue and green

Example #3

In the below example, we are adding duplicate elements to the sadd but it will not add it will return zero values.

Code:

SADD color red black white
SMEMBERS color

Output:

duplicate elements

Conclusion

Redis sadd command is basically used to add members to set which was stored in the key. If suppose members already exist then it will ignore to add the new member. We are using two parameters while using redis sadd first is name of set and another is the member name.

Recommended Articles

This is a guide to Redis SADD. Here we discuss the introduction, how to create redis SADD, and add elements. set command and examples. You may also have a look at the following articles to learn more –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW