All Products
Search
Document Center

Tair (Redis® OSS-Compatible):exHash

Last Updated:Mar 28, 2026

TairHash (exHash) is a hash data structure that lets you set an expiration time and version for each field. This capability enhances the flexibility of the hash data structure and simplifies development in many scenarios.

Overview

TairHash extends the standard Redis hash with two capabilities that native Redis hash lacks: per-field expiration and per-field versioning. You can set a TTL on individual fields without affecting the rest of the key, and use version numbers to prevent conflicting concurrent writes.

TairHash uses an Active Expire algorithm that proactively checks and deletes expired fields without noticeably affecting response time. It also supports passive expiration, which removes expired fields on access.

Key features

  • Set an expiration time and version for each field.

  • Use active and passive expiration policies for fields.

  • Use syntax similar to the native Redis hash data type.

This module is open source. For more information, see TairHash.

Prerequisites

Before you begin, ensure you have:

Keep your instance on the latest minor version for additional features and improved stability. See Minor version update. For instances using a cluster architecture or read/write splitting architecture, also update the proxy nodes — otherwise some commands may not be recognized.

Precautions

The commands operate on TairHash data in a Tair instance.

Commands

The following tables list all TairHash commands, grouped by operation type.

Write commands

CommandSyntaxDescription
EXHSETEXHSET key field value [EX time] [EXAT time] [PX time] [PXAT time] [NX | XX] [VER | ABS version] [KEEPTTL]Adds a field to the specified TairHash key. Creates the key if it does not exist. Overwrites the field value if the field already exists.
EXHMSETEXHMSET key field value [field value ...]Adds multiple fields to the specified TairHash key. Creates the key if it does not exist. Overwrites existing field values.
EXHINCRBYEXHINCRBY key field num [EX time] [EXAT time] [PX time] [PXAT time] [VER | ABS version] [MIN minval] [MAX maxval] [KEEPTTL]Increments the integer value of a field by num. Creates the key or field if they do not exist (field starts at 0 before the increment).
EXHINCRBYFLOATEXHINCRBYFLOAT key field num [EX time] [EXAT time] [PX time] [PXAT time] [VER | ABS version] [MIN minval] [MAX maxval] [KEEPTTL]Increments the floating-point value of a field by num. Creates the key or field if they do not exist (field starts at 0 before the increment).

Expiration commands

CommandSyntaxDescription
EXHEXPIREEXHEXPIRE key field seconds [VER | ABS version]Sets a relative expiration time in seconds for a field.
EXHPEXPIREEXHPEXPIRE key field milliseconds [VER | ABS version]Sets a relative expiration time in milliseconds for a field.
EXHEXPIREATEXHEXPIREAT key field timestamp [VER | ABS version]Sets an absolute expiration time (UNIX timestamp in seconds) for a field.
EXHPEXPIREATEXHPEXPIREAT key field milliseconds-timestamp [VER | ABS version]Sets an absolute expiration time (UNIX timestamp in milliseconds) for a field.

Read commands

CommandSyntaxDescription
EXHGETEXHGET key fieldGets the value of a field. Returns nil if the key or field does not exist.
EXHMGETEXHMGET key field [field ...]Gets the values of multiple fields. Returns nil for fields that do not exist.
EXHGETWITHVEREXHGETWITHVER key fieldGets the value and version number of a field. Returns nil if the key or field does not exist.
EXHMGETWITHVEREXHMGETWITHVER key field [field ...]Gets the values and version numbers of multiple fields.
EXHTTLEXHTTL key fieldGets the remaining TTL of a field, in seconds.
EXHPTTLEXHPTTL key fieldGets the remaining TTL of a field, in milliseconds.
EXHVEREXHVER key fieldGets the current version number of a field.
EXHLENEXHLEN key [NOEXP]Gets the number of fields in the key. Specify NOEXP to count only non-expired fields.
EXHEXISTSEXHEXISTS key fieldChecks whether a field exists.
EXHSTRLENEXHSTRLEN key fieldGets the length of a field's value string.
EXHKEYSEXHKEYS keyGets all fields in the key.
EXHVALSEXHVALS keyGets all field values in the key.
EXHGETALLEXHGETALL keyGets all fields and values in the key.
EXHSCANEXHSCAN key op subkey [MATCH pattern] [COUNT count]Scans the key iteratively. Memory-optimized instances only.

