Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Redis WRONGTYPE error: the key holds a different kind of value

ErrorWRONGTYPE Operation against a key holding the wrong kind of value

Short answer

Redis stores one type per key (string, list, set, hash and others), and each command works on only some types. WRONGTYPE means your command and the key's type do not match. Run TYPE on the key, then use the matching command, or stop two parts of the app from sharing one key name.

  • Applies to Redis 7, Redis 8, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask Redis what type the key really has. Run it against the same database your PHP code uses.

    redis-cli TYPE your:key
  2. 2Match what you see, then jump to its fix

    1. 1TYPE prints hash, list, set or zset, and your code calls GET.The command does not fit the typeFix
    2. 2The code worked for a while, then began failing for keys written by a different feature or an older release.Another writer reused the key name for a different typeFix
    3. 3The failing call is SET with the GET option.SET with GET hit a key that is not a stringFix

What does the error look like?

  • WRONGTYPE Operation against a key holding the wrong kind of value

Cause 1 of 3

The command does not fit the type

1Check

The Redis manual says GET returns an error if the value is not a string, because GET handles only string values. The same logic holds for every command and its type.

redis-cli TYPE session:42
redis-cli HGETALL session:42

2Fix

Use the command family for the stored type, or store the value as the type your code expects. The command pages name the type each command works on.

# type is hash: read it as a hash
redis-cli HGETALL session:42
# type is string: GET is correct
redis-cli GET cache:home

3Verify

The command returns data and no WRONGTYPE.

redis-cli TYPE session:42

Confirmed by redis.io

Did this fix it?

Cause 2 of 3

Another writer reused the key name for a different type

1Check

SET replaces whatever is at the key, regardless of its type. A cache write, a session handler and a queue that share a name leave the key as whichever wrote last. TYPE shows the survivor.

redis-cli TYPE your:key

2Fix

Give each feature its own key prefix. Remove the stale key once, so the owner can recreate it with the right type.

redis-cli DEL your:key

3Verify

The owner writes the key again, and TYPE shows the type it expects.

redis-cli TYPE your:key

Confirmed by redis.io

Did this fix it?

Cause 3 of 3

SET with GET hit a key that is not a string

1Check

The SET page says that with GET, an error is returned and SET is aborted if the stored value is not a string.

redis-cli TYPE your:key

2Fix

Delete the key first with DEL when you really want to replace a non-string value, or choose a new key name.

redis-cli DEL your:key
redis-cli SET your:key value GET

3Verify

SET returns nil for a new key (or the old string) without an error.

redis-cli GET your:key

Confirmed by redis.io

Did this fix it?

How do I stop it coming back?

  • Prefix keys per feature (session:, cache:, queue:) and write down the type of each prefix.
  • Run TYPE on a key before blaming the client library.
  • Give test and development environments their own Redis database, so test data cannot collide with real keys.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

PHP cannot load the phpredis extension (redis.so)

PHP Warning: PHP Startup: Unable to load dynamic library 'redis.so'

PHP loaded a php.ini that names redis.so, but no file with that name exists for this PHP build, or the CLI and FPM read different ini files.

3 causes, each with a fix

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.