Skip to main content

SDIFFCARD

Get set difference cardinality.
2 min read

Use SDIFFCARD to count the members of the first set that are not present in any of the other sets. It returns only the count; use SDIFF to retrieve the members.

Keys that do not exist count as empty sets, so a missing first key returns 0. The command does not modify the source sets or store the difference.

LIMIT stops counting once that many members have been found and returns the limit. For example, LIMIT 1 checks whether the difference contains any members. Omitting LIMIT or using LIMIT 0 returns the full count.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<numkeys>YesNoPositive integer specifying how many key arguments follow.
<key>YesYesSet key. Members of all subsequent sets are excluded from the first set.
LIMIT <limit>NoNoNon-negative integer that caps the returned count; 0 means no limit.

Important points#

  • numkeys must equal the number of key arguments that immediately follow it.
  • Key order matters: the command subtracts the other sets from the first set.
  • With one key, the command returns that set's cardinality, capped by a positive LIMIT.

Response#

The number of members in the difference, capped by LIMIT when it is positive.

ProtocolReply
RESP2Integer
RESP3Integer

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. Client examples query set1 and set2 from the Redis CLI example.

Redis CLI

The difference contains a and b, so its cardinality is 2.

@upstash/redis
Note

This command is not supported yet in @upstash/redis.

upstash_redis
Note

This command is not supported yet in upstash_redis.

ioredis
node-redis
redis-py
go-redis
jedis
redis-rs