Working with Redis Functions
1. Creating Functions
| Command | Description |
|---|---|
FUNCTION LOAD [REPLACE] code | Persistent library, replicated via RDB/AOF 7.0+ |
#!lua name=mylib
redis.register_function('myadd', function(keys, args)
return redis.call('INCRBY', keys[1], args[1])
end)
2. Calling Functions
| Command | Description |
|---|---|
FCALL fname numkeys key ... arg ... | Execute (read+write) |
FCALL_RO fname numkeys ... | Replica-safe read-only call |
3. Listing Functions
| Command | Returns |
|---|---|
FUNCTION LIST [LIBRARYNAME pat] [WITHCODE] | All libraries |
4. Deleting Functions
| Command | Description |
|---|---|
FUNCTION DELETE libname | Remove library |
5. Dumping Functions
| Command | Returns |
|---|---|
FUNCTION DUMP | Binary payload of all libraries |
6. Restoring Functions
| Command | Options |
|---|---|
FUNCTION RESTORE payload [FLUSH|APPEND|REPLACE] | Reload from DUMP |
7. Flushing Functions
| Command | Description |
|---|---|
FUNCTION FLUSH [ASYNC|SYNC] | Wipe all libraries |
8. Getting Function Stats
| Command | Returns |
|---|---|
FUNCTION STATS | Currently running function + engine stats |
9. Killing Running Functions
| Command | Limitations |
|---|---|
FUNCTION KILL | Only if no writes performed |
10. Understanding Functions vs Lua Scripts
| Aspect | Functions | EVAL Scripts |
|---|---|---|
| Persistence | RDB/AOF replicated | Not persisted; re-load after restart |
| Naming | Named libraries | SHA1 hash |
| Use case | App-level extensions | Ad-hoc atomic ops |