Home > Back-end >  The 2020-10-29: use redis to realize distributed current-limiting components, high concurrency scena
The 2020-10-29: use redis to realize distributed current-limiting components, high concurrency scena

Time:10-30

The 2020-10-29: use redis to realize distributed current-limiting components, high concurrency scenario can only access the same IP a minute 100 times, more than limit abnormal returns, write implementation approach or pseudo code, # # f greatly architects a daily topic

CodePudding user response:

Using Redis INCR atoms on the properties, the key value of IP, is valid for 1 minute
Suppose to access, IP - 1 from the value of the cache out incr is less than 100 release, greater than 100 is wrong

CodePudding user response:

Use redis String for storage, IP as the Key, the value with the method of setting value, after each set out the return value is the increase of value, if the limits to 100, to the set a timeout for 1 minute, the Key will be automatically disappear in a minute

CodePudding user response:

 
False:
$isKey=EXISTS rate. Limiting: $IP
If $isKey is 1
$times=INCR rate. Limiting: $IP
If $times & gt; 100//100 visit will increase to 101
Throw//access frequency more than limit, throw an exception
The exit
The else
MULTI//here, if not add transaction, race conditions possible
INCR rate. Limiting: $IP
The EXPIRE $keyName, 60
The EXEC
  • Related