antirez/redis — Issue #339: SRANDMEMBER in 2.6 unstable scripting -Write commands not allowed after non deterministic commands 
Author: mvreg
Created: 2012-02-17 16:26:44+00:00
State: open

In 2.6 unstable we receive following error for the script, that was running in redis scripting branch: (based on 2.4) "write commands not allowed after non deterministic commands"

We believe the problem is in SRANDMEMBER, that picks random record and is as such unusable for us (worked in scripting unstable, now redis has more strict lua code checking).

Example that fails:
eval "redis.call('sadd','testkey','somevalue') \n redis.call('srandmember','testkey') \n return redis.call('set','testkey5','1')" 0
ERR Error running script (call to f_4fc1fa16885f314de7659a11b851b82ff2e434bb): Write commands not allowed after non deterministic commands

Since SRANDMEMBER is based on random generator, it is non deterministic.. However if SRANDMEMBER or similar function would have predetermined number-seed or we could get record from set by offset, we could easy create workaround.

Proposed solution: adding new command to get SMEMBERBYOFFSET(key, offsetnumber) or SRANDMEMBERBYSEED(key, randomseedid)

Our solution ideas - any one would be ok, since we don't find any other workaround.

--SMEMBERBYID(key,recordoffset)
math.randomseed(tonumber(ARGV[1]))
local totalrecords=SCARD(datakey)
local randomid=math.random(totalrecords)
local randommember=SMEMBERBYID(datakey,randomid) --NEW COMMAND REQUIRED TO GET RANDOM RECORD USING PRESELECTED ID

--SRANDMEMBERBYSEED(key,seed)
math.randomseed(tonumber(ARGV[1]))
local randomseedid=math.random()
local randommember=SRANDMEMBERBYBYSEED(datakey,randomseedid) --NEW COMMAND REQUIRED TO GET RANDOM RECORD USING PRESELECTED ID