Skip to main content
A hash is a map of field-value pairs stored under a single key. You can think of each hash as a lightweight object or record: one key holds all the attributes of a user, a product, or any other entity without needing to scatter them across many string keys. Hash commands let you read, write, and introspect individual fields or the entire map in one round-trip.

HSET

Sets one or more fields on the hash stored at key. If the key does not exist, a new hash is created. Existing fields are overwritten with the new value; brand-new fields are added. Syntax: HSET key field value [field value ...]
HSET user:1 name Alice age 30 email alice@example.com
# (integer) 3  (three new fields were added)

HSET user:1 age 31
# (integer) 0  (field existed, updated in place)
Returns: Integer — the number of fields that were newly added (not updated).

HGET

Returns the value of a single field in the hash stored at key. Syntax: HGET key field
HGET user:1 name
# "Alice"

HGET user:1 phone
# (nil)
Returns: Bulk string — the field value, or nil if the key or field does not exist.

HDEL

Removes one or more fields from the hash stored at key. Fields that do not exist are silently ignored. Syntax: HDEL key field [field ...]
HDEL user:1 email
# (integer) 1

HDEL user:1 phone fax
# (integer) 0  (neither field existed)
Returns: Integer — the number of fields that were actually removed.

HGETALL

Returns all field-value pairs in the hash stored at key as a flat array: field₁, value₁, field₂, value₂, … Syntax: HGETALL key
HGETALL user:1
# 1) "name"
# 2) "Alice"
# 3) "age"
# 4) "31"
Returns: Array of bulk strings alternating field and value. Returns an empty array if the key does not exist.

HKEYS

Returns all field names in the hash stored at key. Syntax: HKEYS key
HKEYS user:1
# 1) "name"
# 2) "age"
Returns: Array of bulk strings — one entry per field. Returns an empty array if the key does not exist.

HVALS

Returns all values in the hash stored at key, in the same order as HKEYS. Syntax: HVALS key
HVALS user:1
# 1) "Alice"
# 2) "31"
Returns: Array of bulk strings — one entry per value. Returns an empty array if the key does not exist.

HLEN

Returns the number of fields in the hash stored at key. Syntax: HLEN key
HLEN user:1
# (integer) 2
Returns: Integer — the field count, or 0 if the key does not exist.

HEXISTS

Tests whether a field exists in the hash stored at key. Syntax: HEXISTS key field
HEXISTS user:1 name
# (integer) 1

HEXISTS user:1 phone
# (integer) 0
Returns: 1 if the field exists, 0 if it does not (or if the key does not exist).

HMGET

Returns the values of multiple fields in one call. For any field that does not exist, nil is returned in that position. Syntax: HMGET key field [field ...]
HMGET user:1 name age phone
# 1) "Alice"
# 2) "31"
# 3) (nil)
Returns: Array of bulk strings (or nil entries) in the same order as the requested fields.

Practical example: user profile

Hashes are a natural fit for storing records. Here’s a complete flow for managing a user profile:
# Create the profile
HSET user:1 name Alice age 30 email alice@example.com

# Read a single attribute
HGET user:1 name
# "Alice"

# Read the whole record
HGETALL user:1
# 1) "name"
# 2) "Alice"
# 3) "age"
# 4) "30"
# 5) "email"
# 6) "alice@example.com"

# List just the field names
HKEYS user:1
# [name, age, email]

# Check field count
HLEN user:1
# (integer) 3

# Update one field
HSET user:1 age 31

# Remove a field
HDEL user:1 email

# Confirm removal
HEXISTS user:1 email
# (integer) 0
Use HSET with multiple field-value pairs instead of issuing several individual HSET calls. A single round-trip is faster and ensures all fields land atomically.
HGETALL returns fields in insertion order as stored internally. If you need a guaranteed order, use HKEYS to retrieve and sort field names, then fetch values with HMGET.