Redis WRONGTYPE error: the key holds a different kind of value
WRONGTYPE Operation against a key holding the wrong kind of valueShort 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.
Fix it fast
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:key2Match what you see, then jump to its fix
- 1
TYPE prints hash, list, set or zset, and your code calls GET.The command does not fit the typeFix - 2
The 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
The failing call is SET with the GET option.SET with GET hit a key that is not a stringFix
- 1
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:422Fix
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:home3Verify
The command returns data and no WRONGTYPE.
redis-cli TYPE session:42Confirmed by redis.io
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:key2Fix
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:key3Verify
The owner writes the key again, and TYPE shows the type it expects.
redis-cli TYPE your:keyConfirmed by redis.io
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:key2Fix
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 GET3Verify
SET returns nil for a new key (or the old string) without an error.
redis-cli GET your:keyConfirmed by redis.io
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
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 fixBack end
Laravel says Class "Redis" not found when using phpredis
Class 'Redis' not found
Laravel uses the phpredis PHP extension by default, so a missing or disabled extension gives Class "Redis" not found.
3 causes, each with a fixBack end
A Laravel queue job times out, the worker exits or the job runs twice
Illuminate\Queue\MaxAttemptsExceededException
A Laravel queue worker stops any job that runs longer than its timeout, 60 seconds by default, and exits with an error.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fix