[Fixed]-Effect on lua scripts when redis is unable to save in background due to memory allocation issues

1👍

The error message told you everything: Commands that may modify the data set are disabled.

Redis tries to fork a new process to do the background saving, but fails because of lack of memory. In this case, Redis disables any update to the database. Otherwise, the data in memory, and data on disk will be inconsistent.

how come redis remains functional when memory can’t be allocated for background saving

Redis can still serve read-only requests, and refuse any requests that may modify the database.

but lua scripts crash?

You lua scripts tries to modify the database, e.g. redis.call('ZADD', KEYS[1], ARGV[1], ARGV[2] .. ':' .. ARGV[3]), and fails.

Is there a way to avoid such a scenario?

  1. You should monitor this kind of error, and move Redis to a new machine with more memory to scale up, or use Redis cluster to scale out.

  2. In your lua script, use redis.pcall instead of redis.call. If redis.call fails, the whole script will terminate. However, if redis.pcall fails, it traps the error and return a lua table for the error message. You can check the returned table to see if your call runs successfully.

Leave a comment