In flash sale scenarios where the purchase time or the quantity is limited, you have to handle traffic peaks that occur before and after the flash sales. You also have to prevent the accepted purchase orders from exceeding the upper limit. To handle these challenges, performance-enhanced instances of ApsaraDB for Redis Enhanced Edition (Tair) offer the TairString data structure that provides a simple and efficient rate limiter. You can use the rate limiter to prevent the accepted purchase orders from exceeding the upper limit. The solutions described in this topic are also applicable to other scenarios where rate or traffic limiting and throttling are required.

Rate limiter for flash sales

Based on the integration with Alibaba Tair, performance-enhanced instances of ApsaraDB for Redis Enhanced Edition (Tair) provides the TairString data structure. TairString is more powerful than native Redis strings. TairString offers all the features of native Redis strings except bit operations.

The EXINCRBY and EXINCRBYFLOAT commands of TairString have similar functions to the INCRBY and INCRBYFLOAT commands of native Redis strings. You can use these commands to increment or decrement values. The EXINCRBY and EXINCRBYFLOAT commands support more options than the two commands of the native Redis strings. These options include EX, NX, VER, MIN, and MAX. For more information, see TairString commands. The solution described in this topic uses MIN and MAX options.

Option Description
MIN Specifies the minimum TairString value.
MAX Specifies the maximum TairString value.

If you use native Redis strings to handle the challenges of flash sales, you have to implement complex code logic and handle management difficulties. This may easily cause excess purchase orders in flash sales. To be more specific, users receive prompts of successful purchases after all the items are sold out in flash sales, which causes negative effects. TairString allows you to compile and run simple code to limit the exact number of purchase orders. The pseudocode is described as follows:

if(EXINCRBY(key_iphone, -1, MIN:0) == "would overflow")

Counters for rate limiting

Similar to Rate limiter for flash sales, you can specify the MAX option of the EXINCRBY command to implement counters for rate limiting. The pseudocode is described as follows:

if(EXINCRBY(rate_limitor, 1, MAX:1000) == "would overflow")

Counters for rate limiting can be applied to various scenarios. For example, you can use the counters to limit the number of concurrent requests, the access frequency, and the number of password changes. For example, in concurrency limiting scenarios, the number of concurrent requests unexpectedly exceeds the system performance threshold. To prevent service crashes and serious issues, you can use a rate limiter to control the number of concurrent requests. This offers an appropriate temporary solution. This solution ensures that the system responds to the concurrent requests in a timely manner if the number of the concurrent requests does not exceed the performance threshold. The EXINCRBY command of TairString allows you to compile simple code to set a rate limiter for concurrent requests. The sample code is described as follows:

public boolean tryAcquire(Jedis jedis,String rateLimitor,int limiter){
    try {
        jedis.getClient().sendCommand(TairCommand.EXINCRBY,rateLimitor,"1","EX","1","MAX",String.valueOf(limiter));    // Set a rate limiter.
        return true;
    }catch (Exception e){
        if(e.getMessage().contains("increment or decrement would overflow")){    // Check whether the result contains error messages.
            return false;
        throw e;