HMSET and HMGET for bulk in Redis - Time & Space Complexity
We want to understand how the time to store and retrieve many fields in Redis changes as the number of fields grows.
How does the work grow when using HMSET and HMGET with many fields at once?
Analyze the time complexity of the following code snippet.
HMSET user:1000 name "Alice" age "30" city "Paris" country "France"
HMGET user:1000 name age city country
This code stores multiple fields for a user in one command and then retrieves those fields in one command.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Processing each field in the hash for setting or getting.
- How many times: Once for each field included in the HMSET or HMGET command.
As you add more fields to set or get, the time to complete the command grows roughly in direct proportion to the number of fields.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Pattern observation: The work grows linearly as you add more fields.
Time Complexity: O(n)
This means the time to set or get fields grows directly with the number of fields you include.
[X] Wrong: "HMSET and HMGET always take constant time no matter how many fields."
[OK] Correct: Each field must be processed, so more fields mean more work and more time.
Understanding how bulk commands scale helps you design efficient data access patterns and shows you can think about performance in real systems.
"What if we used multiple single-field HSET and HGET commands instead of one HMSET and HMGET? How would the time complexity change?"
Practice
HMSET do?Solution
Step 1: Understand HMSET purpose
The HMSET command is used to save many fields and their values inside one hash key in Redis.Step 2: Compare with other commands
Other commands like DEL delete keys, and INCR increments values, so they do not match HMSET's bulk saving role.Final Answer:
Stores multiple fields and their values in a single hash key at once. -> Option CQuick Check:
HMSET saves many fields at once = B [OK]
- Confusing HMSET with commands that delete keys
- Thinking HMSET retrieves data instead of saving
- Mixing HMSET with single field commands like HSET
user:1 using HMSET?Solution
Step 1: Recall HMSET syntax
HMSET expects the key followed by field and value pairs separated by spaces, without brackets or braces.Step 2: Check each option
HMSET user:1 name "Alice" age 30 city "NY" matches the correct syntax: key, field, value, field, value... Options B, C, and D use invalid brackets or JSON-like syntax not accepted by Redis.Final Answer:
HMSET user:1 name "Alice" age 30 city "NY" -> Option DQuick Check:
HMSET syntax is key + field value pairs space-separated = A [OK]
- Using brackets or braces in HMSET command
- Putting fields and values inside parentheses
- Confusing HMSET with JSON syntax
HMSET product:100 name "Laptop" price 1200 stock 30HMGET product:100 name priceWhat will be the output of the HMGET command?
Solution
Step 1: Understand HMSET stored data
The HMSET command stores fields 'name' with value "Laptop" and 'price' with value "1200" (as string) in the hash 'product:100'.Step 2: Understand HMGET output
HMGET fetches the values of the requested fields in order, returning an array of strings: ["Laptop", "1200"].Final Answer:
["Laptop", "1200"] -> Option AQuick Check:
HMGET returns values as strings in array = A [OK]
- Expecting numeric types instead of strings
- Confusing HMGET output with field names
- Getting only one value instead of array
HMGET user:5 name age but get (nil) for both fields. What is the most likely reason?Solution
Step 1: Interpret (nil) results
Getting (nil) means the requested fields or the hash key do not exist in Redis.Step 2: Check command syntax and server status
HMGET syntax is correct without brackets, and if the server was down, no response would be received.Final Answer:
The hash keyuser:5does not exist or has no such fields. -> Option BQuick Check:
(nil) means missing key or fields = D [OK]
- Adding brackets around fields in HMGET
- Assuming HMGET cannot fetch multiple fields
- Thinking server down causes (nil) responses
name, email, and age in Redis using HMSET, then retrieve only email and age with HMGET. Which sequence of commands is correct?Solution
Step 1: Write correct HMSET command
HMSET requires key followed by field-value pairs separated by spaces without brackets or equals signs, so HMSET user:10 name "Bob" email "bob@example.com" age 25
HMGET user:10 email age is correct.Step 2: Write correct HMGET command
HMGET takes the key and list of fields separated by spaces, so HMSET user:10 name "Bob" email "bob@example.com" age 25
HMGET user:10 email age's HMGET is correct.Final Answer:
HMSET user:10 name "Bob" email "bob@example.com" age 25
HMGET user:10 email age -> Option AQuick Check:
HMSET and HMGET use space-separated fields and values = C [OK]
- Using JSON or bracket syntax in HMSET
- Adding parentheses around fields in HMGET
- Using equals signs in HMSET command
Ready to prove it?
Pass the test and this pattern moves unknown
