Advertisement
Intermediate Redis, Hashes in Redis

How do you retrieve only the field names or only the values from a Redis hash, without needing both together?

Short answer

You use HKEYS to retrieve just the list of field names currently stored in a hash, and HVALS to retrieve just the list of corresponding values, which can be useful when you only need one or the other rather than the complete set of field value pairs that HGETALL would return.

Save Redis mock interview
Advertisement
AModel answer
You use HKEYS to retrieve just the list of field names currently stored in a hash, and HVALS to retrieve just the list of corresponding values, which can be useful when you only need one or the other rather than the complete set of field value pairs that HGETALL would return.
Example

HKEYS user:100 HVALS user:100

Real-world example
A data export tool uses HKEYS to first discover what fields exist across different user hashes with varying schemas, before deciding which specific fields to actually extract and include in its export.
Likely follow-ups
  • Is there a performance difference between HKEYS, HVALS, and HGETALL?
  • How do you get just the count of fields in a hash without retrieving the data itself?
Related topics