Object storage with hashes in Redis - Time & Space Complexity
When storing objects using hashes in Redis, it is important to understand how the time to store or retrieve data changes as the object size grows.
We want to know how the number of operations changes when we add more fields to a hash.
Analyze the time complexity of the following Redis commands storing an object as a hash.
HSET user:1000 name "Alice"
HSET user:1000 age "30"
HSET user:1000 email "alice@example.com"
HGETALL user:1000
This code stores multiple fields of a user object in a Redis hash and then retrieves all fields at once.
Look for repeated actions that affect performance.
- Primary operation: Setting or getting each field in the hash.
- How many times: Once per field stored or retrieved.
As the number of fields in the hash grows, the time to store or retrieve all fields grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 fields | About 10 operations |
| 100 fields | About 100 operations |
| 1000 fields | About 1000 operations |
Pattern observation: The time grows linearly as the number of fields increases.
Time Complexity: O(n)
This means the time to store or retrieve all fields grows directly with the number of fields in the hash.
[X] Wrong: "Storing or getting a hash is always fast and constant time no matter how many fields it has."
[OK] Correct: Each field requires its own operation, so more fields mean more work and longer time.
Understanding how Redis hashes scale helps you explain data storage choices clearly and shows you know how performance changes with data size.
"What if we used multiple smaller hashes instead of one big hash? How would that affect the time complexity when retrieving all data?"
Practice
Solution
Step 1: Understand Redis hash structure
Redis hashes store multiple field-value pairs grouped under a single key.Step 2: Compare options with hash definition
Only Multiple fields and their values under one key correctly describes storing multiple fields and values under one key.Final Answer:
Multiple fields and their values under one key -> Option DQuick Check:
Hashes = multiple fields + values [OK]
- Thinking hashes store only one value
- Confusing hashes with lists or sets
- Assuming keys are stored without values
Solution
Step 1: Identify command to add/update hash field
HSET is the Redis command to set a field in a hash.Step 2: Check command syntax
HSET key field value is correct; so HSET user:1 name Alice is valid.Final Answer:
HSET user:1 name Alice -> Option BQuick Check:
HSET adds/updates hash fields [OK]
- Using HGET instead of HSET to add data
- Using SET which is for strings, not hashes
- Confusing HMGET (retrieves multiple fields) with HSET
HSET user:2 age 30HSET user:2 name BobHGETALL user:2What is the output of
HGETALL user:2?Solution
Step 1: Understand HSET commands
HSET adds fields 'age' with '30' and 'name' with 'Bob' to hash 'user:2'.Step 2: Understand HGETALL output format
HGETALL returns an array of field-value pairs as strings in order added.Final Answer:
["age", "30", "name", "Bob"] -> Option CQuick Check:
HGETALL returns all fields and values as string pairs [OK]
- Expecting key name in output
- Mixing data types (number vs string)
- Only getting one field instead of all
HGET user:3 email but get a null response. What could be the reason?Solution
Step 1: Understand HGET behavior
HGET returns null if the field does not exist in the hash.Step 2: Check other options
Key being a string would cause an error, not null; server down or syntax error would cause different errors.Final Answer:
The field 'email' does not exist in the hash 'user:3' -> Option AQuick Check:
Missing field in hash returns null [OK]
- Assuming null means server or syntax error
- Confusing key type errors with missing field
- Expecting error instead of null for missing field
Solution
Step 1: Analyze storage options
Storing each user as a separate hash groups related fields logically and allows efficient access.Step 2: Evaluate retrieval efficiency
Using HGETALL on 'user:<id>' fetches all fields at once, simpler and faster than scattered keys or complex parsing.Final Answer:
Store each user as a separate hash with key 'user:<id>' and use HGETALL to get all fields -> Option AQuick Check:
Separate hashes per user = organized and efficient [OK]
- Mixing all users in one hash causing complex keys
- Using separate keys for each field complicates retrieval
- Storing JSON loses Redis hash benefits
Ready to prove it?
Pass the test and this pattern moves unknown
