database type_error ai_generated true

redis.exceptions.ResponseError: Error running script (call to f_abcdef123456): @user_script:1: WRONGTYPE Operation against a key holding the wrong kind of value

ID: database/redis-lua-script-execution-error

Also available as: JSON · Markdown · 中文
88%Fix Rate
90%Confidence
1Evidence
2023-09-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Redis 7.2.4 active
Redis 6.2.14 active
Redis Stack 7.2.0 active

Root Cause

A Redis Lua script attempted to perform an operation (e.g., LPUSH on a string key) that is incompatible with the data type stored at the key, causing a WRONGTYPE error.

generic

中文

Redis Lua 脚本尝试对键执行与存储的数据类型不兼容的操作(例如对字符串键执行 LPUSH),导致 WRONGTYPE 错误。

Official Documentation

https://redis.io/docs/latest/develop/interact/programmability/error-handling/

Workarounds

  1. 90% success Identify the offending key by running: TYPE <key> in redis-cli. Then either delete the key (DEL <key>) or modify the script to handle the existing type (e.g., use EXISTS to check type before operations).
    Identify the offending key by running: TYPE <key> in redis-cli. Then either delete the key (DEL <key>) or modify the script to handle the existing type (e.g., use EXISTS to check type before operations).
  2. 85% success Update the Lua script to use TYPE command to check the key's type before performing operations, and return an error or handle gracefully: local keyType = redis.call('TYPE', KEYS[1]); if keyType ~= 'list' then return redis.error_reply('Expected list, got ' .. keyType) end
    Update the Lua script to use TYPE command to check the key's type before performing operations, and return an error or handle gracefully: local keyType = redis.call('TYPE', KEYS[1]); if keyType ~= 'list' then return redis.error_reply('Expected list, got ' .. keyType) end
  3. 80% success If the key should be a list but is currently a string, convert it by deleting and recreating: DEL mykey; LPUSH mykey value1 value2; then retry the script.
    If the key should be a list but is currently a string, convert it by deleting and recreating: DEL mykey; LPUSH mykey value1 value2; then retry the script.

中文步骤

  1. Identify the offending key by running: TYPE <key> in redis-cli. Then either delete the key (DEL <key>) or modify the script to handle the existing type (e.g., use EXISTS to check type before operations).
  2. Update the Lua script to use TYPE command to check the key's type before performing operations, and return an error or handle gracefully: local keyType = redis.call('TYPE', KEYS[1]); if keyType ~= 'list' then return redis.error_reply('Expected list, got ' .. keyType) end
  3. If the key should be a list but is currently a string, convert it by deleting and recreating: DEL mykey; LPUSH mykey value1 value2; then retry the script.

Dead Ends

Common approaches that don't work:

  1. Flushing all keys with FLUSHALL to clear the data type mismatch 95% fail

    This deletes all data, which is an extreme measure; the script will still fail if the same key is recreated with the wrong type.

  2. Ignoring the error and retrying the script 100% fail

    The error is deterministic; retrying without fixing the key type will produce the same error every time.

  3. Using EVAL instead of EVALSHA to force script recompilation 90% fail

    The error is not due to script caching; it's a runtime type mismatch. EVAL will still fail with the same error.