title: Using Redis transaction vs redlock to solve the Lost Update Problem
body: <p>I'm working with a Redis cluster having 2+ nodes. I'm trying to figure out which tool best fits for handling concurrency - transaction or locking. Transactions are <a href="https://redis.io/topics/transactions" rel="nofollow noreferrer">well documented</a>, but I didn't find a good best-practice-example on redlock. I also wonder why two tools exist and what's the use case for each.</p>
  <p>For simplicity, let's assume I want to do a concurrent increment and there is no INCR command in Redis.</p>
  <p>Option 1. <strong>Using Transactions</strong></p>
  <p>If I understand correctly, NodeJS pseudocode would look like this:</p>
  <p>transactIncrement = async (key) =&gt; {
  await redisClient.watch(key);
  let value = redisClient.get(key);
  value = value + 1;
  const multi = await redisClient.multi();
  try {
  await redisClient.set(key, value, multi);
  await redisClient.exec(multi);
  } catch (e) {
  // most probably error thrown because transaction failed
  // TODO: think if it's a good idea to restart in every case, introducing a potential infinite loop
  // whatever, restart
  await transactIncrement(key);
  }
  }</p>
  <p>Bad things I can see above are:</p>
  <ul>
  <li>try-catch block</li>
  <li>possibility to use transactions with multiple keys is limited on redis cluster</li>
  </ul>
  <p>Option 2. <strong>Redlock</strong></p>
  <p><strong>Is it true that trying to lock a resource that's already locked would not cause a failure immediately? So  that redlock tries N times before erroring?</strong></p>
  <p>If true then here's my pseudocode:</p>
  <pre><code>redlockIncrement = async (key) =&gt; {
    await redlock.lock(key, 1);  
    // below this line it's guaranteed that other &quot;threads&quot; are put on hold 
    // and cannot access the key, right? 
    let value = await redisClient.get(key);
    value = value + 1;
    await redisClient.set(key, value);
    await redlock.unlock(key);
  }
  </code></pre>
  <p><strong>Summary</strong></p>
  <p>If I got things right then redlock is definitely a more powerful technique. Please correct me if I'm wrong in the above assumptions. It would also be really great if someone provides an example of code solving similar problem because I couldn't find one.</p>