Version commands

CommandSyntaxDescription
EXHSETVEREXHSETVER key field versionSets the version number of a field.

Delete commands

CommandSyntaxDescription
EXHDELEXHDEL key field [field ...]Deletes one or more fields from the key.
DELDEL key [key ...]Deletes one or more TairHash keys using the native Redis DEL command.

Syntax conventions

ConventionMeaning
UPPERCASE KEYWORDCommand keyword
_italic_Variable
[option]Optional parameter
A|BMutually exclusive — specify one only
...Parameter can be repeated

EXHSET

time complexity
SyntaxEXHSET key field value [EX time] [EXAT time] [PX time] [PXAT time] [NX | XX] [VER | ABS version] [KEEPTTL]
O(1)
DescriptionAdds a field to the specified TairHash key. Creates the key if it does not exist. Overwrites the field value if the field already exists.

Options

OptionDescription
keyThe TairHash key.
fieldThe field to set.
valueThe value of the field.
EX timeRelative expiration time in seconds. 0 means the field expires immediately. Omit to set no expiration.
EXAT timeAbsolute expiration time as a UNIX timestamp in seconds. 0 means the field expires immediately.
PX timeRelative expiration time in milliseconds. 0 means the field expires immediately.
PXAT timeAbsolute expiration time as a UNIX timestamp in milliseconds. 0 means the field expires immediately.
NXSets the field only if it does not already exist.
XXSets the field only if it already exists.
VER versionOptimistic concurrency control. If the field exists and its current version matches version, the operation proceeds and the version increments by 1. If versions differ, an error is returned. If the field does not exist or its version is 0, the check is skipped and the version becomes 1 after the operation.
ABS versionSets the version to an exact value regardless of the current version.
KEEPTTLPreserves the field's current expiration when you do not specify EX, EXAT, PX, or PXAT. Without KEEPTTL, EXHSET clears the existing expiration.
After you set a timeout for a field, running EXHSET again without a timeout option clears the expiration (unless you use KEEPTTL). To set an expiration on the key itself, use the native EXPIRE or EXPIREAT command.

Return values

ValueMeaning
1A new field was created.
0The field existed and its value was overwritten.
-1XX was specified but the field does not exist, or NX was specified but the field already exists.
"ERR update version is stale"VER was specified and the version does not match the current version.

Example

EXHSET myhash field1 val EX 10
(integer) 1

EXHGET

SyntaxEXHGET key field
Time complexityO(1)
DescriptionGets the value of a field. Returns nil if the key or field does not exist.

Return values

ValueMeaning
Field valueThe field exists.
nilThe key or field does not exist.

Example

EXHSET myhash field1 val
EXHGET myhash field1
"val"

EXHMSET

SyntaxEXHMSET key field value [field value ...]
Time complexityO(N), where N is the number of fields
DescriptionAdds multiple fields to the specified TairHash key. Creates the key if it does not exist. Overwrites existing field values.
To set expiration on individual fields after creation, use EXHEXPIRE, EXHPEXPIRE, EXHEXPIREAT, or EXHPEXPIREAT. To set expiration on the key itself, use the native EXPIRE or EXPIREAT command.

Return values

ValueMeaning
OKThe operation succeeded.

Example

EXHMSET myhash field1 val1 field2 val2
OK

EXHEXPIRE

SyntaxEXHEXPIRE key field seconds [VER | ABS version]
Time complexityO(1)
DescriptionSets a relative expiration time in seconds for a field.

Options

OptionDescription
keyThe TairHash key.
fieldThe field to set the expiration on.
secondsRelative expiration time in seconds.
VER versionSame optimistic concurrency behavior as in EXHSET.
ABS versionSets the version to an exact value regardless of the current version.

