The Redis SCARD command returns the set cardinality (number of elements) of the set stored at key.

Syntax

The basic syntax of the SCARD command is as follows:

1
SCARD key

Available Since

Redis version >= 1.0.0

Time Complexity

O(1)

ACL Categories

@read, @set, @fast

Return Value

Returns the cardinality (number of elements) of the set, or 0 if the key does not exist.

Example 1

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> SADD s_db redis
(integer) 1
127.0.0.1:6379> SADD s_db hbase
(integer) 1
127.0.0.1:6379> SADD s_db mysql
(integer) 1
127.0.0.1:6379> SCARD s_db
(integer) 3
127.0.0.1:6379>

Example 2

If the key does not exist, it returns 0.

1
2
3
4
5
127.0.0.1:6379> EXISTS ss
(integer) 0
127.0.0.1:6379> SCARD ss
(integer) 0
127.0.0.1:6379>

In the example above, we first check if the key(ss) exists. If it doesn’t, the EXISTS command returns 0. Next, we execute the SCARD command, which also returns 0.