redis implementation counter introduction to the method of preventing brushing

  • 2020-05-30 21:16:07
  • OfStack

Recently, due to the coming of "double 11", the company needs to deal with 1 concurrency restriction on the interface request, or make a security interception to prevent brushing:
For example: 1 interface request, limit the total number of requests per second to 200 times, more than 200 times to wait, wait for 1 second, request again, here use 1 redis as a counter mode to achieve.

Call the redis method:

INCR key

Increment the numeric value stored in key by 1.

If key does not exist, the value of key is initialized to 0 before the INCR operation is performed.

If the value contains the wrong type, or a value of string type cannot be represented as a number, then 1 error is returned.

This is an operation on a string, because Redis does not have a dedicated integer type, so the string stored in key is interpreted as a decimal 64-bit signed integer to perform the INCR operation.


redis> SET test 20
redis> INCR test
(integer) 21
redis> GET test #  A numeric value in  Redis  Is saved as a string 

Implementation of counter

The counter is the most intuitive mode for the atomically self-incrementing operation of Redis, and the idea is fairly simple: every time an operation occurs, send an INCR command to Redis.

For example, in an web application, if you want to know how many times a user has clicked on a page every day for a year, you can just use ID and the relevant date as the key, and do the auto-increment once every time the user clicks on the page.

For example, if the user name is peter and the click time is March 22, 2012, then execute the command INCR peter::2012.3.22.

$redisKey =  " api_name_ "  + $api;
$count = $this->redis->incr($redisKey);
if ($count == 1) {
// Set expiration date 1s
$this->redis->expire($redisKey,1);// Set up the 1s Expiration time 
if (count > 200) {// Security interception to prevent brushing 
return false;// Over and back false
// Subsequent processing 

This is a simple implementation of the redis counter application, in addition to the following methods:

This simple pattern can be extended in several ways:

By combining INCR and EXPIRE, you can count only for the specified lifetime (counting).
The client can atomically get the current value of the counter and reset it by using the GETSET command, refer to the GETSET command for more information.
With other auto-increment/auto-decrement operations, such as DECR and INCRBY, the user can increase or decrease the value of the counter by performing different operations, such as those that may be used in the game's scorecard.


This article is about the implementation of redis counter - to prevent brushing method of all the content, I hope to help you. Interested friends can continue to see this site: Redis master and slave synchronous analysis, Java programming redisson implementation of distributed lock code examples, a brief description of the difference between Redis and MySQL, if you have any questions can leave a message, this site will promptly reply to you. Thank you for your support!

Related articles: