HKEYS and HVALS in Redis - Time & Space Complexity
We want to understand how the time it takes to get all keys or all values from a Redis hash changes as the hash grows.
How does the number of fields in the hash affect the work Redis does when using HKEYS or HVALS?
Analyze the time complexity of the following code snippet.
HKEYS myhash
HVALS myhash
This code gets all the field names with HKEYS and all the field values with HVALS from the hash named 'myhash'.
- Primary operation: Redis scans through each field in the hash to collect keys or values.
- How many times: Once for each field in the hash.
As the number of fields in the hash grows, Redis must look at each field once to gather keys or values.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Pattern observation: The work grows directly with the number of fields; doubling fields doubles the work.
Time Complexity: O(n)
This means the time to get all keys or values grows in a straight line with the number of fields in the hash.
[X] Wrong: "HKEYS or HVALS always run in constant time, no matter how big the hash is."
[OK] Correct: Redis must look at every field to collect keys or values, so the time grows with the number of fields.
Knowing how commands like HKEYS and HVALS scale helps you understand performance when working with large hashes in Redis.
"What if we used HSCAN instead of HKEYS or HVALS? How would the time complexity change?"
Practice
HKEYS myhash return?Solution
Step 1: Understand the purpose of HKEYS
The commandHKEYSis used to get all the field names (keys) from a Redis hash.Step 2: Apply to the given hash
When you runHKEYS myhash, it returns all the keys insidemyhash, not the values or count.Final Answer:
All the field names (keys) inside the hashmyhash-> Option AQuick Check:
HKEYS returns keys = A [OK]
- Confusing HKEYS with HVALS
- Thinking it returns values instead of keys
- Assuming it returns the count of fields
user:100?Solution
Step 1: Identify the command to get all values
The Redis command to get all values from a hash isHVALS.Step 2: Check syntax correctness
The correct syntax isHVALS keyname, soHVALS user:100is correct. Parentheses are not used in Redis commands.Final Answer:
HVALS user:100 -> Option AQuick Check:
HVALS syntax = HVALS key [OK]
- Using parentheses like a function call
- Mixing HKEYS and HVALS commands
- Using HGETALL instead of HVALS for values only
product:1 with fields and values:{"name": "Pen", "color": "blue", "price": "1.5"}What will
HVALS product:1 return?Solution
Step 1: Understand what HVALS returns
HVALS returns all the values stored in the hash fields, not the keys.Step 2: Apply to the given hash
The values are "Pen", "blue", and "1.5". So the command returns these values as a list.Final Answer:
["Pen", "blue", "1.5"] -> Option BQuick Check:
HVALS returns values list = A [OK]
- Confusing keys with values
- Expecting field names instead of values
- Thinking HVALS returns the hash key name
HKEYS myhash but get an empty list even though you know myhash has data. What could be the problem?Solution
Step 1: Check the data type of the key
Ifmyhashis not a hash but a string or other type,HKEYSreturns an empty list.Step 2: Understand why other options are incorrect
TypingHVALSwould return values, not empty list. Server down causes connection errors, not empty list. An empty hash would return empty list but question says it has data.Final Answer:
myhashis not a hash type but a string or other type -> Option CQuick Check:
Wrong type key = empty HKEYS result [OK]
- Assuming empty list means empty hash
- Ignoring key data type
- Thinking server down returns empty list
session:123 but only want the values that are not empty strings. Which approach using HKEYS and HVALS is best?Solution
Step 1: Understand what HKEYS and HVALS return
HKEYS returns all field names, HVALS returns all values including empty strings.Step 2: Choose a method to filter empty values
Since HVALS returns all values without keys, filtering empty strings is hard without knowing which value belongs to which key. Using HKEYS to get keys, then fetching each value with HGET allows filtering empty strings precisely.Step 3: Evaluate other options
HVALS alone does not provide keys, so filtering loses context. HGETALL returns all keys and values but question asks about HKEYS and HVALS usage. Using only HKEYS does not get values.Final Answer:
Use HKEYS to get keys, then fetch and filter values with HGET -> Option DQuick Check:
Filter empty values by keys + HGET = B [OK]
- Filtering values without keys loses context
- Assuming HVALS excludes empty strings
- Using only HKEYS without fetching values
Ready to prove it?
Pass the test and this pattern moves unknown
