Skip to main content
~

Set

RESP3

prefix: ~  ·  client type: set / Set / HashSet / frozenset

RESP3 type – requires HELLO 3 at connection startup to activate. RESP2 clients will not receive this type.

RESP3 sets encode unordered collections with test-for-existence semantics. Format: ~<count>\r\n followed by count elements. SMEMBERS, SUNION, SDIFF, and SINTER now return sets instead of arrays in RESP3. Clients return native set types (Python set, Ruby Set, Java HashSet) without needing to know the command name.

Details

Sets have the format ~<count>\r\n followed by count RESP elements.

Sets are semantically unordered. The protocol does not enforce that elements are unique, but the spec says implementations should handle duplicates gracefully (typically by inserting into a native Set type which deduplicates automatically).

Client implementations: Python: set() Ruby: Set (standard library) Java: HashSet<V> JavaScript (no native set for arbitrary types): Map with values as true

In RESP2, SMEMBERS returned an *-array. The client had to know the command name to decide whether to return a list or set. RESP3 makes this automatic.

Commands returning sets in RESP3: SMEMBERS, SUNION, SDIFF, SINTER, KEYS (also returns ~), HKEYS (also returns ~).

Wire encoding

Wire bytes (CRLF shown as \\r\\n)
~3\r\n+apple\r\n+banana\r\n+cherry\r\n
# represents {apple, banana, cherry} (unordered set)

Type information

Prefix byte~
VersionRESP3
Client typeset / Set / HashSet / frozenset
SpecificationRESP3 specification (GitHub) →

Redis commands returning this type

SMEMBERSSUNIONSDIFFSINTER

See Also