Bird
Raised Fist0
Redisquery~10 mins

HSET and HGET for fields in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - HSET and HGET for fields
Start
↓
HSET key field value
↓
Store field-value in hash
↓
HGET key field
↓
Retrieve value for field
↓
Output value
↓
End
The flow shows setting a field-value pair in a hash with HSET, then retrieving the value with HGET.
Execution Sample
Redis
HSET user:1 name "Alice"
HSET user:1 age "30"
HGET user:1 name
HGET user:1 age
This code sets two fields 'name' and 'age' in the hash 'user:1' and then retrieves their values.
Execution Table
StepCommandActionResultHash State
1HSET user:1 name "Alice"Set field 'name' to 'Alice' in 'user:1'1 (new field added){"name": "Alice"}
2HSET user:1 age "30"Set field 'age' to '30' in 'user:1'1 (new field added){"name": "Alice", "age": "30"}
3HGET user:1 nameGet value of field 'name' from 'user:1'Alice{"name": "Alice", "age": "30"}
4HGET user:1 ageGet value of field 'age' from 'user:1'30{"name": "Alice", "age": "30"}
5HGET user:1 emailGet value of non-existing field 'email'nil (field does not exist){"name": "Alice", "age": "30"}
💡 No more commands; retrieval ends after last HGET.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
user:1 hash{}{"name": "Alice"}{"name": "Alice", "age": "30"}{"name": "Alice", "age": "30"}{"name": "Alice", "age": "30"}{"name": "Alice", "age": "30"}
name field valuenilAliceAliceAliceAliceAlice
age field valuenilnil30303030
email field valuenilnilnilnilnilnil
Key Moments - 2 Insights
Why does HGET return nil for a field that was never set?
Because the field does not exist in the hash, as shown in step 5 of the execution_table where HGET user:1 email returns nil.
What does the number returned by HSET mean?
It shows how many new fields were added. In steps 1 and 2, HSET returns 1 because each time a new field is added to the hash.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the hash state after step 2?
A{"name": "Alice"}
B{}
C{"name": "Alice", "age": "30"}
D{"age": "30"}
💡 Hint
Check the 'Hash State' column in row 2 of the execution_table.
At which step does HGET return nil?
AStep 5
BStep 4
CStep 3
DNone
💡 Hint
Look at the 'Result' column for each HGET command in the execution_table.
If we run HSET user:1 name "Bob" after step 2, what will HGET user:1 name return?
A"Alice"
B"Bob"
Cnil
DError
💡 Hint
HSET updates the field value if it already exists, so the latest value is returned.
Concept Snapshot
HSET key field value - sets a field in a hash.
Returns 1 if new field added, 0 if updated.
HGET key field - gets the value of a field.
Returns nil if field does not exist.
Used to store and retrieve small sets of key-value pairs inside a Redis key.
Full Transcript
This visual execution shows how Redis commands HSET and HGET work with hash fields. First, HSET adds a field and value to a hash key. Each new field added returns 1. Then HGET retrieves the value for a given field. If the field exists, it returns the stored value. If the field does not exist, it returns nil. The hash state updates after each HSET, and remains unchanged after HGET. This helps store multiple related values under one key efficiently.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Stores or updates a field and its value inside a hash key -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    HSET user:1 name John -> Option A
  4. 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

  1. Step 1: Analyze the HSET commands

    The first command sets field age to 30 in user:2. The second sets city to London.
  2. Step 2: Understand HGET command

    HGET user:2 age retrieves the value of field age from user:2, which was set to 30.
  3. Final Answer:

    30 -> Option B
  4. 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

  1. Step 1: Understand (nil) output in HGET

    When HGET returns (nil), it means the requested field does not exist in the hash key.
  2. 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).
  3. Final Answer:

    The field email does not exist in the hash user:3 -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    0, because Redis stores numeric zero as a valid string value -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard