📖 Scenario: You are building a simple contact list application. Each contact has a name, phone number, and email address. You want to store each contact's details in Redis in a way that groups all their information together.
🎯 Goal: Learn how to use Redis hashes to represent an object (a contact) with multiple fields, so you can easily store and retrieve all details of a contact as one unit.
📋 What You'll Learn
Create a Redis hash to store a contact's details
Add fields for name, phone, and email to the hash
Retrieve all fields of the contact from the hash
Update one field in the hash
💡 Why This Matters
🌍 Real World
Storing user profiles, product details, or any grouped data in Redis using hashes helps keep related information organized and easy to access.
💼 Career
Understanding how to use Redis hashes is valuable for backend developers and database administrators working with fast, in-memory data stores.
Progress0 / 4 steps
1
Create a Redis hash for a contact
Use the Redis command HSET to create a hash called contact:1 with these exact fields and values: name set to "Alice", phone set to "123-456-7890", and email set to "alice@example.com".
Redis
Hint
Use HSET followed by the hash key and pairs of field and value.
2
Retrieve all fields of the contact
Use the Redis command HGETALL to get all fields and values from the hash contact:1.
Redis
Hint
HGETALL returns all fields and values in the hash.
3
Update the phone number field
Use the Redis command HSET to update the phone field of contact:1 to the new value "987-654-3210".
Redis
Hint
Use HSET with the hash key, field name, and new value to update a field.
4
Retrieve the updated contact details
Use the Redis command HGETALL again to retrieve all fields and values from contact:1 and confirm the phone number was updated.
Redis
Hint
Use HGETALL to see all fields and confirm the update.
Practice
(1/5)
1. Why are Redis hashes considered similar to objects in programming?
easy
A. Because they store multiple related fields under one key, like object properties
B. Because they only store single values like simple variables
C. Because they automatically run code like functions
D. Because they store data in a list format
Solution
Step 1: Understand what a Redis hash stores
A Redis hash stores multiple field-value pairs under one key, grouping related data.
Step 2: Compare with programming objects
Programming objects group properties (fields) together, similar to how hashes group fields.
Final Answer:
Because they store multiple related fields under one key, like object properties -> Option A
Quick Check:
Hashes = grouped fields like object properties [OK]
Hint: Think of hashes as objects holding many properties [OK]
Common Mistakes:
Confusing hashes with simple string keys
Thinking hashes run code like functions
Assuming hashes store lists instead of field-value pairs
2. Which of the following is the correct Redis command to set a field 'name' with value 'Alice' in a hash 'user:1'?
easy
A. HMSET user:1 name Alice
B. SET user:1 name Alice
C. HGET user:1 name Alice
D. HSET user:1 name Alice
Solution
Step 1: Identify the command to set a single field in a hash
HSET sets a single field-value pair in a Redis hash.
Step 2: Check other commands
SET is for strings, HMSET is deprecated, HGET retrieves values.
Final Answer:
HSET user:1 name Alice -> Option D
Quick Check:
Use HSET to set hash fields [OK]
Hint: Use HSET to add or update a single hash field [OK]
Common Mistakes:
Using SET instead of HSET for hashes
Using HGET which reads but does not set
Using deprecated HMSET command
3. Given the Redis commands: HSET user:2 age 30 HSET user:2 city "New York" What will HGETALL user:2 return?
medium
A. ["user:2", "age", "30", "city", "New York"]
B. ["age", "30", "city", "New York"]
C. ["age", 30, "city", "New York"]
D. ["age", "city", "30", "New York"]
Solution
Step 1: Understand HGETALL output format
HGETALL returns an array of field and value strings alternating.
Step 2: Match fields and values
Fields are "age" and "city" with values "30" and "New York" as strings.
Final Answer:
["age", "30", "city", "New York"] -> Option B
Quick Check:
HGETALL returns field-value pairs as strings [OK]
Hint: HGETALL returns fields and values as string pairs [OK]
Common Mistakes:
Including the key name in the output
Returning values as numbers instead of strings
Mixing field and value order
4. You run the command HSET user:3 name "Bob" age 25 but get an error. What is the likely cause?
medium
A. Field names cannot be strings
B. The key 'user:3' does not exist yet
C. HSET only accepts one field-value pair at a time in this Redis version
D. Age must be a string, not a number
Solution
Step 1: Check HSET command syntax
Older Redis versions allow only one field-value pair per HSET call.
Step 2: Analyze error cause
Trying to set two pairs at once causes syntax error in older versions.
Final Answer:
HSET only accepts one field-value pair at a time in this Redis version -> Option C
Quick Check:
HSET single pair per call in older Redis [OK]
Hint: Check Redis version for multi-field HSET support [OK]
Common Mistakes:
Assuming key must exist before HSET
Thinking field names can't be strings
Confusing data type requirements for values
5. You want to store user profiles with fields: 'name', 'age', and 'email' in Redis. Which approach best uses hashes to represent these profiles efficiently?
hard
A. Use a separate hash for each user with key like 'user:<id>' and fields for each property
B. Store all users in one big hash with user IDs as fields and JSON strings as values
C. Use simple string keys for each property like 'name:<id>', 'age:<id>', 'email:<id>'
D. Store user data as a list with all properties concatenated as one string
Solution
Step 1: Consider data organization for user profiles
Each user should have a separate hash to group their properties clearly.
Step 2: Evaluate other options
One big hash mixes users, separate strings scatter data, lists lose field names.
Final Answer:
Use a separate hash for each user with key like 'user:<id>' and fields for each property -> Option A
Quick Check:
Separate hashes per user = clear, efficient object representation [OK]
Hint: Use one hash per object for clear property grouping [OK]
Common Mistakes:
Mixing all users in one hash losing clarity
Using separate keys for each property causing scattered data
Storing data as lists losing field names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.