Performance-enhanced instances of ApsaraDB for Redis Enhanced Edition (Tair) provide the TairHash data structure. TairHash supports efficient and dynamic expiration policies, and these policies provide a quick method for you to release memory. However, the adoption of these policies increases the memory consumption of TairHash data. This topic describes the memory consumption and the two expiration policies of TairHash data. This helps you handle the tradeoff between data expiration efficiency and memory consumption, and reduce service costs.

Memory consumption comparison between native Redis hashes and TairHash data

If no expiration time is specified, the amount of memory consumed by native Redis hashes is similar to that consumed by TairHash data. The following tables provide testing details.

  • Test 1

    Test environment

    Item Test command Test condition
    TairHash EXHSET tairhashkey field value
    • The size of the field is 1 KB.
    • The size of the value is 1 KB.
    • The append only file (AOF) and Redis database backup (RDB) persistence policies are disabled.
    • The instance contains only one node. No replicas are available.
    Native Redis hashes HSET hashkey field value

    Test results

    Number of fields Memory consumed by TairHash data Memory consumed by native Redis hashes
    10000 29.79 MB 29.79 MB
    100000 297.02 MB 297.02 MB
    1000000 2.9 GB 2.9 GB
  • Test 2

    Test environment

    Item Test command Test condition
    TairHash EXHSET tairhashkey field value
    • The size of the field is 64 bytes.
    • The size of the value is 10 KB.
    • The AOF and RDB persistence policies are disabled.
    • The instance contains only one node. No replicas are available.
    Native Redis hashes HSET hashkey field value

    Test results

    Number of fields Memory consumed by TairHash data Memory consumed by native Redis hashes
    10000 104.17 MB 103.79 MB
    100000 1.02 GB 1.02 GB
    1000000 10.19 GB 10.19 GB
  • Test 3

    Test environment

    Item Test command Test condition
    TairHash EXHSET tairhashkey field value
    • The size of the field is 64 bytes.
    • The size of the value is 64 bytes.
    • The AOF and RDB persistence policies are disabled.
    • The instance contains only one node. No replicas are available.
    Native Redis hashes HSET hashkey field value

    Test results

    Number of fields Memory consumed by TairHash data Memory consumed by native Redis hashes
    10000 2.39 MB 2.02 MB
    100000 25.33 MB 19.31 MB
    1000000 253.29 MB 191.1 MB

The test results show that if you do not specify expiration policies, the amount of memory consumed by TairHash data is similar to or the same as that consumed by native Redis hashes. If you specify the expiration time for the fields of TairHash data, expiration policies affect the memory consumption of TairHash data.

Expiration policies supported by TairHash

Similar to native Redis hashes, TairHash supports two expiration policies: active expiration and passive expiration.

  • Active expiration
    The active expiration policy of TairHash differs from that of native Redis hashes.
    • The native Redis service runs periodic tasks to select a key at random from the keys where the expiration time is specified and to check whether the key expires. If the key expires, the key is deleted. If the key does not expire, the key is retained. This results in an inefficient process.
    • Performance-enhanced instances of ApsaraDB for Redis Enhanced Edition (Tair) also run periodic tasks to check the TairHash fields where the expiration time is specified. If a field expires, the field is deleted to release the consumed memory. In addition, performance-enhanced instances of ApsaraDB for Redis Enhanced Edition (Tair) use min heaps to sort the fields based on the expiration time of the fields. The system always selects the field that expires before the other fields expire. The active expiration policy of TairHash is more efficient than that of native Redis hashes, but requires additional memory. For more information, see Memory consumption caused by active expiration.
  • Passive expiration
    The passive expiration policy of TairHash is similar to that of native Redis hashes. If a client accesses an expired field of TairHash data, the passive expiration policy is triggered and the expired field is deleted to release the consumed memory. The following example describes the passive expiration process of TairHash data.
    1. A field of TairHash data expired a short of period ago, and is not deleted in an active way. At this time, a client runs the EXHGET command to retrieve the field.
    2. An ApsaraDB for Redis Enhanced Edition (Tair) instance determines whether the field expires.
    3. The field has expired. Therefore, the instance deletes the field to release the consumed memory, and returns a null value to the client.
    The passive expiration policy requires no additional memory. However, if an expired field is not accessed by clients, the memory consumed by the expired field cannot be released.

For more information about the rules that determine when the two expiration policies take effect, see Rules that determine when the expiration policies take effect. In actual business scenarios, we recommend that you combine active expiration and passive expiration policies. For more information, see Best practices.

Rules that determine when the expiration policies take effect

The expiration policies of TairHash data take effect based on the following rules:

  • By default, active expiration and passive expiration are enabled at the same time.
  • Passive expiration takes effect for all the fields where the expiration time is specified in TairHash data.
  • Active expiration takes effect for individual fields. If you run a command to specify the expiration time for a field of TairHash data, you can add the NOACTIVE option to disable active expiration for the field.
    Note For more information about TairHash commands, see TairHash commands.

If you do not add the NOACTIVE option when you specify the expiration time, active expiration takes effect and requires additional memory. For more information about the additional memory consumption that is caused by active expiration, see Memory consumption caused by active expiration.

