TairZset (exZset) enables you to sort items by scores with up to 256 dimensions of the double type. This feature is useful for leaderboards in industries such as gaming, live streaming, music, and e-commerce. It greatly improves data processing efficiency and simplifies client-side implementation because it does not require encoding or decoding.
Introduction to TairZset
The native Redis Sorted Set (also known as Zset) supports sorting by only a single double-precision score. This makes it difficult to implement multi-dimensional sorting. For example, using methods such as IEEE 754 bit-level concatenation is complex, reduces precision, and prevents the use of the EXZINCRBY command.
The TairZset data structure, developed by Alibaba Cloud, simplifies the implementation of multi-dimensional sorting. It has the following advantages over traditional methods:
Supports sorting by scores with up to 256 dimensions of the double type. The sorting priority is from left to right.
For a multi-dimensional score, the leftmost score has the highest priority. For example, with a three-dimensional score such as score1#score2#score3, TairZset first compares score1. If the score1 values are equal, TairZset then compares score2. If the score2 values are also equal, TairZset compares score3. If all score dimensions are identical, the elements are sorted by lexicographic order (ASCII order).
To make this easier to understand, you can think of the hash symbol (#) as a decimal point (.). For example, the relationship between 0#99, 99#90, and 99#99 can be understood as 0.99 < 99.90 < 99.99. Therefore, 0#99 < 99#90 < 99#99.
Supports the EXZINCRBY command. You no longer need to retrieve the current data, increment the value locally, and then write it back to Tair.
Provides an API similar to the native Zset API.
Provides capabilities for both standard leaderboards and distributed architecture leaderboards .
Provides an open-source TairJedis client. You do not need to perform any encoding or decoding. You can also use the open-source implementation as a reference to develop clients in other languages.
Typical scenarios
TairZset is suitable for leaderboard scenarios in industries such as gaming, live streaming, music, and e-commerce. Examples include the following:
Live streaming leaderboards: In a streamer competition, streamers are first ranked by their current popularity score. If their popularity scores are the same, they are ranked by the number of likes. If the number of likes is also the same, they are ranked by the total value of gifts received.
Medal tables: Participants are ranked based on the number of gold, silver, and bronze medals. They are first sorted by the number of gold medals. If the number of gold medals is the same, they are sorted by the number of silver medals. If the number of silver medals is also the same, they are sorted by the number of bronze medals.
Game leaderboards: Players are ranked based on multiple dimensions, such as score, task completion time, and rank level.
This module is open-source. For more information, see TairZset.
Best practices
Prerequisites
The instance must be a Tair memory-optimized instance. If the instance is a memory-optimized instance compatible with Redis 5.0, its minor version must be 1.7.1 or later.
The latest minor version provides more features and higher stability. We recommend that you update the instance to the latest minor version. For more information, see Update the minor version of an instance. If your instance is a cluster instance or read/write splitting instance, we recommend that you update the proxy nodes in the instance to the latest minor version to ensure that all commands can be run as expected.
Notes
The commands in this topic apply to TairZset data in a Tair instance.
Commands
Command | Syntax | Description |
| Stores one or more members with their scores in the TairZset at the specified key. Note To implement multi-dimensional sorting, use a hash symbol (#) to separate the scores for each dimension, such as | |
| Increments the score of a member in the TairZset at the specified key by the value of increment. | |
| Returns the score of a member in the TairZset at the specified key. If the key or member does not exist, it returns nil. | |
| Returns the specified range of elements from the TairZset at the specified key. | |
| Returns the specified range of elements from the TairZset at the specified key. Elements are ordered from high to low score. Elements with the same score are ordered in reverse lexicographic order. Note Other than the reverse sorting order, this command is similar to EXZRANGE. | |
| Returns all elements in the TairZset at the specified key with a score between min and max (inclusive). Elements are ordered from low to high score. Elements with the same score are ordered by lexicographic order. | |
| Returns all elements in the TairZset at the specified key with a score between min and max (inclusive). Unlike the default sorting order, this command returns elements ordered from high to low score. Elements with the same score are ordered in reverse lexicographic order. Note Other than the reverse sorting order, this command is similar to EXZRANGEBYSCORE. Note that max comes before min in this command. | |
| When all elements in a TairZset have the same score, this command returns all elements at the specified key that are between the min and max lexicographical range. | |
| When all elements in a TairZset have the same score, this command returns all elements at the specified key that are between the max and min lexicographical range. Note Other than the reverse sorting order, this command is similar to EXZRANGEBYLEX. Note that max comes before min in this command. | |
| Removes the specified members from the TairZset at the specified key. Non-existent members are ignored. | |
| Removes all elements in the TairZset at the specified key with a score between min and max (inclusive). | |
| Removes all elements in the TairZset at the specified key with a rank between start and stop. | |
| When all elements in a TairZset have the same score, this command removes all elements at the specified key that are between the max and min lexicographical range. Note If you run this command and the EXZRANGEBYLEX command with the same min and max parameter values, this command removes the same elements that the EXZRANGEBYLEX command returns. | |
| Returns the cardinality (number of elements) of the TairZset at the specified key. | |
| Returns the rank of a member in the TairZset at the specified key, with scores ordered from low to high. The rank (or index) is 0-based, so the member with the lowest score has a rank of 0. | |
| Returns the rank of a member in the TairZset at the specified key, with scores ordered from high to low. The rank (or index) is 0-based, so the member with the highest score has a rank of 0. Note Other than the reverse sorting order, this command is similar to EXZRANK. | |
| Returns the number of elements in the TairZset at the specified key with a score between min and max. | |
| When all elements in a TairZset have the same score, this command returns the number of elements at the specified key that are between the min and max lexicographical range. | |
| Calculates the rank of a given score in the TairZset at the specified key, with scores ordered from low to high. The rank (or index) is 0-based, so the member with the lowest score has a rank of 0. Note If the specified score does not exist, the command returns the prospective rank of that score in the TairZset. If the specified score already exists, Tair ranks the specified score before the existing score by default. | |
| Calculates the rank of a given score in the TairZset at the specified key, with scores ordered from high to low. The rank (or index) is 0-based, so the member with the highest score has a rank of 0. Note If the specified score does not exist, the command returns the prospective rank of that score in the TairZset. If the specified score already exists, Tair ranks the specified score after the existing score by default. | |
| Deletes one or more TairZset keys using the native Redis DEL command. |
The following list describes the conventions for the command syntax used in this topic:
Uppercase keyword: indicates the command keyword.Italic text: indicates variables.[options]: indicates that the enclosed parameters are optional. Parameters that are not enclosed by brackets must be specified.A|B: indicates that the parameters separated by the vertical bars (|) are mutually exclusive. Only one of the parameters can be specified....: indicates that the parameter preceding this symbol can be repeatedly specified.
EXZADD
Category | Description |
Syntax |
|
Time complexity | O(N) |
Command description | Stores one or more members with their scores in the TairZset at the specified key. The system performs different actions based on whether the key and member exist:
Note To implement multi-dimensional sorting, use a hash symbol (#) to separate the scores for each dimension, such as |
Options |
|
Return value | An integer that indicates the following:
|
Examples | Command example: Return example: |
EXZINCRBY
Category | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Increments the score of a member in the TairZset at the specified key by the value of increment. The system performs different actions based on whether the key and member exist:
Note
|
Options | None |
Return value | Returns the new score of the member as a string. If you use multi-dimensional scores, the format is |
Examples | Command example: Return example: |
EXZSCORE
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Returns the score of a member in the TairZset at the specified key. If the key or member does not exist, it returns nil. |
Options | None |
Return value | Returns the score of the member as a string. If you use multi-dimensional scores, the format is |
Examples | Command example: Return example: |
EXZRANGE
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements returned. |
Command description | Returns the specified range of elements from the TairZset at the specified key. |
Options |
|
Return value | Returns a list of elements in the specified range. If you use the WITHSCORES option, the return value includes the scores of the elements. |
Examples | Command example: Example response: |
EXZREVRANGE
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements returned. |
Command description | Returns the specified range of elements from the TairZset at the specified key. Elements are ordered from high to low score. Elements with the same score are ordered in reverse lexicographic order. Note Other than the reverse sorting order, this command is similar to EXZRANGE. |
Options |
|
Return value | Returns a list of elements in the specified range. If you use the WITHSCORES option, the return value includes the scores of the elements. |
Examples | Command example: Return example: |
EXZRANGEBYSCORE
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements returned. Note When M is a constant (for example, when you use the LIMIT option to always return the top 10 elements), the complexity can be considered O(log(N)). |
Command description | Returns all elements in the TairZset at the specified key with a score between min and max (inclusive). Elements are ordered from low to high score. Elements with the same score are ordered by lexicographic order. |
Options |
|
Return value | Returns a list of elements in the specified score range. If you use the WITHSCORES option, the return value includes the scores of the elements. |
Examples | Command example: Return example: |
EXZREVRANGEBYSCORE
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements returned. Note When M is a constant (for example, when you use the LIMIT option to always return the top 10 elements), the complexity can be considered O(log(N)). |
Command description | Returns all elements in the TairZset at the specified key with a score between min and max (inclusive). Unlike the default sorting order in TairZset, this command returns elements ordered from high to low score. Elements with the same score are ordered in reverse lexicographic order. Note Other than the reverse sorting order, this command is similar to EXZRANGEBYSCORE. Note that max comes before min in this command. |
Options |
|
Return value | Returns a list of elements in the specified score range. If you use the WITHSCORES option, the return value includes the scores of the elements. |
Examples | Command example: Return example: |
EXZRANGEBYLEX
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements returned. Note When M is a constant (for example, when you use the LIMIT option to return the top 10 elements), the complexity can be considered O(log(N)). |
Command description | When all elements in a TairZset have the same score, this command returns elements from the key that are between the min and max lexicographical range. This ensures that elements are sorted by lexicographic order. Note
|
Options |
|
Return value | Returns a list of elements whose names are within the specified range. |
Examples | Command example: Return example: |
EXZREVRANGEBYLEX
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements returned. Note When M is a constant (for example, when you use the LIMIT option to always return the top 10 elements), the complexity can be considered O(log(N)). |
Command description | When all elements in a TairZset have the same score, this command returns elements from the key that are between the max and min lexicographical range. Note Other than the reverse sorting order, this command is similar to EXZRANGEBYLEX. Note that max comes before min in this command. |
Options |
|
Return value | Returns a list of elements whose names are within the specified range. |
Examples | Command example: Return example: |
EXZREM
Category | Description |
Syntax |
|
Time complexity | O(M*log(N)), where N is the number of elements in the TairZset and M is the number of elements to be removed. |
Command description | Removes the specified members from the TairZset at the specified key. Non-existent members are ignored. Note If the specified key exists but its data structure is not a TairZset, an error is returned. |
Options | None |
Return value | Returns the number of members that were removed from the key, not including non-existent members. |
Examples | Command example: Return example: |
EXZREMRANGEBYSCORE
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements to be removed. |
Command description | Removes all elements in the TairZset at the specified key with a score between min and max (inclusive). |
Options | min and max represent the minimum and maximum scores. If the elements in the key use multi-dimensional scores, use a hash symbol (#) to separate the scores for each dimension. Note
|
Return value | Returns the number of elements removed. |
Examples | Command example: Return example: |
EXZREMRANGEBYRANK
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements removed by the operation. |
Command description | Removes all elements in the TairZset at the specified key with a rank between start and stop. |
Options | start and stop are 0-based indexes, where 0 represents the element with the lowest score. If an index is negative, it represents an offset from the element with the highest score. For example, -1 is the element with the highest score, and -2 is the element with the second-highest score. |
Return value | The number of elements removed. |
Examples | Command example: Return example: |
EXZREMRANGEBYLEX
Category | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N is the number of elements in the TairZset and M is the number of elements removed by the operation. |
Command description | When all elements in a TairZset have the same score, this command removes elements from the key that are between the max and min lexicographical range. This ensures that elements are sorted by lexicographic order. Note If you run this command and the EXZRANGEBYLEX command with the same min and max parameter values, this command removes the same elements that the EXZRANGEBYLEX command returns. |
Options | min and max represent the minimum and maximum member names as strings. You must specify the character range. For example:
|
Return value | The number of elements removed. |
Examples | Command example: Return example: |
EXZCARD
Category | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Returns the cardinality (number of elements) of the TairZset at the specified key. |
Options | None |
Return value | Returns the number of elements in the key. If the key does not exist, it returns 0. |
Examples | Command example: Return example: |
EXZRANK
Category | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Returns the rank of a member in the TairZset at the specified key, with scores ordered from low to high. The rank (or index) is 0-based, so the member with the lowest score has a rank of 0. |
Options | None |
Return value |
|
Examples | Command example: Return example: |
EXZREVRANK
Category | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Returns the rank of a member in the TairZset at the specified key. The results are ordered from high to low score. The rank (or index) is 0-based, so the member with the highest score has a rank of 0. Note Other than the reverse sorting order, this command is similar to EXZRANK. |
Options | None |
Return value |
|
Examples | Command example: Return example:(integer) 1 |
EXZCOUNT
Category | Description |
Syntax |
|
Time complexity | O(log(N)), where N is the number of elements in the TairZset. Note Because the query range is obtained based on element ranks, the amount of work involved in this operation is not proportional to the size of the query range. |
Command description | Returns the number of elements in the TairZset at the specified key with a score between min and max. |
Options | min and max represent the minimum and maximum scores. If the elements in the key use multi-dimensional scores, use a hash symbol (#) to separate the scores for each dimension. Note
|
Return value | Returns the number of elements (as an integer) within the specified score range. |
Examples | Command example: Return example: |
EXZLEXCOUNT
Category | Description |
Syntax |
|
Time complexity | O(log(N)), where N is the number of elements in the TairZset. Note Because the query range is obtained based on element ranks, the amount of work involved in this operation is not proportional to the size of the query range. |
Command description | When all elements in a TairZset have the same score, this command returns the number of elements at the specified key that are between the min and max lexicographical range. This ensures that elements are sorted by lexicographic order. Note
|
Options | min and max represent the minimum and maximum member names as strings. You must specify the character range. For example:
|
Return value | Returns the number of elements (as an integer) within the specified score range. |
Examples | Command example: Return example: |
EXZRANKBYSCORE
Category | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Calculates the rank of a given score in the TairZset at the specified key, with scores ordered from low to high. The rank (or index) is 0-based, so the member with the lowest score has a rank of 0. Note If the specified score does not exist, the command returns the prospective rank of that score in the TairZset. If the specified score already exists, Tair ranks the specified score before the existing score by default. |
Options | None |
Return value | Returns the rank of the specified score in the key. |
Examples | Command example: Return example: |
EXZREVRANKBYSCORE
Category | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Calculates the rank of a given score in the TairZset at the specified key, with scores ordered from high to low. The rank (or index) is 0-based, so the member with the highest score has a rank of 0. Note If the specified score does not exist, the command returns the prospective rank of that score in the TairZset. If the specified score already exists, Tair ranks the specified score after the existing score by default. |
Options | None |
Return value | Returns the rank of the specified score in the key. |
Examples | Command example: Return example: |