Back to results for “Lua Scripting”
Redis › Lua Scripting

What are the performance and blocking implications of running a Lua script in Redis, especially for a script that takes a long time to execute?

Advanced

Answer

Since Redis processes commands using a single main thread, a Lua script runs to completion before Redis can process any other client's commands, meaning a slow or inefficient script can block the entire server and delay every other operation, making it important to keep scripts fast, avoid unbounded loops over large datasets, and thoroughly test script performance before deploying it to production.
-- A script that loops over a very large dataset
-- can block the entire Redis server while it runs
for i = 1, 1000000 do
  redis.call('set', 'key' .. i, 'value')
end
Real-world example A team accidentally deployed a Lua script that looped over a very large dataset, briefly freezing their entire Redis instance and affecting every other application sharing that server, prompting them to add stricter script performance review to their deployment process.

Common follow-ups: How do you measure how long a specific Lua script takes to execute before deploying it?;What configuration options exist to limit how long a script is allowed to run?