{"id":25970,"date":"2022-06-27T12:02:00","date_gmt":"2022-06-27T02:02:00","guid":{"rendered":"https:\/\/database.guide\/?p=25970"},"modified":"2022-06-27T12:02:00","modified_gmt":"2022-06-27T02:02:00","slug":"redis-incrby-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-incrby-command-explained\/","title":{"rendered":"Redis INCRBY Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>INCRBY<\/code> command increments the value of a key by the specified amount.<\/p>\n\n\n\n<p>If the key doesn&#8217;t exist, <code>INCRBY<\/code> creates the key with a value of <code>0<\/code> and then increments it by the specified amount.<\/p>\n\n\n\n<p>An error occurs if the key contains a value of the wrong type or contains a string that cannot be represented as integer.&nbsp;<code>INCRBY<\/code> operations are limited to 64 bit signed integers.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBY key increment<\/code><\/pre>\n\n\n\n<p>Where <code><em>key<\/em><\/code> is the key to increment, and <em><code>increment<\/code><\/em> is the amount to increment it by.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we set a key like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET score \"10\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>Let&#8217;s quickly take a look at it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET score<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"10\"<\/pre>\n\n\n\n<p>OK, now let&#8217;s increment it by seven:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBY score 7<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 17<\/pre>\n\n\n\n<p>We get an integer reply with the value of the key after the <code>INCRBY<\/code> operation was performed.<\/p>\n\n\n\n<p>Let&#8217;s run the <code><a href=\"https:\/\/database.guide\/redis-get-command-explained\/\" data-type=\"post\" data-id=\"25826\">GET<\/a><\/code> command again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET score<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"17\"<\/pre>\n\n\n\n<p>As expected, the original value of 10 has been incremented by 7.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Negative Values<\/h2>\n\n\n\n<p>Redis does not have a dedicated integer type. However, the string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation. Therefore, we can increment negative values just as we can increment positive values.<\/p>\n\n\n\n<p>Set a negative value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET score \"-10\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>Let&#8217;s quickly take a look at it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET score<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"-10\"<\/pre>\n\n\n\n<p>OK, now let&#8217;s increment it by seven:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBY score 7<\/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>The value has been incremented by seven.<\/p>\n\n\n\n<p>We can check this at any time by running the <code>GET<\/code> command again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET score<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"-3\"<\/pre>\n\n\n\n<p>We can also increment it by a negative amount, which has the effect of decrementing the value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBY score -7<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) -10<\/pre>\n\n\n\n<p>We can also use the <code>DECR<\/code> and <code>DECRBY<\/code> commands to decrement values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Key Doesn&#8217;t Exist<\/h2>\n\n\n\n<p>If the key doesn&#8217;t exist, <code>INCRBY<\/code> creates the key with a value of <code>0<\/code> and then increments it by the specified amount.<\/p>\n\n\n\n<p>Let&#8217;s delete our <code>score<\/code> key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEL score<\/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>The <code>DEL<\/code> command returns the number of keys that were deleted &#8211; in this case 1.<\/p>\n\n\n\n<p>Now let&#8217;s try to increment that (non-existent) key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBY score 10<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 10<\/pre>\n\n\n\n<p>So we can see that <code>score<\/code> has been recreated with a value of 10.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Value is the Wrong Type<\/h2>\n\n\n\n<p>An error occurs if the key contains a value of the wrong type or contains a string that cannot be represented as an integer.<\/p>\n\n\n\n<p>To test this, let&#8217;s try incrementing a value that can&#8217;t be represented as an integer.<\/p>\n\n\n\n<p>Set the amount:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET score \"Ten\"<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>Let&#8217;s quickly take a look at it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET score<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Ten\"<\/pre>\n\n\n\n<p>OK, now let&#8217;s try to increment it by seven:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBY score 7<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) ERR value is not an integer or out of range<\/pre>\n\n\n\n<p>An error occurred as expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Increment by One<\/h2>\n\n\n\n<p>Although <code>INCRBY<\/code> can be used to increment a key by one, there&#8217;s another command called <code>INCR<\/code> that is designed to do exactly that. When using <code>INCR<\/code>, you don&#8217;t need to specify the increment amount &#8211; you only need to specify the key.<\/p>\n\n\n\n<p>So the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCR score<\/code><\/pre>\n\n\n\n<p>Is the equivalent of this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCRBY score 1<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the INCRBY command increments the value of a key by the specified amount. If the key doesn&#8217;t exist, INCRBY creates the key with a value of 0 and then increments it by the specified amount. An error occurs if the key contains a value of the wrong type or contains a string that &#8230; <a title=\"Redis INCRBY Command Explained\" class=\"read-more\" href=\"https:\/\/database.guide\/redis-incrby-command-explained\/\" aria-label=\"Read more about Redis INCRBY Command Explained\">Read more<\/a><\/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-25970","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\/25970","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=25970"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25970\/revisions"}],"predecessor-version":[{"id":26273,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/25970\/revisions\/26273"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=25970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=25970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=25970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}