0
0
Redisquery~10 mins

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

Choose your learning style9 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.