Redis speed limiter design (lua scripts and things are not used), redislua

Source: Internet
Author: User
Tags redis cluster

Redis speed limiter design (lua scripts and things are not used), redislua
The company has a need to send a fixed number of requests per minute to a server other than the LAN, a fixed number of requests, it means that all requests sent by all machines in the cluster are a fixed number, which requires a distributed speed limiter. The first thought was to use the incr method in redis and found an example in the official document of redis.

FUNCTION LIMIT_API_CALL(ip)ts = CURRENT_UNIX_TIME()keyname = ip+":"+tscurrent = GET(keyname)IF current != NULL AND current > 10 THEN    ERROR "too many requests per second"ENDIF current == NULL THEN    MULTI        INCR(keyname, 1)        EXPIRE(keyname, 1)    EXECELSE    INCR(keyname, 1)ENDPERFORM_API_CALL()
However, this script has two disadvantages for me: 1. My speed limiter does not limit the speed of a machine, but the speed of the whole cluster. So after judging the length of the key with the speed limit, there will be a lot of machines executing incr actions 2 company DBAs do not support using things
After reading the implementation of the speed limiter in the document, you can either use things or use lua scripts (the company does not support lua because lua is also transactional, and the company is a sharding redis cluster ), all are denied
It seems that we can only detect the program flow chart for a while. Suppose we can only request 20 requests within 1 s.



Step 1 use the incr command to obtain a value 2 to determine whether the value is 1. If the value is 1, it indicates that this acquisition is the first incr operation after the key value expires. In this operation, you need to set the key timeout time. However, transaction support is not used here. When the program runs to check that the value is 1 and the expire operation is not performed, the program can only request 20 times, therefore, after each value is obtained, perform the remainder operation with 10. If the value is a multiple of 10, the system checks the value. If the timeout time is not set, set 3 to determine whether the value is less than 20. If the value is less than or equal to 20, we believe that the thread has obtained the lock within the specified time. If the value is greater than 20, we think that the lock is not obtained, sleep continues to request the lock after a period of time.
In this way, we have implemented a speed limiter that does not use things and lua scripts.
If there are any errors or more elegant solutions, please give me more advice.
Note that the ttl key must be detected when the value of 1 in the flowchart is a multiple of 10. Here we can optimize the ttl key detection operation. If the value of 1 is a multiple of 10, the expire key is used directly.








How can I set a valid time for the redis value with lua?

No existing method exists

If you want to implement the function
In general, you can use insert or remove to increase or decrease the value of a table in the normal way.
Then, an additional listener is added to the table. When the specified time is reached, the related values are removed.

Easy-to-use scripts, not like lua

Most of the scripts in easy language are memory CALL... View, the buttons are basically invisible

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.