TExcelTAD
Home › Redis › Redis Numbers & Counters

Redis Numbers & Counters

Redis has no separate number type. A number is a string that looks like a number, and a family of commands treats such strings as integers or floating-point values. Because each command is atomic, Redis is a natural place for counters that many servers update at the same time.

INCR and DECR

INCR adds 1 to the integer stored at a key and returns the new value. If the key does not exist, Redis starts from 0. DECR subtracts 1.

redis · try it yourself
INCR visits
INCR visits
INCR visits
DECR visits
GET visits
TYPE visits
redis> INCR visits
(integer) 1
redis> INCR visits
(integer) 2
redis> INCR visits
(integer) 3
redis> DECR visits
(integer) 2
redis> GET visits
"2"
redis> TYPE visits
string

The value is still a string (TYPE says string), but internally Redis stores small integers in a compact integer encoding.

Why atomic counters matter

Imagine two web servers counting visits by reading, adding one and writing back:

text
server A: GET visits  -> 10
server B: GET visits  -> 10
server A: SET visits 11
server B: SET visits 11      # one visit is lost!

With INCR the read-add-write happens inside Redis as one step, so both increments count and the result is 12. Whenever you catch yourself writing "GET, change in the program, SET", check whether a single atomic command can do it instead.

INCRBY and DECRBY

To add or subtract more than one, give the amount. Negative amounts work too:

redis · try it yourself
SET stock:mug 50
DECRBY stock:mug 3
INCRBY stock:mug 20
INCRBY stock:mug -7
GET stock:mug
redis> SET stock:mug 50
OK
redis> DECRBY stock:mug 3
(integer) 47
redis> INCRBY stock:mug 20
(integer) 67
redis> INCRBY stock:mug -7
(integer) 60
redis> GET stock:mug
"60"

Floating-point numbers with INCRBYFLOAT

INCRBYFLOAT adds a decimal amount (positive or negative). There is no DECRBYFLOAT: pass a negative number instead.

redis · try it yourself
SET balance 10.50
INCRBYFLOAT balance 2.25
INCRBYFLOAT balance -0.75
INCRBYFLOAT balance 1e2
GET balance
INCRBYFLOAT ratio 0.1
INCRBYFLOAT ratio 0.2
redis> SET balance 10.50
OK
redis> INCRBYFLOAT balance 2.25
"12.75"
redis> INCRBYFLOAT balance -0.75
"12"
redis> INCRBYFLOAT balance 1e2
"112"
redis> GET balance
"112"
redis> INCRBYFLOAT ratio 0.1
"0.1"
redis> INCRBYFLOAT ratio 0.2
"0.3"

The reply is the new value as a string. Redis removes trailing zeros, so 12.75 - 0.75 is stored as 12, and exponents like 1e2 are accepted on input. The arithmetic uses extended (long double) precision and the result is printed with at most 17 significant digits, which is why 0.1 + 0.2 shows up as a clean 0.3 here, while in Python or JavaScript the same sum prints 0.30000000000000004. It is still binary floating point underneath, so rounding errors can appear after many operations.

Tip: For money, store integer cents with INCRBY instead of decimals with INCRBYFLOAT. Integer arithmetic is exact.

Limits and errors

Integer commands work on signed 64-bit integers, from −9223372036854775808 to 9223372036854775807. Going past the limit is an error, not a silent wrap-around. Values that are not numbers are rejected:

redis · try it yourself
SET big 9223372036854775806
INCR big
INCR big
SET name "Ada"
INCR name
SET price 9.99
INCR price
INCRBYFLOAT price 0.01
redis> SET big 9223372036854775806
OK
redis> INCR big
(integer) 9223372036854775807
redis> INCR big
(error) ERR increment or decrement would overflow
redis> SET name "Ada"
OK
redis> INCR name
(error) ERR value is not an integer or out of range
redis> SET price 9.99
OK
redis> INCR price
(error) ERR value is not an integer or out of range
redis> INCRBYFLOAT price 0.01
"10"

Note the last two lines: INCR refuses 9.99 because it is not an integer, while INCRBYFLOAT accepts it.

Counting with many keys

A popular pattern combines counters with time-bucketed key names: one counter per day, hour or minute. Reading a range of them is a single MGET:

redis · try it yourself
INCRBY views:2026-10-15 120
INCRBY views:2026-10-16 98
INCRBY views:2026-10-17 143
INCR views:2026-10-17
MGET views:2026-10-15 views:2026-10-16 views:2026-10-17
redis> INCRBY views:2026-10-15 120
(integer) 120
redis> INCRBY views:2026-10-16 98
(integer) 98
redis> INCRBY views:2026-10-17 143
(integer) 143
redis> INCR views:2026-10-17
(integer) 144
redis> MGET views:2026-10-15 views:2026-10-16 views:2026-10-17
1) "120"
2) "98"
3) "144"

In a real application each bucket would also get an expiry, so old counters clean themselves up. You will see that in the Expiration and Rate limiting lessons.

Counters inside other types

The same idea exists inside the collection types, each with its own command:

Type Command Example
String INCRBY INCRBY page:views 1
Hash field HINCRBY / HINCRBYFLOAT HINCRBY user:1 logins 1
Sorted-set member ZINCRBY ZINCRBY scores 10 ada

Test yourself

What does INCR hits return when hits does not exist yet?

  1. (nil)
  2. (integer) 0
  3. (integer) 1
  4. (error) no such key

How do you subtract 2.5 from a floating-point value stored at temp?

  1. DECRBYFLOAT temp 2.5
  2. INCRBYFLOAT temp -2.5
  3. DECRBY temp 2.5
  4. INCR temp -2.5

Why is INCR safer than GET + add + SET in application code?

  1. It is faster to type
  2. It runs atomically inside Redis, so concurrent updates are not lost
  3. It stores numbers on disk
  4. It locks the whole database for a second