📖 Scenario: You are managing a small online bookstore. You want to store information about books such as title, author, and price in Redis using hashes. This will help you quickly retrieve and update book details.
🎯 Goal: Build a Redis hash to store book details with fields for title, author, and price. Then add a helper variable for a book ID, update the hash with core book data, and finally add a field for stock quantity.
📋 What You'll Learn
Create a Redis hash key named book:1001 to store book details
Add a variable book_id with the value 1001
Use HSET to add fields title, author, and price to the hash
Add a new field stock with the value 30 to the hash
💡 Why This Matters
🌍 Real World
Storing objects like books in Redis hashes allows fast access and updates for web applications such as online stores.
💼 Career
Understanding Redis hashes is useful for backend developers working with caching, session storage, or real-time data storage.
Progress0 / 4 steps
1
Create a Redis hash key for the book
Create a Redis hash key named book:1001.
Redis
Hint
Use the HSET command with the key book:1001 and field book_id value "1001".
2
Add a variable for the book ID
Create a variable called book_id and set it to the string "1001".
Redis
Hint
Assign the string "1001" to the variable book_id.
3
Add book details to the hash
Use HSET with the key book:1001 to add fields title with value "Redis Basics", author with value "Jane Doe", and price with value 19.99.
Redis
Hint
Use one HSET command to add all three fields and their values.
4
Add stock quantity to the book hash
Add a new field stock with the value 30 to the Redis hash key book:1001 using HSET.
Redis
Hint
Use HSET again to add the stock field with value 30.
Practice
(1/5)
1. What does a Redis hash store?
easy
A. A set of unique keys without duplicates
B. Only a single string value per key
C. A list of keys without values
D. Multiple fields and their values under one key
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 D
Quick Check:
Hashes = multiple fields + values [OK]
Hint: Hashes group fields and values under one key [OK]
Common Mistakes:
Thinking hashes store only one value
Confusing hashes with lists or sets
Assuming keys are stored without values
2. Which Redis command correctly adds a field 'name' with value 'Alice' to a hash key 'user:1'?
easy
A. HGET user:1 name Alice
B. HSET user:1 name Alice
C. SET user:1 name Alice
D. HMGET user:1 name Alice
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 B
Quick Check:
HSET adds/updates hash fields [OK]
Hint: Use HSET to add or update hash fields [OK]
Common Mistakes:
Using HGET instead of HSET to add data
Using SET which is for strings, not hashes
Confusing HMGET (retrieves multiple fields) with HSET
3. Given the following Redis commands executed in order: HSET user:2 age 30 HSET user:2 name Bob HGETALL user:2 What is the output of HGETALL user:2?
medium
A. ["age", 30, "name", "Bob"]
B. ["user:2", "age", "30", "name", "Bob"]
C. ["age", "30", "name", "Bob"]
D. ["name", "Bob"]
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 C
Quick Check:
HGETALL returns all fields and values as string pairs [OK]
Hint: HGETALL returns flat list of field-value strings [OK]
Common Mistakes:
Expecting key name in output
Mixing data types (number vs string)
Only getting one field instead of all
4. You run the command HGET user:3 email but get a null response. What could be the reason?
medium
A. The field 'email' does not exist in the hash 'user:3'
B. The key 'user:3' is a string, not a hash
C. The Redis server is down
D. The command syntax is incorrect
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 A
Quick Check:
Missing field in hash returns null [OK]
Hint: Null means field missing, not syntax or server error [OK]
Common Mistakes:
Assuming null means server or syntax error
Confusing key type errors with missing field
Expecting error instead of null for missing field
5. You want to store user profiles with fields 'name', 'age', and 'email' in Redis hashes. Which approach is best to retrieve all user data efficiently?
hard
A. Store each user as a separate hash with key 'user:<id>' and use HGETALL to get all fields
B. Store all users in one hash with fields like 'user1_name', 'user1_age', 'user2_name', etc.
C. Store each field as a separate key like 'name:<id>', 'age:<id>', 'email:<id>'
D. Store user data as JSON strings in simple keys
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 A
Quick Check:
Separate hashes per user = organized and efficient [OK]
Hint: Use one hash per user for grouped fields and easy retrieval [OK]
Common Mistakes:
Mixing all users in one hash causing complex keys
Using separate keys for each field complicates retrieval
Storing JSON loses Redis hash benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.