Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis incrementing a numeric value - ERR value is not an integer or out of range

Redis incr function behaves erratically. When trying to increment any positive integer key that has not been set yet, it results in the following error. However, when first setting it by set then incrementing using incr for the same key, the problem is no longer there. Any reason why it behaves this way? Any workarounds?

127.0.0.1:6379[5]> incr 100
(error) ERR value is not an integer or out of range
127.0.0.1:6379[5]> incr '100'
(error) ERR value is not an integer or out of range
127.0.0.1:6379[5]> incr "100"
(error) ERR value is not an integer or out of range
127.0.0.1:6379[5]> set 100 1
OK
127.0.0.1:6379[5]> incr 100
(integer) 2

This behavior is only true when incrementing non-existing Integer keys:

127.0.0.1:6379> get "ahmedov"
(nil)
127.0.0.1:6379> incr "ahmedov"
(integer) 1
127.0.0.1:6379> incr "ahmedov"
(integer) 2
127.0.0.1:6379> get 12.1
(nil)
127.0.0.1:6379> incr 12.1
(integer) 1
127.0.0.1:6379> get -1
(nil)
127.0.0.1:6379> incr -1
(integer) 1
like image 975
Ahmedov Avatar asked Oct 24 '25 03:10

Ahmedov


1 Answers

Until you had actually called the SET command, you were trying to increment a key named '100' that did not contain a valid representation of an integer number.

After setting the key called '100' to the string "1", the increment succeeds and returns 2 (1+1) as expected.

like image 57
Itamar Haber Avatar answered Oct 27 '25 01:10

Itamar Haber