0
0
Redisquery~10 mins

Why hashes represent objects in Redis - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why hashes represent objects
Start: Create Redis Hash
Add Field-Value Pairs
Store as Single Key
Access Fields by Name
Update or Retrieve Specific Fields
Hash Represents Object with Properties
This flow shows how Redis hashes store multiple field-value pairs under one key, representing an object with properties.
Execution Sample
Redis
HSET user:1000 name "Alice" age "30" city "NY"
HGET user:1000 name
HGETALL user:1000
Create a hash for user:1000 with fields name, age, city; then get the name field and all fields.
Execution Table
StepCommandActionResult
1HSET user:1000 name "Alice" age "30" city "NY"Create hash key 'user:1000' with fieldsFields set: name=Alice, age=30, city=NY
2HGET user:1000 nameRetrieve 'name' field from hashAlice
3HGETALL user:1000Retrieve all fields and valuesname: Alice, age: 30, city: NY
4HSET user:1000 age "31"Update 'age' field to 31Field 'age' updated to 31
5HGETALL user:1000Retrieve all fields after updatename: Alice, age: 31, city: NY
6DEL user:1000Delete the hash keyKey 'user:1000' deleted
7HGETALL user:1000Try to get fields from deleted key(nil)
💡 Execution stops after key deletion; no fields exist for retrieval.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 6
user:1000does not exist{name: Alice, age: 30, city: NY}{name: Alice, age: 31, city: NY}does not exist
Key Moments - 3 Insights
Why does Redis use a single key with multiple fields instead of multiple keys for each property?
Because the hash stores all related fields under one key, it groups properties like an object, making access and updates efficient and organized, as shown in execution_table steps 1 and 3.
What happens when you update a field in a Redis hash?
Updating a field changes only that field's value without affecting others, as seen in step 4 where 'age' changes from 30 to 31, confirmed by step 5.
What does retrieving all fields from a hash return after deletion?
After deleting the hash key (step 6), retrieving fields returns nil (step 7), meaning the object no longer exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'age' after step 4?
A31
B30
Cnil
DError
💡 Hint
Check the 'Result' column in step 4 and step 5 for the updated 'age' value.
At which step does the hash key 'user:1000' get deleted?
AStep 3
BStep 4
CStep 6
DStep 7
💡 Hint
Look for the 'DEL' command in the 'Command' column.
If you add a new field 'email' to the hash after step 5, how would the variable_tracker change?
AThe 'user:1000' variable would be deleted
BA new column 'After Step 5' with 'email' field added
CNo change because fields can't be added
DThe 'age' field would reset to 30
💡 Hint
Adding fields updates the hash object state, reflected in variable_tracker columns.
Concept Snapshot
Redis hashes store multiple field-value pairs under one key.
They represent objects with properties.
Use HSET to add/update fields.
Use HGET or HGETALL to retrieve fields.
Efficient for grouped data access and updates.
Full Transcript
This visual execution shows how Redis hashes represent objects by storing multiple fields under a single key. First, a hash key 'user:1000' is created with fields name, age, and city. Then, retrieving a single field or all fields shows the stored data. Updating a field changes only that field's value. Deleting the key removes the entire object. This grouping makes Redis hashes a natural way to represent objects with properties.