{"id":25877,"date":"2022-06-15T11:33:30","date_gmt":"2022-06-15T01:33:30","guid":{"rendered":"https:\/\/database.guide\/?p=25877"},"modified":"2022-06-15T11:33:30","modified_gmt":"2022-06-15T01:33:30","slug":"redis-msetnx-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-msetnx-command-explained\/","title":{"rendered":"Redis MSETNX Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>MSETNX<\/code> command allows us to set multiple keys at once, but only if none of them exist. <\/p>\n\n\n\n<p>This allows us to set multiple keys representing different fields of a single logical object in a way that ensures that either all the fields are set, or none are set.<\/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>MSETNX key value &#91; key value ...]<\/code><\/pre>\n\n\n\n<p>Which basically means we can set multiple key\/value pairs at once.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s a simple example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MSETNX name \"Woof\" type \"Dog\" age \"10\"<\/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>In this case, the <code>MSET<\/code> operation was successful, and so we got an integer reply of <code>1<\/code>.<\/p>\n\n\n\n<p>We can now use the <code>MGET<\/code> command to return the values of each of those keys. Here&#8217;s an example of doing just that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MGET name type age<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1) \"Woof\"\n2) \"Dog\"\n3) \"10\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">When a Key Already Exists<\/h2>\n\n\n\n<p>As mentioned, if any of the keys already exist, nothing is set. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MSETNX name \"Big Woof\" age \"11\"<\/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>We get an integer reply of <code>0<\/code>, which means nothing was set.<\/p>\n\n\n\n<p>Now let&#8217;s get the values again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MGET name type age<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1) \"Woof\"\n2) \"Dog\"\n3) \"10\"<\/pre>\n\n\n\n<p>As expected, the values haven&#8217;t changed since the first example. <\/p>\n\n\n\n<p>If this is a problem for you, use <code>MSET<\/code> instead. The <code>MSET<\/code> command overwrites any existing values with the new value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the MSETNX command allows us to set multiple keys at once, but only if none of them exist. This allows us to set multiple keys representing different fields of a single logical object in a way that ensures that either all the fields are set, or none are set.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,84,20],"class_list":["post-25877","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-string","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25877","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=25877"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25877\/revisions"}],"predecessor-version":[{"id":25884,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25877\/revisions\/25884"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=25877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=25877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=25877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}