1

Redis forbids commands like 'EVAL' and 'EVALSHA' in user scripts. What can we benefit from scuh prohibition?

One way to bypass this limitation is to put all Lua scripts into one. However, it violate the scripts' maintainability. Is their any better solution?

Minsheng Liu
  • 760
  • 7
  • 23
  • You may want to consider [lua-call](https://github.com/josiahcarlson/lua-call): *A wrapper and library to allow your Redis Lua scripts to call each other inside Redis.* – deltheil Mar 31 '14 at 08:49

1 Answers1

0

Redis forbids that because of replicatability in write operations.

The SHA1 checksum of the script should describe everything the Lua script does. When the script as well as the data is replicated, and executed on a Redis slave, the result should be exactly the same.

Therefore, functionality with a random nature, like rand or time are excluded from write operations. Why eval and evalsha are forbidden from read scripts as well, is probably because it makes the 'is-write-script' analysis impossible or at least cumbersome.

A possible solution on how to work around this, see this post.

We use this SHA1 response value loopback mechanism when connecting to different redis servers, see this diagram.

Hope this helps, TW

Community
  • 1
  • 1
Tw Bert
  • 3,659
  • 20
  • 28
  • Thanks! You said "is probably because it makes the 'is-write-script' analysis impossible or at least cumbersome." Does it mean that it is possible for Redis to support calling EVAL & EVALSHA in Lua scripts in theory but the author chose not to do it? – Minsheng Liu Mar 23 '14 at 02:25
  • Will it have something to do with replication? Will the slave replicate the script too? – Minsheng Liu Mar 23 '14 at 02:33
  • Besides, I am writing a simple Lua macro system which could copy other script into one file. Would it be a good practical? – Minsheng Liu Mar 23 '14 at 02:36
  • The author (antirez) definately chose this. And I can understand this decision from his point of view, it's a good choice. Redis should stay simple and solid. Your macro system: yes, that's a good practice, possibly. Can you give some examples? – Tw Bert Mar 23 '14 at 03:01
  • Hi Minsheng, just wanted to give you a heads up, some new insights. See [here](http://stackoverflow.com/questions/21718277/is-it-possible-to-call-lua-functions-defined-in-other-lua-scripts-in-redis/22599862#comment34409490_22599862), good post by Josiah, if you can live with some hackiness. – Tw Bert Mar 27 '14 at 22:06
  • Hi Tw Bert, thanks much for your supplement! Luckily my ugly macro code is going to wrap other Lua scripts as functions and they have their local KEYS and ARGV. A simple demo of my macro system is [here](https://gist.github.com/notcome/e233e0776cf5b9ac6bd0) – Minsheng Liu Mar 28 '14 at 01:43
  • Hi Minsheng Liu, looks good. Intrigued by the last line "What's your point?" :) – Tw Bert Mar 28 '14 at 02:04