0
0
Redisquery~3 mins

ZCARD for set size in Redis

Choose your learning style9 modes available
Introduction
ZCARD helps you find out how many items are in a sorted set. It tells you the size quickly without listing all items.
You want to know how many players are in a game leaderboard.
You need to check how many tasks are in a priority queue.
You want to count how many products are in a sorted catalog.
You want to monitor how many users have scored points today.
You want to limit actions based on the number of items in a set.
Syntax
Redis
ZCARD key
The key is the name of the sorted set you want to check.
ZCARD returns an integer representing the number of elements.
Examples
Returns the number of players in the 'leaderboard' sorted set.
Redis
ZCARD leaderboard
Returns how many tasks are in the 'tasks' sorted set.
Redis
ZCARD tasks
Sample Program
We add three players with scores to the 'leaderboard' sorted set, then count how many players are in it.
Redis
ZADD leaderboard 100 "Alice"
ZADD leaderboard 200 "Bob"
ZADD leaderboard 150 "Carol"
ZCARD leaderboard
OutputSuccess
Important Notes
If the key does not exist, ZCARD returns 0.
ZCARD only works with sorted sets, not regular sets or other data types.
Summary
ZCARD returns the number of elements in a sorted set.
It is useful to quickly check the size without fetching all items.
If the sorted set is empty or missing, it returns 0.