Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Redis Difficulty: Advanced ClaudeChatGPTCursor

Redis Functions Library Design Prompt

Design a Redis 7+ Functions library (FUNCTION LOAD) to replace ad-hoc EVAL scripts — with named entry points, KEYS/ARGV discipline, atomicity, and cluster-safe key access.

Target user
Engineers building server-side Redis logic
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior engineer who designs Redis 7+ Functions libraries (the successor to raw `EVAL` scripting).

I will provide:
- The atomic operations you want server-side (compare-and-set, rate limit, dequeue-and-ack, conditional update)
- Whether the deployment is standalone or Redis Cluster
- Current `EVAL`/`EVALSHA` usage, if any

Your job:

1. **Explain Functions vs EVAL**: `FUNCTION LOAD` registers a named library whose functions persist with the dataset and replicate/persist automatically — unlike ad-hoc `EVAL` scripts you must re-load and track by SHA. Functions are the recommended path for reusable server-side logic; call with `FCALL name numkeys key... arg...`.
2. **Enforce KEYS/ARGV discipline**: every key the function touches must be passed in `KEYS` (declared via `numkeys`), never constructed from `ARGV` inside the script. This is what lets Redis (and Cluster) route and verify the command — in Cluster all keys must hash to the same slot, so document required hash tags.
3. **Guarantee atomicity + no side effects**: the whole function runs atomically on the single thread; forbid long loops (they block every other client), non-deterministic calls that break replication (use `redis.setresp`/allowed APIs), and unbounded `redis.call` over big collections. Keep functions short.
4. **Design the entry points**: name each function, list its KEYS/ARGV contract, return shape, and error handling (`redis.error_reply`). Group related functions in one library with a shared name.
5. **Handle flags**: mark read-only functions with `no-writes` so they can run on replicas; keep writers off replicas.
6. **Version + deploy**: `FUNCTION LOAD REPLACE` for upgrades, `FUNCTION DUMP`/`RESTORE` to move libraries, `FUNCTION LIST` to audit. Keep source in version control, not just loaded on the server.
7. **Test the blocking risk**: estimate worst-case elements touched; anything O(N) over a big key can stall the instance.

Mark DESTRUCTIVE or risky: constructing keys from ARGV (breaks Cluster routing / bypasses ACL key patterns), long-running loops (block all clients), non-deterministic writes (replication divergence), and `FUNCTION FLUSH` (drops every library).

---

Atomic operations wanted: [DESCRIBE]
Standalone or Cluster: [DESCRIBE]
Existing EVAL usage: [PASTE]

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

Redis Functions replaced the fragile “load an EVAL script and track it by SHA” pattern, but the failure modes are the same and unforgiving: keys built inside the script break Cluster routing and ACLs, and any loop blocks the whole single-threaded server. This prompt designs a named, versioned library with a strict KEYS/ARGV contract, deterministic writes, and no-writes flags for replica-safe reads.

How to use it

  1. List the atomic operations you actually need — Functions exist to make multi-step logic atomic.
  2. Declare every touched key in KEYS — never build keys from ARGV.
  3. State standalone vs Cluster — Cluster forces same-slot keys and hash tags.

Useful commands

# Load / upgrade a library from a file
cat mylib.lua | redis-cli -x FUNCTION LOAD REPLACE

# Call a function: FCALL name numkeys KEYS... ARGV...
redis-cli FCALL cas 1 config:flag expected new

# Read-only function is safe on replicas when flagged no-writes
redis-cli FCALL_RO getflag 1 config:flag

# Audit, back up, and move libraries
redis-cli FUNCTION LIST
redis-cli FUNCTION DUMP > funcs.bin

Example library skeleton

#!lua name=mylib
-- compare-and-set: KEYS[1]=key, ARGV[1]=expected, ARGV[2]=new
redis.register_function('cas', function(keys, args)
  if redis.call('GET', keys[1]) == args[1] then
    redis.call('SET', keys[1], args[2])
    return 1
  end
  return 0
end)

-- read-only, replica-safe
redis.register_function{
  function_name='getflag',
  callback=function(keys, args) return redis.call('GET', keys[1]) end,
  flags={'no-writes'}
}

Common findings this catches

  • Keys built from ARGV → CROSSSLOT / ACL bypass in Cluster.
  • Loops over big keys → the whole instance stalls.
  • Non-deterministic writes → replica divergence.
  • Writers run on replicas → missing no-writes discipline.
  • Libraries only on the server → lost on FUNCTION FLUSH; keep source in VCS.

When to escalate

  • The logic needs cross-slot keys — redesign the key layout or move logic to the app.
  • A function is unavoidably O(N) over a large collection — process incrementally instead.

Related prompts

More Redis prompts & error guides

Browse every Redis prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.