Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

FUNCTION LOAD

Last updated
FUNCTION LOAD [REPLACE] function-code
Group
Scripting and functions
Complexity
O(N) where N is the length of the code
Arity
-3 (at least 3 words, counting the command name)
HotKV Cloud
All plans
On-Premise
no license needed

FUNCTION LOAD compiles a library and registers its functions. The code must start with a metadata line naming the engine and the library, such as #!lua name=mylib; library names may contain letters, digits and underscores. The code then calls redis.register_function once per function, either as redis.register_function('name', callback) or with a table giving function_name, callback, and optionally flags and description.

The library is saved with the dataset and replicated. Loading a library whose name is taken fails unless you pass REPLACE, which swaps the old library for the new one in one step. No function name may be registered by two libraries.

Option Effect
REPLACE Replace a library of the same name instead of failing.
Case RESP2 RESP3
Loaded Bulk string: the library name Bulk string

Errors:

  • ERR Library '<name>' already exists without REPLACE.
  • ERR Function <name> already exists when another library registers that function.
  • ERR No functions registered when the code registers no function.
  • ERR Missing library metadata, ERR Library name was not given, ERR Engine '<engine>' not found and similar errors for a bad first line.
  • ERR Error compiling function: ... when the code does not compile.
  • ERR Unknown option given: <option> for an option other than REPLACE.
127.0.0.1:6379> FUNCTION LOAD "#!lua name=util\nredis.register_function('echo', function(keys, args) return args[1] end)"
"util"
127.0.0.1:6379> FUNCTION LOAD "#!lua name=util\nredis.register_function('echo', function(keys, args) return args[1] end)"
(error) ERR Library 'util' already exists
127.0.0.1:6379> FUNCTION LOAD REPLACE "#!lua name=util\nredis.register_function('echo', function(keys, args) return 'v2: ' .. args[1] end)"
"util"
127.0.0.1:6379> FCALL echo 0 "hi"
"v2: hi"

Same as Redis 7 and Valkey 8.

FCALL, FUNCTION LIST, FUNCTION DELETE