Return values

ValueMeaning
1The field exists and the expiration was set.
0The field does not exist.

Example

EXHSET myhash field1 val
EXHEXPIRE myhash field1 100
(integer) 1

EXHPEXPIRE

SyntaxEXHPEXPIRE key field milliseconds [VER | ABS version]
Time complexityO(1)
DescriptionSets a relative expiration time in milliseconds for a field.

Options

OptionDescription
keyThe TairHash key.
fieldThe field to set the expiration on.
millisecondsRelative expiration time in milliseconds.
VER versionSame optimistic concurrency behavior as in EXHSET.
ABS versionSets the version to an exact value regardless of the current version.

Return values

ValueMeaning
1The field exists and the expiration was set.
0The field does not exist.

Example

EXHSET myhash field1 val
EXHPEXPIRE myhash field1 1000
(integer) 1

EXHEXPIREAT

SyntaxEXHEXPIREAT key field timestamp [VER | ABS version]
Time complexityO(1)
DescriptionSets an absolute expiration time (UNIX timestamp in seconds) for a field.

Options

OptionDescription
keyThe TairHash key.
fieldThe field to set the expiration on.
timestampAbsolute UNIX timestamp in seconds.
VER versionSame optimistic concurrency behavior as in EXHSET.
ABS versionSets the version to an exact value regardless of the current version.

Return values

ValueMeaning
1The field exists and the expiration was set.
0The field does not exist.

Example

EXHEXPIREAT myhash field1 1293840000
(integer) 1

EXHPEXPIREAT

SyntaxEXHPEXPIREAT key field milliseconds-timestamp [VER | ABS version]
Time complexityO(1)
DescriptionSets an absolute expiration time (UNIX timestamp in milliseconds) for a field.

Options

OptionDescription
keyThe TairHash key.
fieldThe field to set the expiration on.
milliseconds-timestampAbsolute UNIX timestamp in milliseconds.
VER versionSame optimistic concurrency behavior as in EXHSET.
ABS versionSets the version to an exact value regardless of the current version.

Return values

ValueMeaning
1The field exists and the expiration was set.
0The field does not exist.

Example

EXHPEXPIREAT myhash field1 1293840000000
(integer) 1

EXHTTL

SyntaxEXHTTL key field
Time complexityO(1)
DescriptionGets the remaining TTL of a field, in seconds.

Return values

ValueMeaning
Positive integerRemaining TTL in seconds.
-1The field exists but has no expiration set.
-2The key does not exist.
-3The field does not exist.

Example

The following example shows all possible return states in a single sequence: a field with a TTL set (85), and a field with no expiration (-1).

EXHSET myhash field1 val1 EX 100
EXHSET myhash field2 val2
EXHTTL myhash field1
EXHTTL myhash field2
EXHTTL myhash field3
(integer) 85
(integer) -1
(integer) -3

EXHPTTL

SyntaxEXHPTTL key field
Time complexityO(1)
DescriptionGets the remaining TTL of a field, in milliseconds.

Return values

ValueMeaning
Positive integerRemaining TTL in milliseconds.
-1The field exists but has no expiration set.
-2The key does not exist.
-3The field does not exist.

Example

EXHSET myhash field1 val1 EX 100
EXHPTTL myhash field1
(integer) 97213

EXHVER

SyntaxEXHVER key field
Time complexityO(1)
DescriptionGets the current version number of a field.

Return values

ValueMeaning
Positive integerThe current version number.
-1The key does not exist.
-2The field does not exist.

Example

EXHVER myhash field1
(integer) 1

EXHSETVER

SyntaxEXHSETVER key field version
Time complexityO(1)
DescriptionSets the version number of a field to an exact value.

Return values

ValueMeaning
1The version was set.
0The key or field does not exist.

Example

EXHSETVER myhash field1 3
(integer) 1

EXHINCRBY

