0
votes

Sets are normally not ordered by nature. But I wonder if SMEMBERS in Redis is backed by a data structure that ensures the order.

Nothing is documented in this short doc: https://redis.io/commands/smembers

and the notes in EVAL are not easy to understand.

In Redis version 4, commands that may return elements in random order, like SMEMBERS (because Redis Sets are unordered) have a different behavior when called from Lua, and undergo a silent lexicographical sorting filter before returning data to Lua scripts. So redis.call("smembers",KEYS[1]) will always return the Set elements in the same order, while the same command invoked from normal clients may return different results even if the key contains exactly the same elements. However starting with Redis 5 there is no longer such ordering step, because Redis 5 replicates scripts in a way that no longer needs non-deterministic commands to be converted into deterministic ones. In general, even when developing for Redis 4, never assume that certain commands in Lua will be ordered, but instead rely on the documentation of the original command you call to see the properties it provides.

Shortly, does redis ensure the order of the results at each call to SMEMBERS ?

The redis version we use:

127.0.0.1:6379> info
# Server
redis_version:5.0.3
1

1 Answers

0
votes

NO, the result is NOT deterministic.

I wonder if SMEMBERS in Redis is backed by a data structure that ensures the order.

The underlying data structure is HASH table, which is NOT ordered.