Back to results for “Commands”
Redis › Lua Scripting

What is the difference between EVAL and EVALSHA, and why would you use EVALSHA in a production application?

Intermediate

Answer

EVAL sends the full text of the Lua script to Redis every single time it runs, while EVALSHA sends only a hash identifier of a script that has already been loaded and cached on the server beforehand, which reduces network bandwidth usage since you avoid repeatedly transmitting the same script text for every single execution.
SCRIPT LOAD "return redis.call('get', KEYS[1])"
-- returns a SHA hash
EVALSHA 'e0e1f9fabfc9d4800c877a703b823ac0578ff831' 1 mykey
Real-world example A high traffic application loads its frequently used Lua scripts once at startup, then calls them repeatedly using EVALSHA, saving meaningful network bandwidth compared to sending the full script text with every single call.

Common follow-ups: What happens if you call EVALSHA with a hash for a script that has not actually been loaded?;How long does Redis keep a loaded script cached?