The TairZset (exZset) data structure allows you to sort data of the DOUBLE type based on 256 dimensions. It is ideal for implementing leaderboards for use in industries such as gaming, live streaming, music, and e-commerce. TairZset improves data processing efficiency and is easy to implement on the client side because you do not need to encode, decode, or encapsulate data.
Introduction to TairZset
The Sorted Set (also known as Zset) data structure of open source Redis allows you to sort DOUBLE-typed scores only by one dimension, which makes multi-dimensional sorting difficult. For example, implementing multi-dimensional sorting by concatenating data based on IEEE 754 has limitations such as complex implementation, reduced precision, and the inability to use the EXZINCRBY command.
To help you implement multidimensional sorting, Alibaba Cloud developed the TairZset data structure. Compared with the preceding method, TairZset provides the following advantages:
Allows DOUBLE-typed scores to be sorted based on a maximum of 256 dimensions. The scores are displayed from left to right based on their priorities.
In a multidimensional sorting, a left score has higher priority than a right score. Take the comparison of three-dimensional scores in the score1#score2#score3 format as an example. TairZset compares the score1s of multiple three-dimensional scores and moves on to score2s only when score1s are equal. If score1s are not equal, the ranking of score1s represents the ranking of the three-dimensional scores involved. By the same logic, score3s are compared only if score2s are equal. If all score1s are equal and the same holds true for score2s and score3s, the involved multidimensional scores are ranked in ASCII sort order.
For easier understanding, you can imagine number signs (#) as decimal points (.). This way, 0#99 < 99#90 < 99#99 can be seen as 0.99 < 99.90 < 99.99.
Supports the EXZINCRBY command. You no longer need to retrieve current data, increase the value, and then write the data back to Tair.
Supports APIs similar to those available for open source Redis ZSET.
Provides regular leaderboards and distributed leaderboards capabilities.
An open-source TairJedis client is provided that requires no encoding, decoding, or encapsulation. You can also refer to the open-source code to implement clients for other languages.
Typical scenarios
TairZset is ideal for sorting and ranking data for use in industries such as gaming, live streaming, music, and e-commerce. The following section lists some examples:
Leaderboards for the live streaming industry: In live matches, commentators are sorted by their current popularity value. If the popularity value is the same, they are sorted by the number of their likes. If the number of likes is also the same, they are sorted by the value of their gifts.
Leaderboards for medals: Participants are sorted by the number of their gold, silver, and bronze 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.
Leaderboards for the gaming industry: Players are sorted based on multiple factors, including their scores, task completion speeds, and achievement levels.
This module is open-source. For more information, see TairZset.
Best practices
Prerequisite
The instance is a Tair memory-optimized instance. If the instance is compatible with Redis 5.0, the 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.
Precautions
The operations apply to TairZset data in a Tair instance.
Command list
Command | Syntax | Description |
| Stores the specified score and element information in a TairZset key. You can specify multiple scores and elements. Note To implement multidimensional sorting, you can separate the scores of each dimension with number signs (#). Example: | |
| Increases the score of an element in a TairZset key. increment indicates the value that you want to add to the score. | |
| Returns the score of an element in a TairZset key. If the key or element does not exist, a value of nil is returned. | |
| Returns the elements of a TairZset key within the specified score range. | |
| Returns the elements of a TairZset key within the specified score range. Elements are stored by score in descending order, and elements with the same score are sorted in reverse lexicographical order. Note This command is similar to EXZRANGE except that this command sorts the results in reverse. | |
| Returns the elements of a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value. The returned elements are sorted by score in ascending order, and elements with the same score are returned in lexicographical order. | |
| Returns the elements of a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value. Contrary to the default sorting of TairZset elements, the elements returned by this command are sorted by score in descending order, and elements with the same score are sorted in reverse lexicographical order. Note This command is similar to EXZRANGEBYSCORE except that this command sorts the results in reverse and places max in front of min. | |
| To ensure elements are sorted in lexicographical order, when all elements in the Key have the same score, this command returns elements stored in the Key whose values are between min and max. | |
| Returns the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order. Note This command is similar to EXZRANGEBYLEX except that this command sorts the results in reverse and places max in front of min. | |
| Removes specified members from a TairZset key. If a specified member does not exist, the member is ignored. | |
| Removes the elements whose scores are greater than or equal to the min value and are less than or equal to the max value from a TairZset key. | |
| Removes the elements whose ranks are within the range of the start value and the stop value from a TairZset key. | |
| Removes the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order. Note If you use the same min and max parameter values for this command and the EXZRANGEBYLEX command, the elements removed by this command are the same as those returned by the EXZRANGEBYLEX command. | |
| Returns the cardinality of a TairZset key. Cardinality indicates the number of elements in a key. | |
| Returns the rank of an element in a TairZset key. Ranks are sorted by score in ascending order. Ranks use a zero-based numbering scheme, which indicates that the rank of the element that has the lowest score is 0. Rank is also known as index. | |
| Returns the rank of an element in a TairZset key. Ranks are sorted by score in descending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the highest score is 0. Rank is also known as index. Note This command is similar to EXZRANK except that this command sorts the results in reverse. | |
| Returns the number of elements in a TairZset key whose scores are between the min value and the max value. | |
| Returns the number of elements in a TairZset key whose scores are between the min value and the max value when all elements of the key have the same score. This ensures that elements are sorted in lexicographical order. | |
| Calculates the rank of the specified score in a TairZset key. Ranks are sorted by score in ascending order. Ranks use a zero-based numbering scheme, which indicates that the rank of the element that has the lowest score is 0. Rank is also known as index. Note If the score does not exist, the estimated rank of the score in the key is returned. By default, if the score already exists, Tair ranks the score in front of the existing score in the key. | |
| Calculates the rank of the specified score in a TairZset key. Ranks are sorted by score in descending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the highest score is 0. Rank is also known as index. Note If the score does not exist, the estimated rank of the score in the key is returned. If the score already exists, Tair ranks the score behind other existing scores in the key by default. | |
| Deletes one or more TairZset keys. |
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
Item | Description |
Syntax |
|
Time complexity | O(N) |
Command description | Adds the specified element with the specified score to a TairZset key. You can specify multiple scores and elements. The system uses different strategies based on whether the key and element exist: Note To implement multidimensional sorting, you can separate the scores of each dimension with number signs (#). Example:
|
Parameters |
|
Output | The return value is an integer. The following rules describe the return value:
|
Example | Sample command:
Sample output:
|
EXZINCRBY
Item | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Increases the score of an element in a TairZset key. increment indicates the value that you want to add to the score. The system uses different strategies based on whether the key and element exist:
Note
|
Options | None |
Output | Returns the new score of the member as a string. If multidimensional scores are used, the score format is |
Example | Sample command:
Sample output:
|
EXZSCORE
Item | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Returns the score of an element in a TairZset key. If the key or element does not exist, a value of nil is returned. |
Parameters | None |
Output | Returns the score of an element as a string. If multidimensional scores are used, the scores are returned in the |
Example | Sample command:
Sample output:
|
EXZRANGE
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements returned. |
Command description | Returns the elements of a TairZset key within the specified score range. |
Parameters |
|
Output | Returns a list of elements within the specified range. If the WITHSCORES option is used, the scores of the elements are included in the results. |
Example | Sample command:
Sample output:
|
EXZREVRANGE
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements returned. |
Command description | Returns the elements of a TairZset key within the specified score range. Elements are stored by score in descending order, and elements with the same score are sorted in reverse lexicographical order. Note This command is similar to EXZRANGE except that this command sorts the results in reverse. |
Parameters |
|
Output | Returns a list of elements within the specified range. If the WITHSCORES option is used, the scores of the elements are included in the results. |
Example | Sample command:
Sample output:
|
EXZRANGEBYSCORE
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned. Note When M is set to a constant, this expression can be regarded as O(log(N)). For example, you can specify that the first 10 elements are always returned by using the LIMIT offset count parameter. |
Command description | Returns the elements of a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value. The returned elements are sorted by score in ascending order, and elements with the same score are returned in lexicographical order. |
Parameters |
|
Output | Returns a list of elements within the specified score range. If the WITHSCORES option is used, the scores of the elements are included in the results. |
Sample command | Sample command:
Sample output:
|
EXZREVRANGEBYSCORE
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in TairZset, and M indicates the number of elements returned. Note When M is set to a constant, this expression can be regarded as O(log(N)). For example, you can specify that the first 10 elements are always returned by using the LIMIT parameter. |
Command description | Returns the elements of a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value. Contrary to the default sorting of TairZset elements, the elements returned by this command are sorted by score in descending order, and elements with the same score are sorted in reverse lexicographical order. Note This command is similar to EXZRANGEBYSCORE except that this command sorts the results in reverse and places max in front of min. |
Parameters |
|
Output | Returns a list of elements within the specified score range. If the WITHSCORES option is used, the scores of the elements are included in the results. |
Sample command | Sample command:
Sample response:
|
EXZRANGEBYLEX
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned. Note When M is set to a constant (for example, when you use the LIMIT parameter to specify that the first 10 elements are returned), this expression can be regarded as O(log(N)). |
Command description | To ensure elements are sorted in lexicographical order, when all elements in the Key have the same score, this command returns elements stored in the Key whose values are between min and max. Note
|
Parameters |
|
Output | A list of elements whose values are within the specified range. |
Sample command | Sample command:
Sample output:
|
EXZREVRANGEBYLEX
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned. Note When M is set to a constant, this expression can be regarded as O(log(N)). For example, you can specify that the first 10 elements are always returned by using the LIMIT offset count parameter. |
Command description | Returns the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order. Note This command is similar to EXZRANGEBYLEX except that this command sorts the results in reverse and places max in front of min. |
Parameters |
|
Output | A list of elements whose values are within the specified range. |
Sample command | Sample command:
Sample output:
|
EXZREM
Item | Description |
Syntax |
|
Time complexity | O(M*log(N)), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be removed. |
Command description | Removes specified members from a TairZset key. If a specified member does not exist, the member is ignored. Note If the key exists but its data structure is not TairZset, an error is returned. |
Options | None |
Output | The number of members removed from the key. The members that do not exist are not included. |
Sample command | Sample command:
Sample output:
|
EXZREMRANGEBYSCORE
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be removed. |
Command description | Removes the elements whose scores are greater than or equal to the min value and are less than or equal to the max value from a TairZset key. |
Parameters | min and max indicate the lowest score and the highest score, respectively. If multidimensional scores are used in the key's elements, scores of each dimension are separated by number signs (#). Note
|
Output | The number of elements removed. |
Sample command | Sample command:
Sample output:
|
EXZREMRANGEBYRANK
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be removed. |
Command description | Removes the elements whose ranks are within the range of the start value and the stop value from a TairZset key. |
Parameters | start and stop both indicate zero-based indexes, where 0 represents the element that has the lowest score. These indexes can be negative numbers. These numbers indicate offsets that start at the element that has the highest score. For example, -1 indicates the element that has the highest score, and -2 indicates the element that has the second highest score. Other elements follow the same rule. |
Output | The number of elements removed. |
Sample command | Sample command:
Sample output:
|
EXZREMRANGEBYLEX
Item | Description |
Syntax |
|
Time complexity | O(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be removed. |
Command description | Removes the elements of a TairZset key whose scores are between the max value and the min value when all elements of the key have the same score. This ensures that elements are sorted in lexicographical order. Note If you use the same min and max parameter values for this command and the EXZRANGEBYLEX command, the elements removed by this command are the same as those returned by the EXZRANGEBYLEX command. |
Parameters | min and max: indicate the minimum and maximum values of the element name (in string format). You need to specify the character intervals, for example:
|
Output | The number of elements removed. |
Sample command | Sample command:
Sample output:
|
EXZCARD
Item | Description |
Syntax |
|
Time complexity | O(1) |
Command description | Returns the cardinality of a TairZset key. Cardinality indicates the number of elements in a key. |
Parameters | None |
Output | The number of elements in a TairZset key. If the key does not exist, a value of 0 is returned. |
Sample command | Sample command:
Sample output:
|
EXZRANK
Item | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Returns the rank of an element in a TairZset key. Ranks are sorted by score in ascending order. Ranks use a zero-based numbering scheme, which indicates that the rank of the element that has the lowest score is 0. Rank is also known as index. |
Options | None |
Output |
|
Sample command | Sample command:
Sample output:
|
EXZREVRANK
Item | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Returns the rank of an element in a TairZset key. Ranks are sorted by score in descending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the highest score is 0. Rank is also known as index. Note This command is similar to EXZRANK except that this command sorts the results in reverse. |
Parameters | None |
Output |
|
Sample command | Sample command:
Sample output:
|
EXZCOUNT
Item | Description |
Syntax |
|
Time complexity | O(log(N)), where N indicates the number of elements in the TairZset key. Note This command uses element ranks to obtain the query range. Therefore, the workloads associated with this command are not proportional to the size of the range. |
Command description | Returns the number of elements in a TairZset key whose scores are between the min value and the max value. |
Parameters | min and max: indicate the lowest score and the highest score, respectively. If multidimensional scores are used in the key's elements, scores of each dimension are separated by number signs (#). Note
|
Output | The integer number of elements within the specified score range. |
Sample command | Sample command:
Sample output:
|
EXZLEXCOUNT
Item | Description |
Syntax |
|
Time complexity | O(log(N)), where N indicates the number of elements in the TairZset key. Note This command uses element ranks to obtain the query range. Therefore, the workloads associated with this command are not proportional to the size of the range. |
Command description | Returns the number of elements in a TairZset key whose scores are between the min value and the max value when all elements of the key have the same score to ensure that elements are sorted in lexicographical order. Note
|
Parameters | min and max: indicate the minimum and maximum values of the element name (in string format). You need to specify the character interval, for example:
|
Output | The integer number of elements within the specified score range. |
Sample command | Sample command:
Sample output:
|
EXZRANKBYSCORE
Item | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Calculates the rank of the specified score in a TairZset key. Ranks are sorted by score in ascending order. Ranks use a zero-based numbering scheme, which indicates that the rank of the element that has the lowest score is 0. Rank is also known as index. Note If the score does not exist, the estimated rank of the score in the key is returned. By default, if the score already exists, Tair ranks the score in front of the existing score in the key. |
Parameters | None |
Output | Returns the rank of the specified score in the key. |
Sample command | Sample command:
Sample response:
|
EXZREVRANKBYSCORE
Item | Description |
Syntax |
|
Time complexity | O(log(N)) |
Command description | Calculates the rank of the specified score in a TairZset key. Ranks are sorted by score in descending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the highest score is 0. Rank is also known as index. Note If the score does not exist, the estimated rank of the score in the key is returned. If the score already exists, Tair ranks the score behind other existing scores in the key by default. |
Parameters | None |
Output | Returns the rank of the specified score in the key. |
Sample command | Sample command:
Sample output:
|