title: Will redis incr command can be limitation to specific number?
body: <p>In our project, we want to use redis's incr command to do limitation to the storage. For example, for the specific goods A, We want to sell only ten of them during the promotion. So we planned to use incr command to add the storage value from 0 to 10. In theory, this plan is no problem with this scenario.</p>
  
  <p>Before start, we did a performance test on the incr command, the result appeared that using incr to do limitation to the goods storage may be unrealiable. </p>
  
  <p>The testing code below：</p>
  
  <pre><code>static int count = 0;
  
  public void performanceToken() throws RedisAccessException {
  
      long result = redisUtil.incrRedisEx("myrrrxxxrrrediskey6");
  
      if (result &lt;= 10) {
          count ++;
      }
  
      Struts2Utils.renderJson(result +"|"+count);
      return;
  }
  </code></pre>
  
  <p>The incrRedisEx was the wrapper of incr command and we used jedis as the driver to drive our redis in our java project.</p>
  
  <p>Then We started our tomcat and we could invoke above interface directly from our browser. The result was good, no mistake, no error. </p>
  
  <p>And then we switched to jmeter, the performance testing tool, who could simulate great pressure from users to invoke the interface. we created 1500 requests to invoke the interface in 1 seconds. But the result is below:</p>
  
  <p>At the first, the code worked ok and we can see that the goods storage was limited to 10. 
  <a href="https://i.stack.imgur.com/V87FP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V87FP.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/cKpXF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cKpXF.png" alt="enter image description here"></a></p>
  
  <p>After a while, we can see that the count number broke the limitation, it growssss!!!
  <a href="https://i.stack.imgur.com/lLFBV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lLFBV.png" alt="enter image description here"></a></p>
  
  <p>Don't know why, redis incr can't do limitation to storage under great user requests. Anyone can give me some lights on it? </p>
  
  <p>Based on redis incr mechanism, it will return new value after doing incr command , if so, why it can't stop the growth of the number, it's a strange thing, really.</p>
  
  <p>For me, I think, maybe at the same time, there are two operations send to redis to do incr operation, but one does the incr command and  returns directly, another one does incr so slow and when it returns, it is found that the number it increases is not 2, but 11, because other requests do the incr command already and the good storage has been increased to 10, maybe? But this explaination is not correct because redis is single thread model. </p>
  
  <p>--------EDIT----------------</p>
  
  <p>I changed my code to avoid thread safe problem:</p>
  
  <pre><code> long result = redisUtil.incrRedisEx("axgggggggggg");
  
      if (result &lt;= 10) {
          logger.error("==generate==order==result==" + result);
      }
  
      Struts2Utils.renderJson(result );
      return;
  </code></pre>
  
  <p>Just found that the log will be write more than ten times.</p>