kissrain
kissrain

Reputation: 105

why redis cannot execute non deterministic commands in lua script

SPOP is not allowed to be executed in lua. And if you do some non-deterministic commands firstly, then you is not allowed to execute write commands. This seems confusing to me. So why redis has such limitation?

Upvotes: 2

Views: 5709

Answers (4)

Iman
Iman

Reputation: 18906

you can act like what proposed at the last of error description

Additional information: ERR Error running script (call to f_082d853d36ea323f47b6b00d36b7db66dac0bebd): @user_script:10: @user_script: 10: Write commands not allowed after non deterministic commands. Call redis.replicate_commands() at the start of your script in order to switch to single commands replication mode.

Call redis.replicate_commands() at the start of your script in order to switch to single commands replication mode. note that because of redis.call('time') call is non deterministic.

local function ticks(key,ticks) 
    redis.replicate_commands()
    local t = ticks
    local time = redis.call('time')
    local last=redis.call('get',key..'_last')
    if last==t then                                     
        local inc=redis.call('incr',key..'cnt')
        t  = t+inc
    else 
        redis.call('set',key..'cnt',0)                                    
    end         
    redis.call('set',key..'_last',t)
    return t
end   
return ticks(@key,@ticks)

Upvotes: 0

Gehad Mohamed
Gehad Mohamed

Reputation: 151

you need to run this replicate commands function before any data changing command

redis.replicate_commands()

it's explained here

Upvotes: 1

Markus
Markus

Reputation: 3148

On a single Redis instance I cannot think about any negativ effect.

But say you're running some master-slave setup. the result of those lua scripts which calls e.g. TIME wouldn't be equal on the master.

Upvotes: 0

Chris Tanner
Chris Tanner

Reputation: 1650

It's explained fairly well in the Redis docs here.

The scripts are replicated to slaves by sending the script over and running it on the slave, so the script needs to always produce the same results every time it's run or the data on the slave will diverge from the data on the master.

You could try the new 'scripts effects replication' in the same link if you need perform non deterministic operations in a script.

Upvotes: 4

Related Questions