{"id":28276,"date":"2022-12-28T12:15:08","date_gmt":"2022-12-28T02:15:08","guid":{"rendered":"https:\/\/database.guide\/?p=28276"},"modified":"2022-12-28T12:15:08","modified_gmt":"2022-12-28T02:15:08","slug":"redis-zdiffstore-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-zdiffstore-command-explained\/","title":{"rendered":"Redis ZDIFFSTORE Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>ZDIFFSTORE<\/code> command computes the difference between the first and all successive input sorted sets and stores the result in the specified key.<\/p>\n\n\n\n<p><code>ZDIFFSTORE<\/code> works in the same way as the <code><a href=\"https:\/\/database.guide\/redis-zdiff-command-explained\/\" data-type=\"post\" data-id=\"28255\">ZDIFF<\/a><\/code> command, except that it stores the result instead of returning it (<code>ZDIFF<\/code> returns the result instead of storing it in a new key).<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE destination numkeys key &#91;key ...]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we create two sorted sets, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZADD cats 1 meow 2 fluffy 3 scratch<\/code><\/pre>\n\n\n\n<p>And:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZADD dogs 1 bark 2 woof 3 fluffy<\/code><\/pre>\n\n\n\n<p>We can use the <code>ZDIFFSTORE<\/code> command against those two sorted sets like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result1 2 cats dogs<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 2<\/pre>\n\n\n\n<p>That computed all the elements in the first set that aren&#8217;t in the second set and stored it in a key called <code>result1<\/code>. We got an integer reply of <code>2<\/code>, which means that two elements were added to the resulting sorted set.<\/p>\n\n\n\n<p>Let&#8217;s take a look at the new key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZRANGE result1 0 -1<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1) \"meow\"\n2) \"scratch\"<\/pre>\n\n\n\n<p>As expected, it only contains two elements. That&#8217;s because <code>fluffy<\/code> appears in both of the original sorted sets and <code>ZDIFFSTORE<\/code> only returns the difference between the first sorted set and all subsequent sorted sets.<\/p>\n\n\n\n<p>The <em><code>numkeys<\/code><\/em> value of <code>2<\/code> in the above examples means that I&#8217;m providing two sets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">More Sorted Sets<\/h2>\n\n\n\n<p>The <code>ZDIFFSTORE<\/code> command allows us to include as many sorted sets as we want in our list. They are all compared to the first one and any difference between the first and subsequent sets are returned.<\/p>\n\n\n\n<p>Let&#8217;s create another sorted set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZADD birds 1 tweet 2 scratch<\/code><\/pre>\n\n\n\n<p>Now let&#8217;s run <code>ZDIFFSTORE<\/code> against all three sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result2 3 cats dogs birds<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>Note that the <em><code>numkeys<\/code><\/em> argument is now <code>3<\/code>, which reflects the number of sorted sets I&#8217;m providing.<\/p>\n\n\n\n<p>This time only one element is added to the new sorted set. That&#8217;s because the other elements from the <code>cats<\/code> set contain values that are also present in at least one of the other sorted sets. That is, the <code>dogs<\/code> set contains <code>fluffy<\/code> and the <code>birds<\/code> set contains <code>scratch<\/code>, both of which are in the <code>cats<\/code> set.<\/p>\n\n\n\n<p>Let&#8217;s take a look at the new sorted set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZRANGE result2 0 -1<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1) \"meow\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Keys<\/h2>\n\n\n\n<p>Keys that don&#8217;t exist are treated as empty sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result3 2 oops1 oops2<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 0<\/pre>\n\n\n\n<p>Nothing was added to the new key. <\/p>\n\n\n\n<p>Let&#8217;s check the new key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZRANGE result3 0 -1<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(empty array)<\/pre>\n\n\n\n<p>The key contains an empty array because none of the keys that we used with <code>ZDIFFSTORE<\/code> existed.<\/p>\n\n\n\n<p>Here&#8217;s what happens when the first key exists, but none of the others do:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result4 3 cats oops1 oops2<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 3<\/pre>\n\n\n\n<p>And take a look at the key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZRANGE result4 0 -1<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1) \"meow\"\n2) \"fluffy\"\n3) \"scratch\"<\/pre>\n\n\n\n<p>All elements from the first set were added to the new key because none of those elements are present in any of the other (empty) sets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Wrong <code><em>numkeys<\/em><\/code> Argument<\/h2>\n\n\n\n<p>Here&#8217;s what happens when we use the wrong value for the <code><em>numkeys<\/em><\/code> argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result5 3 cats dogs<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) ERR syntax error<\/pre>\n\n\n\n<p>In this case I specified that there would be three sorted sets to compare, but I only provided two.<\/p>\n\n\n\n<p>The same error applies when it&#8217;s the other way around (i.e. I specify <code>2<\/code> but provide three keys):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result6 2 cats dogs birds<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) ERR syntax error<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Passing a Key of the Wrong Type<\/h2>\n\n\n\n<p>Here&#8217;s what happens when we pass a key that contains a different type (i.e. it&#8217;s not a sorted set):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result7 2 cats country<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) WRONGTYPE Operation against a key holding the wrong kind of value<\/pre>\n\n\n\n<p>In this case, the <code>country<\/code> key contains a string.<\/p>\n\n\n\n<p>However, it works fine on non-sorted sets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result8 2 countries cats<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 2<\/pre>\n\n\n\n<p>In this case the <code>countries<\/code> key contains a (non-sorted) set as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZRANGE result8 0 -1<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1) \"Australia\"\n2) \"New Zealand\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">When the Destination Key Already Exists<\/h2>\n\n\n\n<p>If the destination key already exists, it&#8217;s overwritten.<\/p>\n\n\n\n<p>Let&#8217;s overwrite the <code>result8<\/code> key from the previous example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZDIFFSTORE result8 2 cats dogs<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 2<\/pre>\n\n\n\n<p>Check the contents:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZRANGE result8 0 -1<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1) \"meow\"\n2) \"scratch\"<\/pre>\n\n\n\n<p>As expected, the <code>result8<\/code> key has been overwritten with our new results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the ZDIFFSTORE command computes the difference between the first and all successive input sorted sets and stores the result in the specified key. ZDIFFSTORE works in the same way as the ZDIFF command, except that it stores the result instead of returning it (ZDIFF returns the result instead of storing it in a &#8230; <a title=\"Redis ZDIFFSTORE Command Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/redis-zdiffstore-command-explained\/\" aria-label=\"Read more about Redis ZDIFFSTORE Command Explained\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,20],"class_list":["post-28276","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=28276"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28276\/revisions"}],"predecessor-version":[{"id":28284,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28276\/revisions\/28284"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=28276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=28276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=28276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}