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
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 integerorBYFLOAT float: how much to add. A negative number subtracts. Without either, it adds 1.LBOUNDandUBOUND: the lowest and highest result allowed.SATURATE: stop exactly at the limit instead of refusing the change.NXorXX: change only if the key doesn’t exist, or only if it does.EX,PX,EXAT,PXAT,PERSISTandENXset 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
SET volume 8
OK
Step 2: Refuse a Change That Goes Too Far
INCREX volume BYINT 3 UBOUND 10
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
INCREX volume BYINT 3 UBOUND 10 SATURATE
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
INCREX volume BYINT -15 LBOUND 0 SATURATE
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
INCREX volume LBOUND 0 UBOUND 10
1) (integer) 1
2) (integer) 1

Clean Up
DEL volume
(integer) 1
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Reading only the first line of the reply | You miss that the change was refused | Check the second line: 0 means nothing changed |
Leaving out SATURATE when you want to stop at the limit | The change is refused instead | Add SATURATE |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
(error) ERR unknown command 'INCREX', with args beginning with: 'volume' 'BYINT' '3' 'UBOUND' '10' | Your Valkey is older than 9.2 | Upgrade 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.
Related
- Valkey Tutorial — Part 4: Strings and Counters
- Valkey Tutorial — Part 2: Your First Valkey Commands with valkey-cli
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