Using HSET and HGET to Manage User Profiles in Redis
📖 Scenario: You are building a simple user profile system using Redis. Each user has a unique ID, and their profile information is stored as fields inside a Redis hash. You will learn how to add fields to a user's profile and retrieve specific fields using Redis commands.
🎯 Goal: Build a Redis hash for a user profile with specific fields using HSET, then retrieve a field value using HGET.
📋 What You'll Learn
Create a Redis hash key for a user profile with the key name user:1001.
Use HSET to add the fields name with value Alice and email with value alice@example.com.
Create a variable called field and set it to the string email.
Use HGET with the key user:1001 and the field stored in field to get the email address.
Store the retrieved email in a variable called user_email.
💡 Why This Matters
🌍 Real World
Storing user profiles or settings in Redis hashes is common in web applications for fast access and updates.
💼 Career
Knowing how to use HSET and HGET is essential for backend developers working with Redis to manage structured data efficiently.
Progress0 / 4 steps
1
Create a Redis hash for user profile
Use the HSET command to create a Redis hash with the key user:1001 and add the field name with the value Alice.
Redis
Hint
Remember, HSET syntax is: HSET key field value.
2
Add email field to the user profile
Use the HSET command to add the field email with the value alice@example.com to the Redis hash with the key user:1001.
Redis
Hint
You can add multiple fields to a hash by calling HSET multiple times.
3
Create a variable for the field name
Create a variable called field and set it to the string email.
Redis
Hint
Just assign the string 'email' to the variable field.
4
Retrieve the email field using HGET
Use the HGET command with the key user:1001 and the field stored in the variable field to get the email address. Store the result in a variable called user_email.
Redis
Hint
Use HGET key field to get the value. Since field is a variable, you can use its value directly.
Practice
(1/5)
1. What does the Redis command HSET do?
easy
A. Stores or updates a field and its value inside a hash key
B. Retrieves the value of a field from a hash key
C. Deletes a field from a hash key
D. Lists all keys in the database
Solution
Step 1: Understand the purpose of HSET
The HSET command 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
HGET retrieves values, HDEL deletes fields, and KEYS lists keys, so they do not match the description of HSET.
Final Answer:
Stores or updates a field and its value inside a hash key -> Option A
Quick Check:
HSET stores/updates field = A [OK]
Hint: HSET adds or changes a field inside a hash key [OK]
Common Mistakes:
Confusing HSET with HGET
Thinking HSET deletes fields
Mixing HSET with key listing commands
2. Which of the following is the correct syntax to set field name to John in hash key user:1 using Redis?
easy
A. HSET user:1 name John
B. HGET user:1 name John
C. SET user:1 name John
D. HSET name user:1 John
Solution
Step 1: Recall HSET syntax
The correct syntax is HSET key field value. Here, key is user:1, field is name, and value is John.
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 A
Quick Check:
HSET key field value = C [OK]
Hint: Remember: HSET key field value order [OK]
Common Mistakes:
Using HGET instead of HSET to set values
Swapping field and key positions
Using SET for hash fields
3. Given these Redis commands executed in order: HSET user:2 age 30 HSET user:2 city London HGET user:2 age What is the output of the last command?
medium
A. London
B. 30
C. user:2
D. nil
Solution
Step 1: Analyze the HSET commands
The first command sets field age to 30 in user:2. The second sets city to London.
Step 2: Understand HGET command
HGET user:2 age retrieves the value of field age from user:2, which was set to 30.
Final Answer:
30 -> Option B
Quick Check:
HGET user:2 age = 30 [OK]
Hint: HGET returns the value of the requested field [OK]
Common Mistakes:
Confusing field names and values
Expecting HGET to return the key name
Assuming HGET returns all fields
4. You run the command HGET user:3 email but get (nil) as output. What could be the reason?
medium
A. The key user:3 is not a hash but a string
B. The Redis server is down
C. The field email does not exist in the hash user:3
D. The syntax of HGET is incorrect
Solution
Step 1: Understand (nil) output in HGET
When HGET returns (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 field email does not exist in the hash user:3 -> Option C
Quick Check:
HGET nil means field missing = B [OK]
Hint: Nil means field missing, not syntax or server error [OK]
Common Mistakes:
Assuming (nil) means server or syntax error
Confusing missing field with missing key
Ignoring data type errors
5. You want to store user profile data in Redis using a hash key profile:100. You run: HSET profile:100 name Alice HSET profile:100 age 0 HGET profile:100 age What will the last command return and why?
hard
A. Empty string, because age is unset
B. nil, because zero is treated as a missing value
C. Error, because age cannot be zero
D. 0, because Redis stores numeric zero as a valid string value
Solution
Step 1: Understand storing zero in Redis hash
Redis stores all hash field values as strings. The value 0 is stored as the string "0", which is a valid value.
Step 2: Check HGET behavior for zero value
HGET returns the exact stored string. Since age was set to 0, it returns "0", not nil or empty.
Final Answer:
0, because Redis stores numeric zero as a valid string value -> Option D
Quick Check:
HGET returns stored string '0' = D [OK]
Hint: Zero is stored as string '0', not nil or empty [OK]
Common Mistakes:
Thinking zero is treated as missing or false
Expecting errors for numeric zero
Confusing empty string with zero
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.