Atomic floating-point counters
Maintain a decimal-valued counter such as a score, balance, or measurement without client-side read-modify-write logic.
Redis · Module
Adds a floating-point number to the numeric string stored at a Redis key and returns the updated value as a string. Missing keys start at zero, and negative increments decrement the value.
INCRBYFLOAT parses the string stored at a key as a double-precision floating-point number, adds the supplied increment, stores the new value back at the same key, and returns that value as a string. A missing key starts at zero; a negative increment performs subtraction.
INCRBYFLOAT key increment| Name | Type | Description |
|---|---|---|
key | key | The name of the key. |
increment | floating-point number | The floating-point amount to add to the value. A negative value decrements the value instead. |
Bulk string reply containing the value of the key after the increment.
redis-cli SET mykey 10.50
redis-cli INCRBYFLOAT mykey 0.1
# "10.6"
redis-cli INCRBYFLOAT mykey -5
# "5.6"The command adds 0.1, then subtracts 5 by using a negative increment.
Maintain a decimal-valued counter such as a score, balance, or measurement without client-side read-modify-write logic.
Apply a reduction using a negative increment while retaining the same command and key representation.
Update a shared numeric value with one Redis command so competing clients do not need separate GET and SET steps.
It adds the floating-point increment to the value at key and returns the resulting value as a string. If the key is absent, Redis initializes it to zero first.
Follow-up: What does a negative increment do? It decrements the stored value, because the operation is ordinary addition with a negative operand.
The syntax is INCRBYFLOAT key increment. Both the existing value and increment must be parseable as double-precision floating-point numbers, and the key must hold a string.
Follow-up: What is the complexity? The command runs in O(1), as documented in the Redis command reference.
On success, Redis stores the updated number at the key and returns it as a bulk string. The returned formatting uses fixed precision of 17 digits after the decimal point.
Follow-up: How is it persisted and replicated? Redis propagates it as a SET operation to avoid inconsistencies from differing floating-point implementations.
INCRINCRBYDECRDECRBYHINCRBYFLOATSET