w3resource

Redis Sets: SPOP

Redis SPOP Command

Redis SPOP command is used to remove and return one or more random member from set stored at specified key.

Here are some common uses and scenarios for the "SPOP" command:

  • Random Removal: Removes and returns a random member from a set.
  • Sampling: Provides a way to sample elements from a set without knowing the elements beforehand.
  • Load Balancing: Helps in distributing elements or tasks randomly across different systems or processes.
  • Temporary Data Removal: Useful for cases where temporary data needs to be removed and processed.
  • Testing: Assists in testing scenarios where random data access is required.

Syntax:

SPOP KEY 

Available since:

1.0.0.

Return Value:

String reply, the removed element, or nil when a key does not exist.

Return Value Type:

Integer

Example: Redis SPOP: Imported random member from the set

127.0.0.1:6379> SADD mycolor1 "red" "green" "blue"
(integer) 3
127.0.0.1:6379> SPOP mycolor1
"red"
127.0.0.1:6379> SMEMBERS mycolor1
1) "green"
2) "blue"
127.0.0.1:6379> SPOP mycolor1
"green"
127.0.0.1:6379> SMEMBERS mycolor1
1) "blue"

Previous: Redis Sets SMOVE
Next: Redis Sets SRANDMEMBER



Follow us on Facebook and Twitter for latest update.