title: Check-and-increment a counter in Redis
body: <p>I have an operation I need to get done N times and no more. The operation is done by many possibly parallel  processes that recieve requests. A process has to check if the counter has exceeded N and, if not, then increment the counter and execute the operation. I suppose I could use a Redis counter for that.</p>
  
  <p>Howerver if I just <code>GET</code> and then <code>INCR</code> a value I might run into a race condition that will result in the operation being done more than N times. How do I perform some kind of <code>test-and-incr</code> operation against Redis?</p>
  
  <p>I know I could use <code>WATCH</code> but that's an optimistic lock. I expect there going to be very many collisions each second. This will result in a lot of failures. Maybe I could just wrap simple <code>GET</code> and <code>INCR</code> with some kind of external mutex. But I am not sure if it's good enough for performance.</p>