Skip to content

Redis Functions — server-side libraries that replace EVAL scripting

Use Redis 7 Functions to register named libraries of Lua functions that persist across restarts, replicate cleanly, and replace the EVALSHA reload dance.

Redis 7 introduces Functions — server-side, named, persistent libraries of Lua functions that replace the EVAL/EVALSHA model for serious server-side logic. Unlike scripts (which live in the script cache and disappear on SCRIPT FLUSH or restart), Functions are first-class objects: they persist with RDB and AOF, replicate to replicas, and survive FLUSHALL. This article covers the library format, registration, invocation, and the migration from script-cached EVAL workflows.

How to verify

# List loaded libraries
redis-cli FUNCTION LIST

# Function statistics
redis-cli FUNCTION STATS

# Dump and restore libraries (backup)
redis-cli FUNCTION DUMP > /tmp/redis-functions.bin
redis-cli FUNCTION RESTORE "$(cat /tmp/redis-functions.bin)" REPLACE

# Call a function
redis-cli FCALL myfunc 1 mykey arg1

What’s happening

A Function lives inside a library. A library is a single Lua source that declares one or more named functions; the source is registered as a unit, with a name and an engine (currently LUA). When a library loads, every function in it becomes callable by name via FCALL (read-write context) or FCALL_RO (read-only context, eligible to run on a replica).

The library source must declare itself with the metadata comment:

#!lua name=mylib
redis.register_function('add', function(KEYS, ARGV)
  return redis.call('INCR', KEYS[1])
end)

The #!lua name=mylib shebang is mandatory and identifies the engine and library name. register_function binds a function name; that name is what the client uses in FCALL.

Compared to EVAL/EVALSHA:

  • Persistence. Functions are stored with the database. A restart, a replica failover, or a FLUSHDB retains them. Scripts vanish.
  • Discoverability. FUNCTION LIST shows what is registered. SCRIPT EXISTS sha1 requires you to know the hash.
  • Versioning. Replacing a library is one atomic FUNCTION LOAD REPLACE. Replacing scripts means coordinating SHA changes across clients.
  • Replication. Function code itself replicates to replicas; calling FCALL replicates the call. With scripts, the script is replicated to replicas on first call but the script cache can desynchronize.
  • Read-only execution. A function called with FCALL_RO can run on a replica if it does not modify state — useful for read-heavy operations.

Function ACLs use ~* for keys, +@scripting for the scripting category, and +fcall/+fcall_ro to grant per-command. A locked-down user can be allowed to call but not to load.

The procedure

  1. Write a library file:

    -- ratelimit.lua
    #!lua name=ratelimit
    
    redis.register_function('check', function(keys, args)
      local key = keys[1]
      local limit = tonumber(args[1])
      local window = tonumber(args[2])
      local count = redis.call('INCR', key)
      if count == 1 then
        redis.call('EXPIRE', key, window)
      end
      if count > limit then
        return 0
      end
      return 1
    end)
    
    redis.register_function('reset', function(keys, args)
      return redis.call('DEL', keys[1])
    end)
  2. Load the library:

    redis-cli FUNCTION LOAD "$(cat ratelimit.lua)"
    # Returns the library name: "ratelimit"

    On subsequent updates, use REPLACE:

    redis-cli FUNCTION LOAD REPLACE "$(cat ratelimit.lua)"
  3. Call functions:

    redis-cli FCALL check 1 user:42:rate 100 60
    redis-cli FCALL reset 1 user:42:rate
  4. Inspect the registered library:

    redis-cli FUNCTION LIST WITHCODE
  5. Back up function libraries to handle disaster recovery. RDB and AOF persist them, but an out-of-band backup is cheap insurance:

    redis-cli FUNCTION DUMP > /backup/functions-$(date +%F).bin
    # Restore on a clean server:
    redis-cli FUNCTION RESTORE "$(cat /backup/functions-2026-06-02.bin)" REPLACE
  6. Migrate from EVAL. A scripted rate limiter becomes a function with no logic change; the client switches from:

    r.evalsha(sha, 1, key, limit, window)

    To:

    r.fcall('check', 1, key, limit, window)

    And register_function('check', ...) lives in version control alongside the application code.

  7. For Redis Cluster, functions load to a single node and gossip across the cluster automatically. FCALL routes by KEYS the same way scripts do.

Common pitfalls

  • The library shebang #!lua name=... is mandatory. Forgetting it returns ERR Missing library meta data.
  • A FUNCTION LOAD without REPLACE fails if the library name exists. Replace explicitly during deploys.
  • FCALL_RO rejects any command that writes — including SET ... NX and INCR. A function meant for read-only contexts must use only @read category commands.
  • Permissions for FCALL require the user to have all the underlying command permissions. A user with +@read cannot call a function that does INCR even if the function name is whitelisted.
  • Functions count toward used_memory because they live in the keyspace metadata. Huge libraries (thousands of functions) bloat memory and INFO output. Group by concern.

In the engagements we run, every Redis Function library lives in version control next to the app, ships in CI via FUNCTION LOAD REPLACE, and has a CI integration test that asserts the library loads cleanly on a fresh Redis. We operate that under Managed Operations — and the predecessor pattern, EVAL/EVALSHA scripting, is what Functions replace.