Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is spop not an allowed script command? #2139

Closed
ssoroka-se opened this issue Nov 12, 2014 · 7 comments
Closed

Why is spop not an allowed script command? #2139

ssoroka-se opened this issue Nov 12, 2014 · 7 comments

Comments

@ssoroka-se
Copy link

I'm trying to write a lua script for redis that includes this line:

local id = redis.call('spop', 'processed_ids')

but I'm getting an error This Redis command is not allowed from scripts. It looks like this error is coming from this line in the source.
As per https://github.com/antirez/redis/blob/unstable/src/redis.c#L163 , spop has the 's' flag set, which is REDIS_CMD_NOSCRIPT. Why was this set?

@michael-grunder
Copy link
Contributor

Hey,

That's because the SPOP command isn't deterministic (it will pop a random member), which would break replication from inside a script. If you look at the source code for SPOP you'll see that it converts into an SREM command for the purposes of replication.

https://github.com/antirez/redis/blob/unstable/src/t_set.c#L399-L402

Cheers! 😺
Mike

@ssoroka-se
Copy link
Author

So a work-around would be to use srandmember and srem, or sscan and srem.

On Wednesday, November 12, 2014, Michael Grunder notifications@github.com
wrote:

Hey,

That's because the SPOP command isn't deterministic (it will pop a random
member), which would break replication from inside a script. If you look at
the source code for SPOP you'll see that it converts into an SREM command
for the purposes of replication.

https://github.com/antirez/redis/blob/unstable/src/t_set.c#L399-L402

Cheers! [image: 😺]
Mike


Reply to this email directly or view it on GitHub
#2139 (comment).

@michael-grunder
Copy link
Contributor

Yeah either of those strategies should work for you. Which one you pick would just have to do with your use case specifically.

@itapita
Copy link

itapita commented Nov 12, 2014

Actually no, you can't do a write after a nondeterministic command for the
same reason (breaking replication) and Redis will block you from doing that
with an error.
On Nov 13, 2014 12:53 AM, "Michael Grunder" notifications@github.com
wrote:

Yeah either of those strategies should work for you. Which one you pick
would just have to do with your use case specifically.


Reply to this email directly or view it on GitHub
#2139 (comment).

@michael-grunder
Copy link
Contributor

Yeah, @itapita is correct. I spaced out on that one for some reason. I need more (or maybe less) coffee.

@ssoroka-se
Copy link
Author

That's kind of a major draw-back. I guess I'm left with sorted sets.

@ssoroka-se
Copy link
Author

For anyone else looking at this in the future, I worked around the problem by doing srandmember outside of the script, then srem inside of the script, passing the member in, and that met my needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants