350

Hi I am using Laravel with Redis .When I am trying to access a key by get method then get following error "WRONGTYPE Operation against a key holding the wrong kind of value"

I am using following code to access the key value -

i use this code for get data from redis

$values = "l_messages";
$value = $redis->HGETALL($values);
print($value);

8 Answers 8

961

Redis supports 6 data types. You need to know what type of value that a key maps to, as for each data type, the command to retrieve it is different.

Here are the commands to retrieve key value(s):

  • if value is of type string -> GET <key>
  • if value is of type hash -> HGET or HMGET or HGETALL <key>
  • if value is of type lists -> lrange <key> <start> <end>
  • if value is of type sets -> smembers <key>
  • if value is of type sorted sets -> ZRANGEBYSCORE <key> <min> <max>
  • if value is of type stream -> xread count <count> streams <key> <ID>. https://redis.io/commands/xread

Use the TYPE command to check the type of value a key is mapping to:

  • type <key>
5
  • 1
    So when you use the methods that write/read byte[]'s directly, which of those is being used? Commented Aug 27, 2020 at 15:49
  • why I can't only use hget for hash type?
    – alramdein
    Commented Aug 20, 2021 at 11:02
  • 3
    @alramdein you can use HGET and specify the "field in the hash stored at key". 'HGET <key> <field>' or HGETALL to see all the fields Commented Sep 7, 2021 at 10:40
  • 2
    Great answer! I don't think I have seen this explained as clearly and simply as this on the Redis website or in their documentation. Commented Jun 28, 2022 at 5:47
  • 1
    You saved my day too, thanks :). And for who is using bullmq, to look at jobs in a queue you should try keys * and HGETALL key to see what you sent to the queue.
    – Tien Do
    Commented Aug 4, 2022 at 4:50
29

This error says that you are trying to push a wrong value into the key, which means that there already exists same key but with different data structure.

To get all the keys do this in the redis cli

keys *

This should display all the keys Now to get the type of value the key stores, do

type <key>

so it says what value you can push into the key. In my case the type was string (using set) and i was trying to use the key as list

13

This error means that the value indexed by the key "l_messages" is not of type hash, but rather something else. You've probably set it to that other value earlier in your code. Try various other value-getter commands, starting with GET, to see which one works and you'll know what type is actually here.

2
  • 11
    Actually, print($redis->TYPE($values)); will tell it to your without the guesswork ;) Commented Jun 21, 2016 at 23:00
  • 1
    Great :) Didn't know this command, never had to use it. Here's the documentation: redis.io/commands/type
    – advance512
    Commented Jun 21, 2016 at 23:17
5

I faced this issue when trying to set something to redis. The problem was that I previously used "set" method to set data with a certain key, like

$redis->set('persons', $persons)

Later I decided to change to "hSet" method, and I tried it this way

foreach($persons as $person){
    $redis->hSet('persons', $person->id, $person);
}

Then I got the aforementioned error. So, what I had to do is to go to redis-cli and manually delete "persons" entry with

del persons

It simply couldn't write different data structure under existing key, so I had to delete the entry and hSet then.

3

This isn't PHP but I had a similar issue in C# and used this page as a reference to resolve the issue.

My .Net Core 3.1 web server's IDistributedCache.SetStringAsync was actually storing the data as a hash instead of a string; I confirmed this using KeyTypeAsync. Needless to say this was extremely confusing. It seems to be storing the configuration DistributedCacheEntryOptions as part of the hash with {data} being the sub-key of the original string.

To resolve this issue you need to read it as a hash and access the sub-key manually instead of reading it as a string. See following code:

var str = (await redisConnection.GetDatabase().HashGetAllAsync("stringKey"))?.FirstOrDefault(ele => ele.Name == "data")?.Value?.ToString();

EDIT: the order of the sub-hash keys is no longer consistent in recent versions of REDIS, so you can't rely on the last item being the actual data anymore. Make sure you match on ele.Name as above to avoid unexpected errors.

2

I faced this issue and came to find out that there was another already existing key with the same name but with different types. The already existing key was a string while I was expecting a hash.

0

WRONG DATABASE INDEX

Sometimes it's because you are in a wrong db number. I had a same problem and changed the db to the right one, there was no problem and it worked!. I suggest you before HGETALL or whatever you want, select the correct db or at least considering which db you are in.

0

I got this error by attempting to fetch a value using plain GET that was originally stored with JSON.SET. I just needed to ensure I was consistently using the correct commands.

This was in Node.js, so the fix was to go from:

await client.get(key);

To:

await client.json.get(key);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.