Incident Response

Redis WRONGTYPE Operation Against a Key: Causes and Fixes

A command for one data type hit a key that holds another. Here is how to see what is really stored, why it happens during deploys and key-name collisions, why MULTI does not save you, and how to clean it up safely on a big keyspace.

· 11 min read · AG Data Team

WRONGTYPE Operation against a key holding the wrong kind of value is how Redis says that a command for one data type was sent to a key holding another type. A GET on a hash, an LPUSH on a string or an HGET on a set all end this way. Valkey returns exactly the same text (valkey server.c). Redis itself is fine; the application and the data disagree about what lives under that key name.

What exactly happens

Every key holds one value of one type. Before a type-specific command runs, Redis compares the stored type with the one the command expects. If they differ, it replies with the shared WRONGTYPE error and changes nothing. A missing key is treated as empty, so it never causes this error (object.c, checkType).

127.0.0.1:6379> HSET user:42 name "Anna"
(integer) 1
127.0.0.1:6379> GET user:42
(error) WRONGTYPE Operation against a key holding the wrong kind of value

Generic key commands work on every type: TYPE, DEL, UNLINK, EXPIRE, TTL, RENAME. Plain SET is special too: it overwrites the key whatever its type, and drops any previous TTL (SET). Keep that in mind, because it is often how a key silently changes type in the first place.

Step 1: look at what is stored

127.0.0.1:6379> TYPE user:42
hash
127.0.0.1:6379> OBJECT ENCODING user:42
"listpack"
127.0.0.1:6379> TTL user:42
(integer) -1
127.0.0.1:6379> TYPE no:such:key
none

TYPE returns string, list, set, zset, hash, stream, and on recent Redis also vectorset, or none if the key does not exist (TYPE). Then match it to the command family that failed:

string   GET, SET, INCR, APPEND, GETRANGE, PFADD/PFCOUNT, SETBIT/GETBIT
list     LPUSH, RPUSH, LPOP, LRANGE, LLEN
set      SADD, SMEMBERS, SISMEMBER, SCARD
zset     ZADD, ZRANGE, ZSCORE, ZCARD, GEOADD, GEOSEARCH
hash     HSET, HGET, HGETALL, HDEL, HLEN
stream   XADD, XRANGE, XREADGROUP, XLEN

Some "types" are not types. Geospatial indexes are stored as sorted sets, and HyperLogLogs and bitmaps are strings (SCAN, TYPE option). So ZRANGE on a geo key works, and GET on a HyperLogLog returns its binary content. The reverse can fail: PFCOUNT on an ordinary string replies WRONGTYPE Key is not a valid HyperLogLog string value. (hyperloglog.c).

The usual causes

  1. Two features share a key name. One service caches user:42 as a JSON string, another keeps user:42 as a hash of counters. Whoever wrote last wins, and the other side gets WRONGTYPE. Fix the naming: every key family should have one owner and one type, for example user:42:profile (string) and user:42:stats (hash).
  2. A data model change during a rolling deploy. The new version stores a hash where the old one stored a string, under the same name. During the rollout both versions run, and the key flips between types. In our experience this is the most common source of sudden WRONGTYPE spikes. Use a new key name or prefix for the new format, such as user:v2:42, and let the old keys expire.
  3. A client library helper picked a different type. Framework caches, rate limiters and session stores each pick their own structure. Point two of them at the same prefix and they collide. Give each library its own prefix or database.
  4. A manual fix in production. Someone ran SET on a key to "reset" it, which replaced the hash with a string. Every reader now fails until the key is deleted or rebuilt.
  5. Wrong command in code. Plain bugs, for example SMEMBERS against a sorted set. The stack trace and the command name usually make this obvious.

Transactions and pipelines do not protect you

A WRONGTYPE error inside MULTI/EXEC is a runtime error. Redis does not roll back: the other commands in the transaction still run, and EXEC returns an array in which only that element is an error (Transactions). Pipelines behave the same way, with one reply per command. If your client only checks whether the call as a whole succeeded, you can end up with half-applied updates.

MULTI
SET a abc
LPOP a
EXEC
1) OK
2) (error) WRONGTYPE Operation against a key holding the wrong kind of value

Fixing it

# the value is garbage or a stale cache entry: remove it
UNLINK user:42

# the new code writes a different type: give it a new key name
HSET user:v2:42 name "Anna" plan "pro"

If the key is a cache entry, delete it and let the application rebuild it with the correct type. Prefer UNLINK to DEL for anything that might be large: it removes the key immediately and reclaims memory in another thread (UNLINK). Our article on Redis big keys explains why deleting a huge hash or set with DEL can stall the server. If the key holds data you cannot lose, read it with the matching command first (HGETALL, SMEMBERS, XRANGE), convert it and write the result under a new name.

What changes on large keyspaces

# count keys of each type under one prefix, run against a replica
redis-cli -h replica --scan --pattern 'user:*' | head -100000 \
  | sed 's/^/TYPE /' | redis-cli -h replica | sort | uniq -c

# or let the server filter by type (Redis 6.0+)
SCAN 0 MATCH user:* TYPE string COUNT 1000

Key takeaways

Redis Valkey Errors Data modeling
Work with us

Redis errors after a release?

We review Redis and Valkey key design, audit production keyspaces and fix data-model problems without flushing the cache or taking the service down.

Get in touch More from the blog