SyntaxEXHINCRBY key field num [EX time] [EXAT time] [PX time] [PXAT time] [VER | ABS version] [MIN minval] [MAX maxval] [KEEPTTL]
Time complexityO(1)
DescriptionIncrements the integer value of a field by num. Creates the key or field if they do not exist; a new field starts at 0 before the increment.

Options

OptionDescription
keyThe TairHash key.
fieldThe field to increment.
numInteger increment value.
EX, EXAT, PX, PXATSame as in EXHSET.
VER versionSame optimistic concurrency behavior as in EXHSET.
ABS versionSets the version to an exact value regardless of the current version.
MIN minvalMinimum allowed value. Returns an error if the result would be less than minval.
MAX maxvalMaximum allowed value. Returns an error if the result would exceed maxval.
KEEPTTLPreserves the field's current expiration. Same behavior as in EXHSET.
After you set a timeout for a field, running EXHINCRBY again without a timeout option clears the expiration (unless you use KEEPTTL).

Return values

ValueMeaning
IntegerThe new value after the increment.

Example

EXHMSET myhash field1 10
EXHINCRBY myhash field1 100
(integer) 110

EXHINCRBYFLOAT

SyntaxEXHINCRBYFLOAT key field num [EX time] [EXAT time] [PX time] [PXAT time] [VER | ABS version] [MIN minval] [MAX maxval] [KEEPTTL]
Time complexityO(1)
DescriptionIncrements the floating-point value of a field by num. Creates the key or field if they do not exist; a new field starts at 0 before the increment.

Options

OptionDescription
keyThe TairHash key.
fieldThe field to increment.
numFloating-point increment value.
EX, EXAT, PX, PXATSame as in EXHSET.
VER versionSame optimistic concurrency behavior as in EXHSET.
ABS versionSets the version to an exact value regardless of the current version.
MIN minvalMinimum allowed value. Returns an error if the result would be less than minval.
MAX maxvalMaximum allowed value. Returns an error if the result would exceed maxval.
KEEPTTLPreserves the field's current expiration. Same behavior as in EXHSET.
After you set a timeout for a field, running EXHINCRBYFLOAT again without a timeout option clears the expiration (unless you use KEEPTTL).

Return values

ValueMeaning
Floating-point stringThe new value after the increment.

Example

EXHMSET myhash field1 10
EXHINCRBYFLOAT myhash field1 9.235
"19.235"

EXHGETWITHVER

SyntaxEXHGETWITHVER key field
Time complexityO(1)
DescriptionGets the value and version number of a field. Returns nil if the key or field does not exist.

Return values

ValueMeaning
Array of [value, version]The field exists.
nilThe key or field does not exist.

Example

EXHGETWITHVER myhash field1
1) "19.235"
2) (integer) 5

EXHMGET

SyntaxEXHMGET key field [field ...]
Time complexityO(1)
DescriptionGets the values of multiple fields. Returns nil for fields that do not exist.

Return values

ValueMeaning
nilThe key does not exist.
Array of valuesOne element per requested field; nil for non-existent fields.

Example

EXHMSET myhash field1 10 field2 var1
EXHMGET myhash field1 field2
1) "10"
2) "var1"

EXHMGETWITHVER

SyntaxEXHMGETWITHVER key field [field ...]
Time complexityO(1)
DescriptionGets the values and version numbers of multiple fields. Returns nil for fields that do not exist.

Return values

ValueMeaning
nilThe key does not exist.
Array of [value, version] pairsOne element per requested field; nil for non-existent fields.

Example

EXHMSET myhash field1 10 field2 var1
EXHMGETWITHVER myhash field1 field2
1) 1) "10"
   2) (integer) 1
2) 1) "var1"
   2) (integer) 1

EXHLEN

SyntaxEXHLEN key [NOEXP]
Time complexityO(1) without NOEXP; O(N) with NOEXP
DescriptionGets the number of fields in the key. By default, includes fields that have expired but not yet been deleted. Use NOEXP to count only non-expired fields.

