What if you could find any detail instantly without flipping through pages?
Why HSET and HGET for fields in Redis? - Purpose & Use Cases
Imagine you have a notebook where you write down details about your friends, like their phone numbers and birthdays. Now, if you want to find a friend's phone number, you have to flip through every page until you find it.
This manual searching is slow and tiring. You might miss the right page or write down wrong info. It's hard to keep everything organized and find what you need quickly.
Using HSET and HGET commands in Redis is like having a digital notebook with tabs for each friend. You can quickly add or update a friend's details with HSET, and instantly get any specific detail with HGET without searching through everything.
Write all info in one big note and read it all to find one detail
HSET friend:john phone 12345
HGET friend:john phoneThis lets you store and retrieve specific pieces of information instantly, making your data easy to manage and access.
Think of a contact app on your phone: when you want to call a friend, the app quickly shows their number without you scrolling through all contacts manually.
Manual searching through data is slow and error-prone.
HSET and HGET let you store and get specific fields quickly.
This makes data handling fast, organized, and reliable.
Practice
HSET do?Solution
Step 1: Understand the purpose of HSET
TheHSETcommand is used to store or update a field-value pair inside a Redis hash key, organizing related data together.Step 2: Differentiate from other commands
HGETretrieves values,HDELdeletes fields, andKEYSlists keys, so they do not match the description ofHSET.Final Answer:
Stores or updates a field and its value inside a hash key -> Option AQuick Check:
HSET stores/updates field = A [OK]
- Confusing HSET with HGET
- Thinking HSET deletes fields
- Mixing HSET with key listing commands
name to John in hash key user:1 using Redis?Solution
Step 1: Recall HSET syntax
The correct syntax isHSET key field value. Here, key isuser:1, field isname, and value isJohn.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.Final Answer:
HSET user:1 name John -> Option AQuick Check:
HSET key field value = C [OK]
- Using HGET instead of HSET to set values
- Swapping field and key positions
- Using SET for hash fields
HSET user:2 age 30HSET user:2 city LondonHGET user:2 ageWhat is the output of the last command?
Solution
Step 1: Analyze the HSET commands
The first command sets fieldageto30inuser:2. The second setscitytoLondon.Step 2: Understand HGET command
HGET user:2 ageretrieves the value of fieldagefromuser:2, which was set to30.Final Answer:
30 -> Option BQuick Check:
HGET user:2 age = 30 [OK]
- Confusing field names and values
- Expecting HGET to return the key name
- Assuming HGET returns all fields
HGET user:3 email but get (nil) as output. What could be the reason?Solution
Step 1: Understand (nil) output in HGET
WhenHGETreturns(nil), it means the requested field does not exist in the hash key.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).Final Answer:
The fieldemaildoes not exist in the hashuser:3-> Option CQuick Check:
HGET nil means field missing = B [OK]
- Assuming (nil) means server or syntax error
- Confusing missing field with missing key
- Ignoring data type errors
profile:100. You run:HSET profile:100 name AliceHSET profile:100 age 0HGET profile:100 ageWhat will the last command return and why?
Solution
Step 1: Understand storing zero in Redis hash
Redis stores all hash field values as strings. The value0is stored as the string "0", which is a valid value.Step 2: Check HGET behavior for zero value
HGETreturns the exact stored string. Sinceagewas set to0, it returns "0", not nil or empty.Final Answer:
0, because Redis stores numeric zero as a valid string value -> Option DQuick Check:
HGET returns stored string '0' = D [OK]
- Thinking zero is treated as missing or false
- Expecting errors for numeric zero
- Confusing empty string with zero
Ready to prove it?
Pass the test and this pattern moves unknown
