What are the INCR and DECR commands in Redis?
Answer
INCR key atomically increments the integer value of a key by 1 and returns the new value. If the key doesn't exist, it is initialized to 0 before the operation, so INCR on a non-existent key returns 1. DECR key decrements by 1. INCRBY key amount and DECRBY key amount allow incrementing/decrementing by arbitrary integers. INCRBYFLOAT works with decimal numbers. These commands are the foundation for many Redis patterns: page view counters (INCR page:home:views), rate limiting (INCR + EXPIRE on a per-minute key), inventory management (decrement stock atomically), and generating auto-incrementing IDs for distributed systems where a central counter is acceptable.
Previous
What does it mean that Redis operations are atomic?
Next
What are LPUSH, RPUSH, LPOP, and RPOP in Redis?