<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rafael Sales on Medium]]></title>
        <description><![CDATA[Stories by Rafael Sales on Medium]]></description>
        <link>https://medium.com/@rafasales?source=rss-aaeb1f13169a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HtkjgZPiSOPwHXxF.jpeg</url>
            <title>Stories by Rafael Sales on Medium</title>
            <link>https://medium.com/@rafasales?source=rss-aaeb1f13169a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 23:10:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rafasales/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Custom atomic operations in Redis using LUA]]></title>
            <link>https://medium.com/@rafasales/custom-atomic-operations-in-redis-using-lua-a56e37e79299?source=rss-aaeb1f13169a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a56e37e79299</guid>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Rafael Sales]]></dc:creator>
            <pubDate>Tue, 28 Nov 2017 07:16:14 GMT</pubDate>
            <atom:updated>2017-11-28T07:20:58.359Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I’ll be using Ruby and </em><a href="https://github.com/redis/redis-rb"><em>Redis gem</em></a><em> in code examples.</em></p><p><a href="https://redis.io/">Redis</a> provide some handy atomic operations out of the box, such as <a href="https://redis.io/commands/incr">INCR</a> and <a href="https://redis.io/commands/rpoplpush">RPOPLPUSH</a>, but sometimes you need to execute a set of Redis commands atomically.</p><h4>MULTI/EXEC</h4><p>Up until Redis v2.5, there was only one way of executing a set of commands atomically: using <a href="https://redis.io/commands/multi">MULTI</a>/<a href="https://redis.io/commands/exec">EXEC</a>, which queues a set of commands and execute them as a single operation in Redis. There’s a caveat in this approach: no command on the queue can depend on results of previous commands.</p><p>Example: let&#39;s pretend Redis did not have an atomic INCR command out of the box. The code below is an attempt to implement an atomic increment operation.</p><pre>redis = Redis.new</pre><pre>redis.multi do<br>  current = redis.get(&#39;site_visits&#39;)<br>  if current<br>    redis.set(&#39;site_visits&#39;, current + 1)<br>  else<br>    redis.set(&#39;site_visits&#39;, 1)<br>  end<br>end</pre><p>Is the code above correct? Nope! The code above is not viable because commands inside a MULTI block are just queued up for later execution, so Redis doesn&#39;t even return values inside the block.</p><h4>EVAL</h4><p>Redis v2.6 introduced <a href="https://redis.io/commands/eval">EVAL</a>, which allows executing LUA scripts atomically. The great nicety of LUA scripts loaded in Redis is that commands can depend on results of previous commands, and the execution guarantees atomicity.</p><p>Let&#39;s rewrite the previous code to make it correct using EVAL:</p><pre>redis = Redis.new</pre><pre>script_id = redis.script(:load, &lt;&lt;-LUA)<br>  local counter_key = KEYS[1]<br>  local current = redis.call(&#39;GET&#39;, counter_key)<br>  if current<br>    redis.call(&#39;SET&#39;, counter_key, tonumber(current) + 1)<br>  else<br>    redis.call(&#39;SET&#39;, counter_key, 1)<br>  end<br>LUA</pre><pre>redis.evalsha(script_id, [&#39;site_visits&#39;])</pre><p>Done! The whole script above runs atomically, preventing the race condition that causes different threads from incrementing the counter based on a stale value.</p><p>The EVAL command in Redis is so important that libraries such as <a href="https://github.com/mperham/sidekiq/">Sidekiq</a> would be a lot more complex (or less reliable) if it didn&#39;t use custom LUA scripts to achieve atomicity in some critical operations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a56e37e79299" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>