Options

OptionDescription
NOEXPTraverses the entire key and filters out expired fields before counting. Note that NOEXP filters fields from the count but does not evict them. Response time scales with key size.

Return values

ValueMeaning
IntegerNumber of fields (may include expired-but-not-deleted fields without NOEXP).
0The key does not exist.

Example

EXHLEN myhash
(integer) 2

EXHEXISTS

SyntaxEXHEXISTS key field
Time complexityO(1)
DescriptionChecks whether a field exists in the key.

Return values

ValueMeaning
1The field exists.
0The key or field does not exist.

Example

EXHEXISTS myhash field1
(integer) 1

EXHSTRLEN

SyntaxEXHSTRLEN key field
Time complexityO(1)
DescriptionGets the length of a field's value string.

Return values

ValueMeaning
Positive integerThe string length.
0The key or field does not exist.

Example

EXHSTRLEN myhash field1
(integer) 2

EXHKEYS

SyntaxEXHKEYS key
Time complexityO(N)
DescriptionGets all fields in the key.

Return values

ValueMeaning
Array of field namesThe key exists.
Empty arrayThe key does not exist.

Example

EXHMSET myhash field1 10 field2 var1
EXHKEYS myhash
1) "field1"
2) "field2"

EXHVALS

SyntaxEXHVALS key
Time complexityO(N)
DescriptionGets all field values in the key.

Return values

ValueMeaning
Array of valuesThe key exists.
Empty arrayThe key does not exist.

Example

EXHMSET myhash field1 10 field2 var1
EXHVALS myhash
1) "10"
2) "var1"

EXHGETALL

SyntaxEXHGETALL key
Time complexityO(N)
DescriptionGets all fields and their values in the key. The response alternates between field names and values.

Return values

ValueMeaning
Array of field-value pairsThe key exists.
Empty arrayThe key does not exist.

Example

EXHMSET myhash field1 10 field2 var1
EXHGETALL myhash
1) "field1"
2) "10"
3) "field2"
4) "var1"

EXHSCAN

SyntaxEXHSCAN key op subkey [MATCH pattern] [COUNT count]
Time complexityO(1) per call; O(N) for a full traversal
DescriptionScans the key iteratively and returns a cursor and a batch of field-value pairs. Memory-optimized instances only.

Options

OptionDescription
keyThe TairHash key.
opStarting position of the scan. See valid values below.
subkeyReference field used with op. Ignored when op is ^ or $.
MATCH patternFilters results by a regular expression pattern applied to field names.
COUNT countNumber of fields to scan per call. Defaults to 10. The actual number of results may differ depending on the key size and whether MATCH is applied.

Valid `op` values

ValueStarting position
>First field greater than subkey
>=First field greater than or equal to subkey
<First field less than subkey
<=First field less than or equal to subkey
==First field equal to subkey
^First field in the key
$Last field in the key

Return values

The response is an array of two elements:

  1. The starting field for the next scan call. Empty when the scan is complete.

  2. The scanned fields and their values, interleaved as [field, value, field, value, ...].

Returns an empty array if the key does not exist.

Example

EXHMSET myhashkey field1 val1 field2 val2 field3 val3 field4 val4 field5 val5
EXHSCAN myhashkey ^ xx COUNT 3
1) "field4"
2) 1) "field1"
   2) "val1"
   3) "field2"
   4) "val2"
   5) "field3"
   6) "val3"

The next scan starts from field4. Pass field4 as subkey with op >= to continue.

EXHDEL

SyntaxEXHDEL key field [field ...]
Time complexityO(1)
DescriptionDeletes one or more fields from the key.

Return values

ValueMeaning
1The field was deleted.
0The key or field does not exist.

Example

EXHDEL myhash field1
(integer) 1

FAQ

Why does exHash use more memory than a standard Redis hash for the same data?

Each field in TairHash stores additional metadata — the expiration timestamp and version number — that a standard Redis hash does not have. This extra per-field metadata is the source of the higher memory usage.