cs
Redis hashes are used to store structured data under a single key.
A hash is essentially a collection of field–value pairs, similar to an object or dictionary.
Example structure:
user:1001
name → "Ufraan"
age → "20"
city → "Hyderabad"
Instead of creating separate keys for each attribute, hashes group related fields together.
Hashes are commonly used for:
- user profiles
- configuration data
- application objects
Setting Fields
HSET
Adds or updates a field inside a hash.
HSET user:1001 name "ufraan"
HSET user:1001 age 20
HSET user:1001 city "Hyderabad"
Retrieving Data
HGET
Retrieve a specific field.
HGET user:1001 name
Output:
"ufraan"
HGETALL
Returns all fields and values.
HGETALL user:1001
Example output:
1) "name"
2) "ufraan"
3) "age"
4) "20"
5) "city"
6) "Hyderabad"
Updating Fields
Running HSET again updates the value.
HSET user:1001 age 21
Removing Fields
HDEL
Deletes a field from the hash.
HDEL user:1001 city
Getting All Field Names
HKEYS
HKEYS user:1001
Returns:
"name"
"age"
Why Hashes Are Useful
Hashes allow storing related information in one place instead of spreading data across many keys.
For example, instead of:
user:1001:name
user:1001:age
user:1001:city
You can simply store:
user:1001 → { name, age, city }
This keeps Redis data more organized and efficient.
Related: [[Redis]] [[Redis - Lists]] [[Redis - Sets]] [[Redis - Strings]] [[Redis - Sorted sets]]