StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

Caching LUA scripts on servers of a Redis cluster

Open nhathongly opened this issue 11 years ago • 11 comments

Is there any way to let StackExchange.Redis to cache a LUA script on all server of a cluster. Then when evaluating the LoadedLuaScript on the server associates with the passed keys. All keys, of course, will be in the same node

nhathongly avatar Nov 06 '15 11:11 nhathongly

I thought I'd replied to this - it might be stuck in a draft email!

There is no explicit feature that provides this currently. Internally, when using ScriptEvaluate, SE.Redis tracks which scripts are known to be present on each node, and executes SCRIPT LOAD as appropriate.

Can I ask what the scenario is that you're trying to cover? Is this for an administrative SCRIPT LOAD? or...?

mgravell avatar Nov 18 '15 11:11 mgravell

I want to implement a thing can be called LoggedInUserManager, which allows many usernames to login to my site using the same browser with tabs. I am using Redis as my session state storage. A new LoggedInUserManagerId will be generated and a new sorted set associated with that new key will be added to Redis once the user open his/her browser and log in to my site. I'm also using some hashes for session data and metadata. I embraced all keys related to a LoggedInUserManager with "{" and "}", ex: {app-name:account-manager}:some-other-text, to make sure that those keys will be in the same Redis node. So I think it is better if the LUA scripts are cached on all servers. Since it not only reduces the bandwidth usage but also lets Redis not compile scripts every time the scripts are evaluated, right?

Sorry if my English is too bad :P

nhathongly avatar Nov 20 '15 01:11 nhathongly

The library will take care of that automatically per node without you having to do anything. So if you issue the same script 57 times involving keys that would touch 4 nodes in the cluster, it will perform almost all of the operations via evalsha. IIRC it only uses eval when it hasn't had confirmation of a script load. There are some edge cases where simply issuing "SCRIPT LOAD ...", " EVALSHA ..." is not sufficient (there are things that can cause command pipes to be discarded - WATCH, etc) On 20 Nov 2015 1:41 a.m., "nhathongly" [email protected] wrote:

I want to implement a thing can be called LoggedInUserManager, which allows many usernames to login to my site using the same browser with tabs. I am using Redis as my session state storage. A new LoggedInUserManagerId will be generated and a new sorted set associated with that new key will be added to Redis once the user open his/her browser and log in to my site. I'm also using some hashes for session data and metadata. I embraced all keys related to a LoggedInUserManager with "{" and "}", ex: {app-name:account-manager}:some-other-text, to make sure that those keys will be in the same Redis node. So I think it is better if the LUA scripts are cached on all servers. Since it not only reduces the bandwidth usage but also lets Redis not compile scripts every time the scripts are evaluated, right?

Sorry if my English is too bad :P

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/301#issuecomment-158254623 .

mgravell avatar Nov 21 '15 18:11 mgravell

Do then I understand correctly when I say using LoadedLuaScript does not make sence in a clustered environment?

In what ways does SE.Redis cache scripts on server to avoid sending them each time? (Do I need to use LuaScript or is a standard cons string script sufficient?) How does SE.Redis handle this in master / slave mode?

projecteon avatar Dec 09 '15 13:12 projecteon

I need to do an audit on those, because essentially SE.Redis already does some work internally to handle whether things are loaded. I didn't add those APIs, so I can't tell you without looking.

On 9 December 2015 at 13:57, Aleksander Spro [email protected] wrote:

Do then I understand correctly when I say using LoadedLuaScript does not make sence in a clustered environment?

In what ways does SE.Redis cache scripts on server to avoid sending them each time? (Do I need to use LuaScript or is a standard cons string script sufficient?) How does SE.Redis handle this in master / slave mode?

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/301#issuecomment-163244510 .

Regards,

Marc

mgravell avatar Dec 09 '15 15:12 mgravell

In terms of ScriptExecute: it tracks what is known to be loaded on a per-server basis, and issues appropriate commands as needed.

On 9 December 2015 at 15:07, Marc Gravell [email protected] wrote:

I need to do an audit on those, because essentially SE.Redis already does some work internally to handle whether things are loaded. I didn't add those APIs, so I can't tell you without looking.

On 9 December 2015 at 13:57, Aleksander Spro [email protected] wrote:

Do then I understand correctly when I say using LoadedLuaScript does not make sence in a clustered environment?

In what ways does SE.Redis cache scripts on server to avoid sending them each time? (Do I need to use LuaScript or is a standard cons string script sufficient?) How does SE.Redis handle this in master / slave mode?

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/301#issuecomment-163244510 .

Regards,

Marc

Regards,

Marc

mgravell avatar Dec 09 '15 15:12 mgravell

But does that mean running ScriptEvaluate without preloading, preloads the script for next execute? From what I can see in ScriptEvalMessage, it only checks if the script is a valid sha string or not and then runs the correct eval method.

Still curious how you sett up preloaded scripts in a cluster environment as well as master slave.

projecteon avatar Dec 10 '15 08:12 projecteon

Yes, running ScriptEvaluate preloads the scripts and substitutes EVALSHA the next time. Look in ScriptEvalMessage.GetMessages, which makes one message into a multi-message piece, including a SCRIPT LOAD - although there is actually some doubt as to whether you ever need to call SCRIPT LOAD here, vs it being cached automatically when using EVAL. This is all done per endpoint, so it works correctly on cluster / slave / etc.

On 10 December 2015 at 08:12, Aleksander Spro [email protected] wrote:

But does that mean running ScriptEvaluate without preloading, preloads the script for next execute? From what I can see in ScriptEvalMessage, it only checks if the script is a valid sha string or not and then runs the correct eval method.

Still curious how you sett up preloaded scripts in a cluster environment as well as master slave.

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/301#issuecomment-163529727 .

Regards,

Marc

mgravell avatar Dec 10 '15 08:12 mgravell

Ok excellent than you :) Seems the preload classes are redundant then..

projecteon avatar Dec 10 '15 08:12 projecteon

I need to re-audit those; the bit that deals with parameterization is all good, though

On 10 December 2015 at 08:42, Aleksander Spro [email protected] wrote:

Ok excellent than you :) Seems the preload classes are redundant then..

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/301#issuecomment-163534624 .

Regards,

Marc

mgravell avatar Dec 10 '15 08:12 mgravell

Closing out to cleanup.

NickCraver avatar May 28 '18 02:05 NickCraver