Skip to main content
0 reactions

How to Increment and Decrement a Counter in Valkey (INCR and DECR)

Featured image for How to Increment and Decrement a Counter in Valkey, showing a counter named visitors at 6 with INCR and DECR buttons and the steps plus 1, plus 1, minus 1, plus 10 and minus 5.
Valkey INCR and DECR.

INCR adds 1 to a number in Valkey® and DECR takes 1 away, in one step that never loses a count. This guide counts visitors.

Type the commands at the valkey-cli prompt. To open it, run valkey-cli in a terminal.

Note: Every command and output on this page is real, run on Valkey 9.2 on an Apple silicon Mac with macOS 26.

Syntax

SyntaxPlain text
INCR key
DECR key
INCRBY key increment
DECRBY key decrement
  • key: the counter. If it doesn’t exist, Valkey starts it at 0.
  • increment and decrement: the whole number to add or take away.

From the INCR (opens in a new tab), DECR (opens in a new tab), INCRBY (opens in a new tab) and DECRBY (opens in a new tab) documentation.

Step 1: Count Up with INCR

127.0.0.1:6379>Plain text
INCR visitors
OutputPlain text
(integer) 1

visitors didn’t exist, so Valkey started at 0 and added 1. Each INCR adds 1 more:

127.0.0.1:6379>Plain text
INCR visitors
OutputPlain text
(integer) 2

Step 2: Count Down with DECR

127.0.0.1:6379>Plain text
DECR visitors
OutputPlain text
(integer) 1

Step 3: Change by More Than One

127.0.0.1:6379>Plain text
INCRBY visitors 10
OutputPlain text
(integer) 11
127.0.0.1:6379>Plain text
DECRBY visitors 5
OutputPlain text
(integer) 6
127.0.0.1:6379>Plain text
GET visitors
OutputPlain text
"6"

Every command replies with the new value. The counter itself is a string, so GET shows "6".

Step 4: Counters Need Whole Numbers

127.0.0.1:6379>Plain text
SET name "Ana"
OutputPlain text
OK
127.0.0.1:6379>Plain text
INCR name
OutputPlain text
(error) ERR value is not an integer or out of range

A key that holds text can’t be counted. Valkey replies with this error and leaves the key unchanged.

valkey-cli session where INCR visitors replies 1 and 2, DECR replies 1, INCRBY 10 replies 11, DECRBY 5 replies 6, GET returns 6, and INCR on a text value returns an error.
Counting visitors up and down.

Clean Up

127.0.0.1:6379>Plain text
DEL visitors name
OutputPlain text
(integer) 2

Common Mistakes

MistakeWhat happensDo this instead
Reading the count, adding 1 in your app and saving it backTwo clients can save the same number, so a count is lostUse INCR, which counts in one step
Using INCR with a decimalValkey refuses itUse INCRBYFLOAT

FAQ

How high can a counter go?

To 9223372036854775807. The INCR documentation (opens in a new tab) says counters are 64-bit signed integers.

Can a counter go below zero?

Yes. DECR and DECRBY go negative. To stop at zero, use INCREX with LBOUND 0 (Valkey 9.2 and later).

Conclusion

INCR and DECR change a counter by 1, and INCRBY and DECRBY by any whole number, each in one atomic step.

Additional Resources

Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.

0 comments