Event counters
Track page views, jobs, logins, or other event totals.
Redis · Command
Increments the integer stored at a key by one. A missing key starts at zero, so the first call returns one.
INCR reads the string stored at key as a signed integer, adds one, stores the result back, and returns the new value. If the key is missing, Redis treats it as zero first. The operation is atomic and works in constant time, but fails when the existing value is not a valid integer string or exceeds the signed 64-bit range.
INCR key| Name | Type | Description |
|---|---|---|
key | key | The name of the key whose integer value is incremented. |
The new integer value after incrementing, returned as an integer reply; an error is returned for an invalid type or non-integer-representable string.
redis-cli INCR page_viewsAtomically increments the counter and prints its new integer value; a missing key starts at zero and therefore returns 1 on the first call.
Track page views, jobs, logins, or other event totals.
Assign sequential numbers to requests or generated items.
Maintain simple per-key quantities that change one unit at a time.
INCR atomically adds one to the integer represented by a string key and returns the resulting value. A missing key is treated as zero, so the first increment returns one.
Follow-up: What happens when the key contains a list? Redis returns a wrong-type error because INCR operates on string values, not list values.
The command has O(1) time complexity. It updates the value in one Redis command, making it suitable for counters that need atomic increments.
Follow-up: How would you increment by an arbitrary integer? Use INCRBY with the key and the desired increment.
INCR parses the stored string as a signed integer, increments it by one, stores the new integer as the key's value, and returns that integer. Values must fit within the signed 64-bit range.
Follow-up: How can a fractional counter be changed? Use INCRBYFLOAT, which is the related command for floating-point increments.
SETGETINCRBYDECRDECRBY