Skip to content

Set Commands

Set commands operate on Redis sets — unordered collections of unique strings. Sets are useful for membership testing, removing duplicates, and set algebra operations.

This page covers: SADD, SCARD, SDIFF, SISMEMBER, SMEMBERS, SMISMEMBER, SPOP, SRANDMEMBER, SREM, SSCAN


SADD

Add one or more members to a set.

📘 Redis Reference: SADD

XML Example

xml
<lettuce-redis:sadd doc:name="Add tags"
    config-ref="Redis_Config"
    key="article:123:tags"
    members="#[['redis', 'mulesoft', 'integration', 'tutorial']]"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key
membersList<String>YesMembers to add. Payload by default via @Content.

Output

Type: Long

The number of members actually added to the set. Members that were already in the set are not counted.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

SREM

Remove one or more members from a set.

📘 Redis Reference: SREM

XML Example

xml
<lettuce-redis:srem doc:name="Remove tags"
    config-ref="Redis_Config"
    key="article:123:tags"
    members="#[['draft', 'incomplete']]"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key
membersList<String>YesMembers to remove. Payload by default via @Content.

Output

Type: Long

The number of members removed from the set. Members that were not in the set are not counted.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

SMEMBERS

Get all members of a set.

📘 Redis Reference: SMEMBERS

XML Example

xml
<lettuce-redis:smembers doc:name="Get all tags"
    config-ref="Redis_Config"
    key="article:123:tags"/>

<!-- payload is now a List<String> of all members -->
<foreach collection="#[payload]">
    <logger level="INFO" message="Tag: #[payload]"/>
</foreach>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key

Output

Type: List<String>

All members of the set. Returns an empty list if the set does not exist.

Large Sets

For sets with many members, consider using Search Set Members which streams results incrementally via SSCAN.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

SISMEMBER

Check if a value is a member of a set.

📘 Redis Reference: SISMEMBER

XML Example

xml
<lettuce-redis:sismember doc:name="Check if user is admin"
    config-ref="Redis_Config"
    key="users:admins"
    member="user:123"/>

<choice>
    <when expression="#[payload == true]">
        <flow-ref name="admin-flow"/>
    </when>
    <otherwise>
        <flow-ref name="standard-user-flow"/>
    </otherwise>
</choice>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key
memberStringYesThe member to test

Output

Type: Boolean

  • true if the member is in the set
  • false if the member is not in the set or the set does not exist

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

SMISMEMBER

Check if multiple values are members of a set.

📘 Redis Reference: SMISMEMBER

XML Example

xml
<lettuce-redis:smismember doc:name="Check multiple permissions"
    config-ref="Redis_Config"
    key="user:123:permissions"
    members="#[['read', 'write', 'delete', 'admin']]"/>

<!-- payload is now a List<Boolean> with results in the same order as the input -->
<set-variable variableName="permissions"
    value="#[{read: payload[0], write: payload[1], delete: payload[2], admin: payload[3]}]"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key
membersList<String>YesMembers to test. Payload by default via @Content.

Output

Type: List<Boolean>

A list of boolean values, one for each input member, in the same order. Each value is:

  • true if the corresponding member is in the set
  • false if the corresponding member is not in the set

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

SCARD

Get the number of members in a set (cardinality).

📘 Redis Reference: SCARD

XML Example

xml
<lettuce-redis:scard doc:name="Count active sessions"
    config-ref="Redis_Config"
    key="sessions:active"/>

<logger level="INFO" message="Active sessions: #[payload]"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key

Output

Type: Long

The number of members in the set. Returns 0 if the set does not exist.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

SDIFF

Compute the difference between the first set and all subsequent sets.

📘 Redis Reference: SDIFF

XML Example

xml
<!-- Find users who have read permission but not write permission -->
<lettuce-redis:sdiff doc:name="Find read-only users"
    config-ref="Redis_Config"
    key="permissions:read"
    keys="#[['permissions:write']]"/>

<!-- payload contains members in "read" but not in "write" -->

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe first set (base set)
keysList<String>YesAdditional sets to subtract from the first set

Output

Type: List<String>

Members that are in the first set but not in any of the other sets.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEOne of the keys is not a set

SPOP

Remove and return one or more random members from a set.

📘 Redis Reference: SPOP

XML Example

xml
<!-- Pop a single random member -->
<lettuce-redis:spop doc:name="Draw winner"
    config-ref="Redis_Config"
    key="contest:entries"/>
<!-- payload is a String -->

<!-- Pop multiple random members -->
<lettuce-redis:spop doc:name="Draw three winners"
    config-ref="Redis_Config"
    key="contest:entries"
    count="3"/>
<!-- payload is a List<String> -->

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key
countIntegerNoNumber of members to pop. When omitted, pops one member.

Output

Type: Object (dynamic, resolved by metadata)

  • When count is not specified: String (the single popped member)
  • When count is specified: List<String> (the popped members)

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

SRANDMEMBER

Get one or more random members from a set without removing them.

📘 Redis Reference: SRANDMEMBER

XML Example

xml
<!-- Get one random member -->
<lettuce-redis:srandmember doc:name="Get random item"
    config-ref="Redis_Config"
    key="products:featured"/>

<!-- Get 5 unique random members -->
<lettuce-redis:srandmember doc:name="Get 5 unique items"
    config-ref="Redis_Config"
    key="products:featured"
    count="5"/>

<!-- Get 10 random members (may include duplicates) -->
<lettuce-redis:srandmember doc:name="Get 10 with duplicates allowed"
    config-ref="Redis_Config"
    key="products:featured"
    count="-10"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key
countIntegerNoNumber of members to return. Positive = unique results; Negative = duplicates allowed.

Output

Type: List<String>

Random members from the set. If count is not specified, returns a single-element list.

Count Sign Matters

  • Positive count: Returns up to count unique members (no duplicates)
  • Negative count: Returns exactly abs(count) members, duplicates allowed

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

SSCAN

Incrementally iterate set members using a cursor.

📘 Redis Reference: SSCAN

Automated Cursor Management

For most use cases, consider using Search Set Members instead, which handles cursor iteration automatically and streams all members. Use SSCAN directly only when you need fine-grained control over pagination.

XML Example

xml
<!-- Start a new scan (cursor 0) -->
<lettuce-redis:sscan doc:name="Scan set members"
    config-ref="Redis_Config"
    key="users:active"
    cursor="0"
    match="user:*"
    count="100"/>

<!-- payload contains a List<String> of members from this page -->
<!-- attributes.cursor contains the next cursor value -->
<logger level="INFO" message="Next cursor: #[attributes.cursor]"/>

<!-- Continue scanning with the next cursor -->
<lettuce-redis:sscan doc:name="Scan next page"
    config-ref="Redis_Config"
    key="users:active"
    cursor="#[attributes.cursor]"
    match="user:*"
    count="100"/>

Parameters

ParameterTypeRequiredDefaultDescription
keyStringYesThe set key to scan
cursorIntegerYesCursor position. Use 0 to start a new scan.
matchStringNoGlob-style pattern to filter members
countIntegerNoHint for the number of elements to return per iteration

Output

Type: List<String>

A list of members matching the pattern for this scan iteration.

Attributes: ScanAttributes — Contains the cursor field with the next cursor value. When cursor is 0, the scan is complete.

Errors

Error TypeCondition
REDIS:COMMANDGeneral command execution error
REDIS:WRONG_TYPEKey exists but is not a set

Released under the MIT License.