INCR

Introduction and Use Case(s)

The INCR command in Redis is used to increment the integer value of a key by one. If the key does not exist, it is set to 0 before performing the operation. This command is useful in scenarios where you need to keep track of counts, such as page hits, number of user logins, or any other sequentially increasing metric.

Syntax

  1. INCR key

Parameter Explanations

  • key: The key whose value you want to increment. This should be a string representing an integer. If the key does not exist, it will be created with a value of 0 before being incremented.

Return Values

The INCR command returns the value of the key after the increment.

Example outputs:

  • If the initial value of mycounter is 10:

    1. dragonfly> INCR mycounter
    2. (integer) 11
  • If the key mycounter does not exist:

    1. dragonfly> INCR mycounter
    2. (integer) 1

Code Examples

  1. dragonfly> SET mycounter 10
  2. OK
  3. dragonfly> INCR mycounter
  4. (integer) 11
  5. dragonfly> INCR mycounter
  6. (integer) 12
  7. dragonfly> DEL mycounter
  8. (integer) 1
  9. dragonfly> INCR mycounter
  10. (integer) 1

Best Practices

  • Ensure that the key’s value is always an integer. Using INCR on a key containing non-integer values will result in an error.
  • Use INCR for thread-safe atomic increments. This avoids race conditions in distributed environments.

Common Mistakes

  • Using INCR on non-integer keys: If you try to increment a key that holds a string or another data type, an error will occur.

    1. dragonfly> SET mykey "hello"
    2. OK
    3. dragonfly> INCR mykey
    4. (error) ERR value is not an integer or out of range

FAQs

What happens if I use INCR on a non-existent key?

If the key does not exist, Redis treats it as if it were set to 0 and then performs the increment operation, resulting in a value of 1.

Can I decrement a key using the INCR command?

No, to decrement a key, you would use the DECR command.