0
0
Redisquery~10 mins

Object storage with hashes in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object storage with hashes
Start
Create Hash Key
Add Field-Value Pairs
Store Hash in Redis
Retrieve Hash by Key
Access Specific Fields
End
This flow shows how to create a hash key, add field-value pairs, store it in Redis, and retrieve specific fields from the hash.
Execution Sample
Redis
HSET user:1000 name "Alice" age "30" city "NY"
HGET user:1000 name
HGETALL user:1000
Store user data as a hash with fields name, age, city; then retrieve the name and all fields.
Execution Table
StepCommandActionResult
1HSET user:1000 name "Alice" age "30" city "NY"Create hash key 'user:1000' and set fields(integer) 3
2HGET user:1000 nameRetrieve 'name' field from hash 'user:1000'"Alice"
3HGETALL user:1000Retrieve all fields and values from hash 'user:1000'["name", "Alice", "age", "30", "city", "NY"]
4HGET user:1000 countryTry to get non-existing field 'country'(nil)
💡 No more commands; demonstration ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
user:1000 (hash)empty{"name":"Alice", "age":"30", "city":"NY"}{"name":"Alice", "age":"30", "city":"NY"}{"name":"Alice", "age":"30", "city":"NY"}{"name":"Alice", "age":"30", "city":"NY"}
name fieldnoneset to "Alice""Alice""Alice""Alice"
country fieldnonenonenonenonenone (does not exist)
Key Moments - 2 Insights
Why does HGET return nil when asking for a field that does not exist?
Because in step 4 of the execution_table, the field 'country' was never set in the hash 'user:1000', so Redis returns nil to indicate no such field.
Does HSET overwrite existing fields or add new ones?
HSET adds new fields or overwrites existing ones. In step 1, all fields are set fresh. If you run HSET again with the same field, it updates the value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the HGET command at step 2?
A"Alice"
B"30"
Cnil
D["name", "Alice"]
💡 Hint
Check the 'Result' column for step 2 in the execution_table.
At which step does the hash 'user:1000' get created and fields set?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column to see when the hash key is created.
If you try to get a field that does not exist, what does Redis return according to the execution_table?
AAn empty string
Bnil
CAn error message
DThe whole hash
💡 Hint
See the result of step 4 in the execution_table.
Concept Snapshot
Object storage with hashes in Redis:
- Use HSET key field value [field value ...] to store multiple fields.
- Use HGET key field to get a single field's value.
- Use HGETALL key to get all fields and values.
- Non-existing fields return nil.
- Hashes store related data under one key for easy access.
Full Transcript
This visual execution shows how Redis stores objects using hashes. First, a hash key 'user:1000' is created with fields 'name', 'age', and 'city' using HSET. Then, HGET retrieves the 'name' field, returning 'Alice'. HGETALL returns all fields and values as a list. Trying to get a non-existing field 'country' returns nil. Variables track the hash content and field values step-by-step. Key moments clarify why nil is returned for missing fields and how HSET updates or adds fields. The quiz tests understanding of these steps and results.