Redis Hashes
10. Redis Hashes
Section titled “10. Redis Hashes”What is a Redis Hash?
Section titled “What is a Redis Hash?”A Redis Hash stores a mapping of fields to values within a single key. It is perfect for storing object-like data.
Analogy: A Hash is like a row in a spreadsheet — the key is the row ID, and the fields are column names with their values.
How Hashes Structure Data
Section titled “How Hashes Structure Data”flowchart TB Key["Key: user:42<br/>One top-level key"] --> Fields["Fields & Values<br/>(stored efficiently together)"]
Fields --> F1["name → 'Prathamesh'"] Fields --> F2["age → '26'"] Fields --> F3["city → 'Pune'"] Fields --> F4["role → 'engineer'"] Fields --> F5["login_count → '0'"]
HSET["HSET user:42 name 'Alice' age '30'<br/>Sets multiple fields in one command"] --> Key HGET["HGET user:42 name<br/>→ Returns: 'Alice'"] -.-> F1 HGETALL["HGETALL user:42<br/>→ Returns all fields"] -.-> Fields
style Key fill:#7c3aed,color:#fff style Fields fill:#3b82f6,color:#fff style F1 fill:#f59e0b,color:#fff style F2 fill:#f59e0b,color:#fff style F3 fill:#f59e0b,color:#fff style F4 fill:#f59e0b,color:#fff style F5 fill:#f59e0b,color:#fff style HSET fill:#059669,color:#fff style HGET fill:#3b82f6,color:#fff style HGETALL fill:#ec4899,color:#fffCore Commands
Section titled “Core Commands”# Set multiple fields at onceHSET user:42 name "Prathamesh" age "26" city "Pune" role "engineer"
# Get a single fieldHGET user:42 name# Output: "Prathamesh"
# Get all fields and valuesHGETALL user:42# Output:# 1) "name"# 2) "Prathamesh"# 3) "age"# 4) "26"# 5) "city"# 6) "Pune"# 7) "role"# 8) "engineer"
# Get multiple fields at onceHMGET user:42 name city# Output: 1) "Prathamesh" 2) "Pune"
# Get just field namesHKEYS user:42# Output: name, age, city, role
# Get just valuesHVALS user:42
# Check if a field existsHEXISTS user:42 email# Output: (integer) 0
# Delete a fieldHDEL user:42 age
# Increment a numeric fieldHSET product:10 stock 100HINCRBY product:10 stock -5# Output: (integer) 95Real-World Examples
Section titled “Real-World Examples”Example 1: User Profile
HSET user:profile:101 \ username "john_doe" \ email "john@example.com" \ plan "pro" \ login_count 0
# Update login count on each loginHINCRBY user:profile:101 login_count 1Example 2: Product Catalog
HSET product:SKU-9001 \ name "Mechanical Keyboard" \ price "4500" \ stock "50" \ category "electronics"
# Update stock after saleHINCRBY product:SKU-9001 stock -1Hash vs Storing JSON as a String
Section titled “Hash vs Storing JSON as a String”| Approach | Pros | Cons |
|---|---|---|
Hash (HSET) | Update individual fields without reading all data | More commands to manage |
JSON String (SET) | Single command to store/retrieve | Must deserialize entire object to update one field |
Best practice: Use Hashes when you need to update individual fields frequently. Use JSON strings when you always read/write the entire object.