Search code examples
redis

Redis hash vs key hierarchy


What's the practical difference between keeping data in multiple hashes (HSET foo oof 1, HSET bar rab 2) and using plain keys in a hierarchy (SET foo:oof 1, SET bar:rab 2)?


Solution

  • According to the manual, you'd use hashes to represent a single object.

    Also, it's not that efficient to iterate over Redis keys, so if you need to get all the data from a single object, HGETALL is your friend, not a KEYS thing:10:*/multiget fiasco.

    However, you can't e.g. set expiry for only one key of a hash, so if you need that functionality, you'll want to use regular keys.