Memory consumption caused by active expiration

Performance-enhanced instances of ApsaraDB for Redis Enhanced Edition (Tair) use min heaps to sort all the fields of TairHash data based on the expiration time of the fields. The instances also create indexes for the fields. After you specify the expiration time for a field of TairHash data, an ApsaraDB for Redis Enhanced Edition (Tair) instance creates a heap node and adds the heap node to a min heap. The heap node stores the field indexes that contain the key information. This allows you to find and delete the corresponding TairHash data and fields after the heap node expires. As a result, the memory that is consumed by the expired TairHash data and fields is released. However, the system cannot immediately release the memory consumed by the expired keys and fields after the command is run. This is because the heap node references the field indexes that contain the key information. Therefore, if you use active expiration, the following additional memory is required:

  • The memory consumed by the heap node
  • The memory consumed by the keys
  • The memory consumed by the fields where the expiration time is specified

The following tables about comparison testing offer an intuitive display of the memory consumption of TairHash data. You can view the memory consumption of TairHash data in different test scenarios: active expiration and passive expiration.

Compare the memory consumption of TairHash in different test scenarios: active expiration and passive expiration

  • Test 1

    Test environment

    Item Test command Test condition
    TairHash data where no expiration time is specified EXHSET tairhashkey field value
    • The size of the field is 1 KB.
    • The size of the value is 1 KB.
    • The AOF and RDB persistence policies are disabled.
    • The instance contains only one node. No replicas are available.
    TairHash data where the expiration time is specified EXHSET tairhashkey field value EX 1000

    Test results

    Number of fields Memory consumed by the TairHash data where no expiration time is specified Memory consumed by the TairHash data where the expiration time is specified
    10000 29.79 MB 46.03 MB
    100000 297.02 MB 460.36 MB
    1000000 2.9 GB 4.6 GB
  • Test 2

    Test environment

    Item Test command Test condition
    TairHash data where no expiration time is specified EXHSET tairhashkey field value
    • The size of the field is 64 bytes.
    • The size of the value is 64 bytes.
    • The AOF and RDB persistence policies are disabled.
    • The instance contains only one node. No replicas are available.
    TairHash data where the expiration time is specified EXHSET tairhashkey field value EX 1000

    Test results

    Number of fields Memory consumed by TairHash data Memory consumed by native Redis hashes
    10000 2.39 MB 4.38 MB
    100000 25.33 MB 45.16 MB
    1000000 253.29 MB 451.66 MB
  • Test 3

    Test environment

    Item Test command Test condition
    TairHash data where no expiration time is specified EXHSET tairhashkey field value
    • The size of the field is 64 bytes.
    • The size of the value is 64 bytes.
    • The AOF and RDB persistence policies are disabled.
    • The instance contains only one node. No replicas are available.
    • When a command is run to specify the expiration time, the NOACTIVE option is added to disable active expiration.
    TairHash data where the expiration time is specified EXHSET tairhashkey field value EX 1000 NOACTIVE

    Test results

    Number of fields Memory consumed by TairHash data Memory consumed by native Redis hashes
    10000 2.39 MB 2.39 MB
    100000 25.33 MB 25.33 MB
    1000000 253.29 MB 253.29 MB

Best practices

TairHash supports the NOACTIVE option for the following commands:

  • EXHSET
  • EXHEXPIRE
  • EXHEXPIREAT
  • EXHPEXPIRE
  • EXHPEXPIREAT
  • EXHINCRBY
  • EXHINCRBYFLOAT

The Rules that determine when the expiration policies take effect section describes the rules that determine when the expiration policies take effect. Based on these rules, if you run the preceding commands to specify the expiration time for a field of TairHash data, you can add the NOACTIVE option to disable active expiration. If you do not add this option, active expiration is enabled. If you add the NOACTIVE option, active expiration does not apply to the target field and no additional memory is required. For more information, see Additional memory caused by active expiration. However, if active expiration is disabled and an expired field is not accessed by clients, the system cannot release the memory consumed by the expired field.

Notice If you run the EXHGETALL, EXHKEYS, EXHVALS, or EXHSCAN command to access TairHash data, passive expiration is not triggered. As a result, the system does not delete the expired fields to release the memory that is consumed by these fields. However, the system filters out the expired fields from the outputs of these commands to provide a quick response for clients and reduce the risks of slow queries.
We recommend that you determine whether to use active expiration or on which data active expiration takes effect based on your business characteristics. The following examples help you determine whether to use and how to use active expiration.
  • If the data validity time is short and the data volume increases at a fast speed, we recommend that you use active expiration. By using active expiration, the system can delete the expired data at the earliest opportunity to release the memory that is consumed by the expired data. The released memory can be used to store new data.
  • If the data validity time is long and the data volume increases at a slow speed, we recommend that you do not use active expiration if possible. This reduces the total memory consumption.
  • If cold data is clearly distinguished from hot data, you can apply active expiration on cold data to improve the memory release efficiency. However, if you write hot data, we recommend that you add the NOACTIVE option to disable active expiration. This helps you to improve the expiration efficiency and reduce the memory consumption.