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
- Two features share a key name. One service caches
user:42as a JSON string, another keepsuser:42as 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 exampleuser:42:profile(string) anduser:42:stats(hash). - 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. - 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.
- A manual fix in production. Someone ran
SETon a key to "reset" it, which replaced the hash with a string. Every reader now fails until the key is deleted or rebuilt. - Wrong command in code. Plain bugs, for example
SMEMBERSagainst 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
- Audit with SCAN, never KEYS.
SCANwalks the keyspace incrementally, and since Redis 6.0 it accepts aTYPEfilter. The filter is applied after keys are fetched, though, so it does not reduce the work of a full pass. Run audits against a replica and keepCOUNTmoderate. The default is 10 (SCAN). - Clean up in batches. When thousands of keys have the wrong type after a bad release, delete them in batches of a few hundred with
UNLINKand watch latency, as described in our latency and SLOWLOG guide. In our experience, one huge loop that deletes everything at once is what turns a data bug into an outage. - Treat key names as schema. Write down every prefix with its type, owner and TTL. Review new prefixes like you would review a new table. Most WRONGTYPE incidents we see in fleets with many services come from two teams choosing the same short prefix.
- Version formats in the key, not in the value. A new structure means a new key name, so old and new code can run side by side during deploys and rollbacks.
- Alert on the error rate. Many clients raise a distinct exception or error prefix for WRONGTYPE. Count it per service. Zero is the normal level, so any steady rate means a real bug, usually introduced by the latest deploy.
# 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
- WRONGTYPE means the command's data type does not match the stored value; nothing is modified.
- Run
TYPE keyfirst; geo is a sorted set, and HyperLogLog and bitmaps are strings. - Plain
SEToverwrites any type, which is often how a key changed type. - Inside
MULTI/EXEC, the other commands still run; there is no rollback. - Use new key names for new data formats, and one owner and type per prefix.
- Audit with
SCAN … TYPEon a replica and clean up with batchedUNLINK.