Redis Lua script not returning expected results when using KEYS in a high-load environment
I'm trying to implement I've looked through the documentation and I'm still confused about I'm deploying to production and I'm relatively new to this, so bear with me. I'm relatively new to this, so bear with me. I'm experiencing unexpected behavior with a Lua script running in Redis 6.0.9. My script is supposed to iterate through keys that match a certain pattern and increment their values, but under high load, it sometimes returns `nil` instead of the expected count of modified keys. Here's the Lua script I'm using: ```lua local keys = redis.call('KEYS', ARGV[1]) local count = 0 for i=1, #keys do count = count + redis.call('INCR', keys[i]) end return count ``` I call this script from my Node.js application using the `ioredis` library. The call looks like this: ```javascript const Redis = require('ioredis'); const redis = new Redis(); async function updateKeys(pattern) { const result = await redis.eval(script, 0, pattern); console.log('Updated keys count:', result); } ``` When I run the script with a pattern like `"user:*"` during peak times, the count occasionally comes back as `nil`, and I need to figure out why. Sometimes it works as expected, but in a production environment, it feels unreliable. I've also noticed that if I run the script with a `SCAN` command instead of `KEYS`, it performs better, but Iām concerned about the overhead and complexity of using `SCAN` for this operation. Has anyone else faced similar issues with the `KEYS` command inside Lua scripts? Is there a limitation I'm overlooking, or would switching to `SCAN` solve this inconsistency? Any best practices or insights would be greatly appreciated! How would you solve this? I'm working with Javascript in a Docker container on macOS. Any suggestions would be helpful.