Skip to main content
0 reactions

How to Keep a Counter Within Limits in Valkey (INCREX)

Featured image for How to Keep a Counter Within Limits in Valkey, showing a volume of 10 on a gauge from LBOUND 0 to UBOUND 10, and the options BYINT 3 UBOUND 10 SATURATE.
Valkey INCREX.

INCREX changes a counter in Valkey® only while the result stays inside the limits you set, so a value never goes below or above them. It’s new in Valkey 9.2. This guide keeps a volume setting between 0 and 10.

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
INCREX key [NX | XX] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | PERSIST]
    [ENX] [BYINT integer | BYFLOAT float] [LBOUND lowerbound] [UBOUND upperbound] [SATURATE]
  • key: the counter. If it doesn’t exist, Valkey starts it at 0.
  • BYINT integer or BYFLOAT float: how much to add. A negative number subtracts. Without either, it adds 1.
  • LBOUND and UBOUND: the lowest and highest result allowed.
  • SATURATE: stop exactly at the limit instead of refusing the change.
  • NX or XX: change only if the key doesn’t exist, or only if it does. EX, PX, EXAT, PXAT, PERSIST and ENX set or remove an expiry in the same step.

The reply has two lines: the value after the command, and the change Valkey applied. From the INCREX documentation (opens in a new tab).

Prerequisites

  • Valkey 9.2 or later.

Step 1: Save a Starting Level

127.0.0.1:6379>Plain text
SET volume 8
OutputPlain text
OK

Step 2: Refuse a Change That Goes Too Far

127.0.0.1:6379>Plain text
INCREX volume BYINT 3 UBOUND 10
OutputPlain text
1) (integer) 8
2) (integer) 0

8 plus 3 would be 11, above the limit of 10, so Valkey refused: the value stays 8, and the change applied is 0.

Step 3: Stop at the Limit with SATURATE

127.0.0.1:6379>Plain text
INCREX volume BYINT 3 UBOUND 10 SATURATE
OutputPlain text
1) (integer) 10
2) (integer) 2

With SATURATE, Valkey stops at 10 and replies that it added only 2.

Step 4: Keep It Above Zero

127.0.0.1:6379>Plain text
INCREX volume BYINT -15 LBOUND 0 SATURATE
OutputPlain text
1) (integer) 0
2) (integer) -10

Taking 15 from 10 would give −5, so the value stops at 0, and the change applied is −10.

Step 5: Count Up by 1 Within Both Limits

127.0.0.1:6379>Plain text
INCREX volume LBOUND 0 UBOUND 10
OutputPlain text
1) (integer) 1
2) (integer) 1
valkey-cli session where INCREX refuses to take volume from 8 past 10, SATURATE stops it at 10, a decrease of 15 stops at 0, and INCREX with both limits adds 1.
INCREX keeps volume between 0 and 10.

Clean Up

127.0.0.1:6379>Plain text
DEL volume
OutputPlain text
(integer) 1

Common Mistakes

MistakeWhat happensDo this instead
Reading only the first line of the replyYou miss that the change was refusedCheck the second line: 0 means nothing changed
Leaving out SATURATE when you want to stop at the limitThe change is refused insteadAdd SATURATE

Troubleshooting

ProblemCauseFix
(error) ERR unknown command 'INCREX', with args beginning with: 'volume' 'BYINT' '3' 'UBOUND' '10'Your Valkey is older than 9.2Upgrade to Valkey 9.2 or later

FAQ

How is INCREX different from INCRBY?

INCRBY always adds and fails with an error if the number gets too big. INCREX checks the result against your limits first, and can set an expiry in the same step, as the documentation (opens in a new tab) explains.

What happens if the value is already outside the limits?

Valkey checks the result, not the current value, so a change that leaves it outside the limits is refused. With SATURATE, the value is set to the limit.

Conclusion

INCREX with LBOUND and UBOUND keeps a counter inside its limits: it refuses a change that goes too far, or stops at the limit with SATURATE.

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