0
0
Redisquery~10 mins

Hash vs string for objects in Redis - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Hash vs string for objects
Start: Store object data
String
End
This flow shows how storing an object in Redis can use either a single string or a hash with fields, affecting how data is stored, retrieved, and updated.
Execution Sample
Redis
SET user:1 "name:John,age:30,city:NY"
HSET user:1 name John age 30 city NY
GET user:1
HGET user:1 name
Store a user object as a string and as a hash, then retrieve the whole string and a single field from the hash.
Execution Table
StepCommandActionResult
1SET user:1 "name:John,age:30,city:NY"Store entire object as one stringOK
2HSET user:1 name John age 30 city NYStore object fields separately in hash3 (fields set)
3GET user:1Retrieve whole string value"name:John,age:30,city:NY"
4HGET user:1 nameRetrieve single field 'name' from hash"John"
5HGET user:1 ageRetrieve single field 'age' from hash"30"
6SET user:1 "name:Jane,age:31,city:LA"Overwrite whole string with new dataOK
7HSET user:1 city LAUpdate only 'city' field in hash1 (field updated)
8GET user:1Retrieve updated whole string"name:Jane,age:31,city:LA"
9HGET user:1 cityRetrieve updated 'city' field from hash"LA"
💡 Commands complete; demonstrates differences in storing and updating objects as string vs hash.
Variable Tracker
KeyStartAfter Step 1After Step 2After Step 6After Step 7
user:1 (string)null"name:John,age:30,city:NY""name:John,age:30,city:NY""name:Jane,age:31,city:LA""name:Jane,age:31,city:LA"
user:1 (hash)empty{name: John, age: 30, city: NY}{name: John, age: 30, city: NY}{name: John, age: 30, city: NY}{name: John, age: 30, city: LA}
Key Moments - 2 Insights
Why does updating a field in a hash not affect other fields?
Because each field in a hash is stored separately, updating one field only changes that field, unlike a string where the whole value must be overwritten (see steps 7 and 6 in execution_table).
Why do we need to parse the string to get individual fields?
The string stores all data as one text blob, so to get a single field like 'name', you must parse the string manually. Hashes store fields separately, so you can get them directly (see steps 3 vs 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the result of HGET user:1 name?
A"John"
B"name:John"
C"user:1"
Dnull
💡 Hint
Check the 'Result' column in execution_table row for step 4.
At which step does the string value for user:1 change to "name:Jane,age:31,city:LA"?
AStep 2
BStep 6
CStep 7
DStep 9
💡 Hint
Look at the 'Action' and 'Result' columns for the string key in execution_table.
If you want to update only the city field without affecting other data, which command is better?
ASET user:1 "new string"
BHSET user:1 city NewCity
CGET user:1
DDEL user:1
💡 Hint
Refer to steps 6 and 7 in execution_table to see how updates differ.
Concept Snapshot
Redis stores objects as either strings or hashes.
Strings hold the whole object as one text value.
Hashes store each field separately as key-value pairs.
Strings require full overwrite to update.
Hashes allow updating individual fields easily.
Use hashes for flexible field access and updates.
Full Transcript
This visual execution compares storing objects in Redis as strings versus hashes. First, the object is stored as a single string with all fields combined. Then, the same object is stored as a hash with separate fields. Retrieving the string returns the whole object text, while retrieving from the hash can get individual fields directly. Updating a string requires overwriting the entire value, but updating a hash can change just one field without affecting others. This shows the practical differences in how Redis handles these two data types for objects.