Sets

Sets #

21 commands: membership, the three set-algebra families, expiry, and deterministic SPOP.

CmdRWSyntaxReturnsExample → reply
SADDWSADD KEY MEMBER [MEMBER ...]integer members addedSADD tags go db → 2
SREMWSREM KEY MEMBER [MEMBER ...]integer members removedSREM tags db → 1
SCARDRSCARD KEYinteger cardinalitySCARD tags → 1
SISMEMBERRSISMEMBER KEY MEMBERinteger 0/1SISMEMBER tags go → 1
SMEMBERSRSMEMBERS KEYarray of membersSMEMBERS tags → [go]
SDIFFRSDIFF KEY [KEY ...]array members in the first but no later setSDIFF a b → [x]
SDIFFSTOREWSDIFFSTORE dest KEY [KEY ...]integer size of the resultSDIFFSTORE out a b → 1
SINTERRSINTER KEY [KEY ...]array members in every setSINTER a b → [y]
SINTERSTOREWSINTERSTORE dest KEY [KEY ...]integer size of the resultSINTERSTORE out a b → 1
SUNIONRSUNION KEY [KEY ...]array members in any setSUNION a b → [x,y]
SUNIONSTOREWSUNIONSTORE dest KEY [KEY ...]integer size of the resultSUNIONSTORE out a b → 2
SMOVEWSMOVE source destination MEMBERinteger 1 moved, 0 not a memberSMOVE a b x → 1
SPOPWSPOP KEY [count]bare: bulk member; with count: arraySPOP queue:ready → job:7
SRANDMEMBERRSRANDMEMBER KEY [count]bare: bulk; positive count: distinct array; negative: array with repeatsSRANDMEMBER tags 1 → [go]
SKEYEXISTSRSKEYEXISTS KEYinteger 0/1 — does the set existSKEYEXISTS tags → 1
SCLEARWSCLEAR KEYinteger members removedSCLEAR tags → 1
SMCLEARWSMCLEAR KEY [KEY ...]integer sets removedSMCLEAR a b → 2
SEXPIREWSEXPIRE KEY secondsinteger 1/0SEXPIRE tags 60 → 1
SEXPIREATWSEXPIREAT KEY unixTimeinteger 1/0SEXPIREAT tags 1767225600 → 1
STTLRSTTL KEYinteger seconds left, -1 noneSTTL tags → 59
SPERSISTWSPERSIST KEYinteger 1 removed, 0 noneSPERSIST tags → 1

Notes #

  • SPOP is deterministic across replicas. Because it is a write command, its result must be identical on every node, so the picked member is derived from the Raft-seeded state rather than math/rand. With count, the count is clamped to the set size; count = 0 answers an empty array and a negative count is an error.
  • SRANDMEMBER is a read command and may use real randomness (count distinct members when positive, repeats when negative). |count| is capped at 1048576.
  • SMOVE to the same key is a no-op that returns 1 and keeps the TTL.
  • SCLEAR/SMCLEAR delete whole sets and their expiration; SREM removes members.
  • SPERSIST is the only *PERSIST command in the engine, and it is known to leave the expiration in place on the ipfs backend. Strings, hashes and lists have no PERSIST at all.
  • Set algebra commands read the sets named in the arguments; *STORE variants overwrite the destination and return the size of the result.