Command Introduction
The Hash command family is designed to operate on the field-value mapping structure within a key, making it suitable for storing structured data such as user profiles and configuration items in a "one key = one object" manner. Its core commands can be categorized into the following four types based on their features:
Reading, Writing, and Deleting Fields
HSET / HMSET: sets the value of one or more fields.
HGET / HMGET / HGETALL: reads a single field, multiple fields in batches, or all field-value pairs.
HDEL: deletes specified fields.
HSETNX: sets a value only if the field does not exist.
Querying and Counting Fields
HEXISTS: checks whether a field exists.
HLEN: returns the number of fields.
HKEYS / HVALS: returns all field names or all field values, respectively.
HSCAN: iterates through fields in a large Hash incrementally using a cursor to avoid the blocking caused by a one-time HGETALL.
Atomic Counters
HINCRBY / HINCRBYFLOAT: atomically increments a numeric field, making it suitable for multi-dimensional counter scenarios such as user points and product inventory.
Enhanced Commands for Higher Versions (Redis 6.2+ / Valkey 8.0+)
HRANDFIELD: randomly returns one or more fields, making it suitable for sampling and random recommendation scenarios.
Command Support Overview
Basic Commands: hdel, hexists, hget, hgetall, hincrby, hincrbyfloat, hkeys, hlen, hmget, hmset, hset, hsetnx, hstrlen, hvals, hscan. These commands are supported by the standard architecture of Redis versions 2.8, 4.0, 5.0, 6.2, and 7.0, as well as the standard and cluster architectures of Valkey versions 8.0 and 9.0.
hrandfield command: is supported only by Redis versions 6.2 and 7.0, and Valkey versions 8.0 and 9.0.
Field-Level Expiration Commands: hexpire, hpexpire, hexpireat, hpexpireat, hexpiretime, hpexpiretime, httl, hpttl, hpersist, hsetex, hgetex, hgetdel. These commands are supported only by Valkey 9.0.
Cross-Slot Restriction: In a cluster architecture, a command is not supported if its operation involves Hash keys distributed across multiple different slots.
Command Compatibility Matrix
The support status of Hash family commands across different versions and architectures is shown in the table below (✓ indicates supported, ✗ indicates not supported, and — indicates that the command does not involve a cross-slot access scenario).
|
hdel | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hexists | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hget | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hgetall | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hincrby | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hincrbyfloat | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hkeys | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hlen | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hmget | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hmset | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hset | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hsetnx | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hstrlen | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | — |
hvals | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hscan | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
hrandfield | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hexpire | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hpexpire | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hexpireat | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hpexpireat | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hexpiretime | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hpexpiretime | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
httl | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hpttl | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hpersist | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hsetex | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hgetex | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |
hgetdel | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | — |