HSET and HGET for fields in Redis - Time & Space Complexity
When working with Redis hashes, it is important to understand how fast commands like HSET and HGET run as the data grows.
We want to know how the time to set or get a field changes when the number of fields in the hash increases.
Analyze the time complexity of the following Redis commands.
HSET user:1000 name "Alice"
HSET user:1000 age "30"
HGET user:1000 name
HGET user:1000 age
This code sets two fields in a hash and then retrieves them.
Look for repeated actions that affect time.
- Primary operation: Accessing a field inside a hash (setting or getting).
- How many times: Each command accesses one field directly without scanning all fields.
Getting or setting a field takes about the same time no matter how many fields are in the hash.
| Input Size (number of fields) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays roughly the same even as the number of fields grows.
Time Complexity: O(1)
This means setting or getting a field in a Redis hash takes constant time, no matter how many fields are stored.
[X] Wrong: "Accessing a field in a hash gets slower as the hash grows because Redis must look through all fields."
[OK] Correct: Redis uses a data structure that lets it find fields directly without scanning, so time stays constant.
Knowing that Redis hash field access is constant time helps you explain how Redis handles data efficiently, a useful skill in many real-world projects.
"What if we used HGETALL to get all fields instead of HGET for one field? How would the time complexity change?"
Practice
HSET do?Solution
Step 1: Understand the purpose of HSET
TheHSETcommand is used to store or update a field-value pair inside a Redis hash key, organizing related data together.Step 2: Differentiate from other commands
HGETretrieves values,HDELdeletes fields, andKEYSlists keys, so they do not match the description ofHSET.Final Answer:
Stores or updates a field and its value inside a hash key -> Option AQuick Check:
HSET stores/updates field = A [OK]
- Confusing HSET with HGET
- Thinking HSET deletes fields
- Mixing HSET with key listing commands
name to John in hash key user:1 using Redis?Solution
Step 1: Recall HSET syntax
The correct syntax isHSET key field value. Here, key isuser:1, field isname, and value isJohn.Step 2: Check each option
HSET user:1 name John matches the syntax exactly. HGET user:1 name John uses HGET which is for retrieval. SET user:1 name John uses SET which is for string keys, not hashes. HSET name user:1 John swaps field and key, which is incorrect.Final Answer:
HSET user:1 name John -> Option AQuick Check:
HSET key field value = C [OK]
- Using HGET instead of HSET to set values
- Swapping field and key positions
- Using SET for hash fields
HSET user:2 age 30HSET user:2 city LondonHGET user:2 ageWhat is the output of the last command?
Solution
Step 1: Analyze the HSET commands
The first command sets fieldageto30inuser:2. The second setscitytoLondon.Step 2: Understand HGET command
HGET user:2 ageretrieves the value of fieldagefromuser:2, which was set to30.Final Answer:
30 -> Option BQuick Check:
HGET user:2 age = 30 [OK]
- Confusing field names and values
- Expecting HGET to return the key name
- Assuming HGET returns all fields
HGET user:3 email but get (nil) as output. What could be the reason?Solution
Step 1: Understand (nil) output in HGET
WhenHGETreturns(nil), it means the requested field does not exist in the hash key.Step 2: Evaluate other options
If the key was not a hash, Redis would return a type error, not(nil). Server down would cause connection errors, and incorrect syntax would cause errors, not(nil).Final Answer:
The fieldemaildoes not exist in the hashuser:3-> Option CQuick Check:
HGET nil means field missing = B [OK]
- Assuming (nil) means server or syntax error
- Confusing missing field with missing key
- Ignoring data type errors
profile:100. You run:HSET profile:100 name AliceHSET profile:100 age 0HGET profile:100 ageWhat will the last command return and why?
Solution
Step 1: Understand storing zero in Redis hash
Redis stores all hash field values as strings. The value0is stored as the string "0", which is a valid value.Step 2: Check HGET behavior for zero value
HGETreturns the exact stored string. Sinceagewas set to0, it returns "0", not nil or empty.Final Answer:
0, because Redis stores numeric zero as a valid string value -> Option DQuick Check:
HGET returns stored string '0' = D [OK]
- Thinking zero is treated as missing or false
- Expecting errors for numeric zero
- Confusing empty string with zero
Ready to prove it?
Pass the test and this pattern moves unknown
