Database / REDIS
What is the purpose of the INCR command?
INCR atomically increments the integer value stored at a key by 1, returning the new value — and because it's atomic, it's safe to call concurrently from many clients without a race condition, unlike a naive "read the current value, add 1, write it back" sequence performed in application code.
SET pageviews:home 0 INCR pageviews:home # 1 INCRBY pageviews:home 5 # 6 DECR pageviews:home # 5 INCRBYFLOAT price 2.50 # for floating-point increments
This atomicity is the entire point: two clients calling INCR on the same key at the same moment are guaranteed to each get a distinct, correctly-incremented result, with no lost updates — a property that would require explicit locking to replicate safely if the increment were instead implemented as separate GET and SET calls. This makes INCR the standard building block for counters, rate limiters, and unique ID generation in Redis-backed applications.
More Related questions...
