You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Increments or decrements the numeric value stored at `key` by the specified amount, with optional upper/lower bounds and expiration control, in a single atomic operation.
125
115
If the key does not exist, it is set to `0` before performing the operation.
126
116
An error is returned if the key contains a value of the wrong type or a string that cannot be interpreted as a number.
127
117
128
-
Unlike [`INCR`]({{< relref "/commands/incr" >}}) and [`INCRBY`]({{< relref "/commands/incrby" >}}), `INCREX` returns an array of two elements: the new value of the key after the increment, and the increment that was actually applied. The `OVERFLOW` option controls what happens when the computed result would fall outside an explicit `LBOUND`/`UBOUND` or the type limits: the command can fail with an error (the default), saturate the result to the bound, or skip the operation.
118
+
Unlike [`INCR`]({{< relref "/commands/incr" >}}) and [`INCRBY`]({{< relref "/commands/incrby" >}}), `INCREX` returns an array of two elements: the new value of the key after the increment, and the increment that was actually applied. When the computed result would fall outside an explicit `LBOUND`/`UBOUND` or the type limits, the default is to skip the operation and reply with `[current_value, 0]`, leaving the key and its TTL untouched. The `SATURATE` flag changes this behavior so the result is capped at the bound instead.
129
119
130
120
## Required arguments
131
121
@@ -137,7 +127,7 @@ The name of the key to increment.
Sets a lower bound for the resulting value. If the computed result would fall below `lowerbound`, the behavior is determined by the `OVERFLOW` option. Defaults to`LLONG_MIN` in integer mode or `-LDBL_MAX` in `BYFLOAT` mode. `LBOUND` must be less than or equal to `UBOUND` when both are specified.
143
+
Sets a lower bound for the resulting value. If the computed result would fall below `lowerbound`, the operation is skipped and the reply is `[current_value, 0]` (or use the `SATURATE` flag to floor the result at `lowerbound` instead). When omitted, the bound is`LLONG_MIN` in integer mode or `-LDBL_MAX` in `BYFLOAT` mode. `LBOUND` must be less than or equal to `UBOUND` when both are specified.
Sets an upper bound for the resulting value. If the computed result would exceed `upperbound`, the behavior is determined by the `OVERFLOW` option. Defaults to`LLONG_MAX` in integer mode or `LDBL_MAX` in `BYFLOAT` mode. `UBOUND` must be greater than or equal to `LBOUND` when both are specified.
149
+
Sets an upper bound for the resulting value. If the computed result would exceed `upperbound`, the operation is skipped and the reply is `[current_value, 0]` (or use the `SATURATE` flag to cap the result at `upperbound` instead). When omitted, the bound is`LLONG_MAX` in integer mode or `LDBL_MAX` in `BYFLOAT` mode. `UBOUND` must be greater than or equal to `LBOUND` when both are specified.
160
150
161
151
</details>
162
152
163
-
<detailsopen><summary><code>OVERFLOW FAIL | SAT | REJECT</code></summary>
Sets the policy for handling out-of-bounds results. A bound violation includes both exceeding an explicit `LBOUND`/`UBOUND` and overflowing the type limits when no explicit bound is given.
155
+
When specified, an out-of-bounds result is capped at `UBOUND` or floored at `LBOUND` (or saturated to the type limits when no explicit bound is given). The second element of the reply reflects the saturated delta. An error is returned if the delta cannot be represented as a 64-bit signed integer in integer mode, or would produce Infinity in `BYFLOAT` mode. Any expiration option is still applied as specified.
166
156
167
-
*`FAIL` (default): if the computed result would violate a bound, the command returns an error and the key is left unchanged. This matches the existing semantics of [`INCRBY`]({{< relref "/commands/incrby" >}}) and [`INCRBYFLOAT`]({{< relref "/commands/incrbyfloat" >}}) on overflow.
168
-
*`SAT`: the result is capped at `UBOUND` or floored at `LBOUND` (or saturated to the type limits when no explicit bound is given). The second element of the reply reflects the saturated delta. An error is returned if the delta cannot be represented as a 64-bit signed integer in integer mode, or would produce Infinity in `BYFLOAT` mode. If the result is saturated, any expiration option is still applied as specified.
169
-
*`REJECT`: the operation is skipped. The key value and its TTL are left unchanged, no keyspace notification is fired, and nothing is replicated. The reply is `[current_value, 0]`, allowing the caller to detect the rejection without handling an error. Any expiration option is ignored on the rejected branch.
157
+
A bound violation includes both exceeding an explicit `LBOUND`/`UBOUND` and overflowing the type limits when no explicit bound is given.
Compare the three `OVERFLOW` policies when the result would exceed `UBOUND`. The default `FAIL` returns an error, `SAT` caps the result at the bound, and `REJECT` leaves the key untouched and reports a zero delta:
234
+
Compare the default out-of-bounds behavior with `SATURATE`when the result would exceed `UBOUND`. By default the key is left untouched and the reply reports a zero delta; with `SATURATE`the result is capped at the bound and the reply reflects the saturated delta:
247
235
248
236
{{% redis-cli %}}
249
237
SET mykey 99
250
238
INCREX mykey BYINT 5 UBOUND 100
251
239
SET mykey 99
252
-
INCREX mykey BYINT 5 UBOUND 100 OVERFLOW SAT
253
-
SET mykey 99
254
-
INCREX mykey BYINT 5 UBOUND 100 OVERFLOW REJECT
240
+
INCREX mykey BYINT 5 UBOUND 100 SATURATE
255
241
{{% /redis-cli %}}
256
242
257
243
## Pattern: window counter rate limiter
258
244
259
245
A common rate-limiting pattern requires atomically incrementing a counter and setting its expiration. With plain [`INCR`]({{< relref "/commands/incr" >}}) and [`EXPIRE`]({{< relref "/commands/expire" >}}), this typically requires a Lua script to be atomic.
260
246
261
-
`INCREX` requires a single native command. `UBOUND` enforces the rate cap, `OVERFLOW REJECT` skips the operation once the cap is reached, and `ENX` ensures that a new window with the correct duration is created if the previous one has expired; if a window already exists, it won't be extended. When the counter has already reached the cap, `actual_increment` is `0`, giving the caller immediate feedback without extra reads or error handling:
247
+
`INCREX` requires a single native command. `UBOUND` enforces the rate cap — by default, once the cap is reached the operation is skipped — and `ENX` ensures that a new window with the correct duration is created if the previous one has expired; if a window already exists, it won't be extended. When the counter has already reached the cap, `actual_increment` is `0`, giving the caller immediate feedback without extra reads or error handling:
262
248
263
249
```python
264
250
new_val, actual_incr = redis.execute_command(
265
251
"INCREX", f"ratelimit:{user_id}",
266
-
"BYINT", 1, "UBOUND", 100,"OVERFLOW", "REJECT",
252
+
"BYINT", 1, "UBOUND", 100,
267
253
"EX", 60, "ENX",
268
254
)
269
255
if actual_incr ==0:
@@ -284,17 +270,17 @@ if actual_incr == 0:
284
270
285
271
[Array reply]({{< relref "/develop/reference/protocol-spec#arrays" >}}): a two-element array:
286
272
287
-
1.**New value** — the value of the key after the increment, or the unchanged current value under `OVERFLOW REJECT`.
288
-
2.**Actual increment** — the increment that was actually applied. May differ from the requested increment when `OVERFLOW SAT` saturates the result to a bound, and is always `0` when `OVERFLOW REJECT` skipped the operation.
273
+
1.**New value** — the value of the key after the increment, or the unchanged current value when an out-of-bounds result caused the operation to be skipped.
274
+
2.**Actual increment** — the increment that was actually applied. May differ from the requested increment when `SATURATE` caps the result at a bound, and is always `0` when an out-of-bounds result caused the operation to be skipped.
289
275
290
276
Both elements are [Integer replies]({{< relref "/develop/reference/protocol-spec#integers" >}}) in integer mode (default or `BYINT`), or [Bulk string replies]({{< relref "/develop/reference/protocol-spec#bulk-strings" >}}) representing the float values in `BYFLOAT` mode.
291
277
292
278
-tab-sep-
293
279
294
280
[Array reply]({{< relref "/develop/reference/protocol-spec#arrays" >}}): a two-element array:
295
281
296
-
1.**New value** — the value of the key after the increment, or the unchanged current value under `OVERFLOW REJECT`.
297
-
2.**Actual increment** — the increment that was actually applied. May differ from the requested increment when `OVERFLOW SAT` saturates the result to a bound, and is always `0` when `OVERFLOW REJECT` skipped the operation.
282
+
1.**New value** — the value of the key after the increment, or the unchanged current value when an out-of-bounds result caused the operation to be skipped.
283
+
2.**Actual increment** — the increment that was actually applied. May differ from the requested increment when `SATURATE` caps the result at a bound, and is always `0` when an out-of-bounds result caused the operation to be skipped.
298
284
299
285
Both elements are [Integer replies]({{< relref "/develop/reference/protocol-spec#integers" >}}) in integer mode (default or `BYINT`), or [Double replies]({{< relref "/develop/reference/protocol-spec#doubles" >}}) in `BYFLOAT` mode.
0 commit comments