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 ...]
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
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 ...]
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
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
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
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
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
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 ...]
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:
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.