Is Lua script a recommended way to query Redis and howto define EVAL KEYS

597 views
Skip to first unread message

Dong-xu Gu

unread,
May 29, 2014, 9:56:49 AM5/29/14
to redi...@googlegroups.com
Hi

I have few questions about Redis with Lua script.

1. It seems using Lua script has some advantage over multiple commands and pipeline. So is it a recommended way to query Redis?

2. Will Lua script still being supported/recommended in Redis 3.x, espcially in Cluster Mode?

3. I understand EVAL command request user pass KEYS along with the Lua script, mainly effort to support Redis cluster.
   Take this examle: Let's say I have a twitter clone. A user has a follower key which is a Set contains followers's id.
   When user post a new tweet, my script will loop the follower key and calculate each follower's timeline key, then add the tweet to them.
   So the command should be: EVAL post_tweet_script 1 user_a_follower_key
   I didn't pass any follower's timeline key to the command, will it be a problem in Redis Cluster or something else?

Marc Gravell

unread,
May 29, 2014, 1:00:51 PM5/29/14
to redi...@googlegroups.com

1: sometimes, yes; it can reduce round-trips and bandwidth, but it isn't applicable for every situation - and you certainly don't want a script looping (etc) for too long as it will block all other connections. It has good use-cases and bad use-cases

2+3 yes, but you need to be careful to declare keys in the KEYS argument. You probably also want to use key has tags - declaring the KEYS doesn't help if they aren't on the same node. Hash tags take the form of "{/user/12345}/followers" and "{/user/12345}/account" - here we can guarantee that these hash to the same slot (which means: same node, assuming it isn't in migration)

Marc

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

Marc Gravell

unread,
May 29, 2014, 1:01:26 PM5/29/14
to redi...@googlegroups.com

/s/has tags/hash tags

Josiah Carlson

unread,
May 29, 2014, 1:02:53 PM5/29/14
to redi...@googlegroups.com
My replies are inline.

On Thu, May 29, 2014 at 6:56 AM, Dong-xu Gu <longke...@gmail.com> wrote:
Hi

I have few questions about Redis with Lua script.

1. It seems using Lua script has some advantage over multiple commands and pipeline. So is it a recommended way to query Redis?

If you find that Lua is better suited for what you need to do, then use it. If you find that regular commands do what you need them to do, then use them. Lua scripting is to Redis what PL/SQL is to many relational databases. It's a different (but fully supported) way of querying and updating your data, depending on your needs.

2. Will Lua script still being supported/recommended in Redis 3.x, espcially in Cluster Mode?

Yes. There are certain limitations about what you can do with Lua scripting in Redis Cluster (all of the keys must be in the same shard, some Redis commands are not supported), but it is usable and useful in many cases.

3. I understand EVAL command request user pass KEYS along with the Lua script, mainly effort to support Redis cluster.
   Take this examle: Let's say I have a twitter clone. A user has a follower key which is a Set contains followers's id.
   When user post a new tweet, my script will loop the follower key and calculate each follower's timeline key, then add the tweet to them.
   So the command should be: EVAL post_tweet_script 1 user_a_follower_key
   I didn't pass any follower's timeline key to the command, will it be a problem in Redis Cluster or something else?

It would be a problem with Redis Cluster, or any other client-sharded Redis setup. If the data doesn't live on the shard where your script is executing, then any attempted writes will write data to the shard that it isn't supposed to have. In a regular sharding setup, that data would happily live there, even if it wasn't supposed to be there. In cluster, there are other issues.

 - Josiah
Reply all
Reply to author
Forward
0 new messages