Command
INCR keyExplanation
The INCR command is atomic — meaning it can safely be used in concurrent environments without race conditions. It’s useful for implementing counters, rate limiters, or metrics. If the key holds a non-integer value, Redis returns an error.
Common Use Cases
- •Counting page views or API requests
- •Implementing rate limiting
- •Tracking event occurrences
Best Practices
- ✓Use namespaced keys like `metrics:visits` for clarity
- ✓Use `INCRBY` for larger increments to reduce command count
- ✓Persist counters periodically if durability is required
Common Mistakes to Avoid
- ⚠Using INCR on keys with non-integer values
- ⚠Forgetting Redis stores all data as strings
Troubleshooting
Problem: ERR value is not an integer
Solution: Delete or reset the key if it contains non-numeric data (`DEL key`).
Problem: Counter resets unexpectedly
Solution: Check for overwriting with `SET` or `FLUSHALL` commands.
Examples
Increment 'counter' by 1
INCR counterTrack user login count
INCR user:100:logins