Old fart
Old fart

Reputation: 600

What happens when Int64 maxvalue is exceded with Redis INCR

Simple enough, I am using Redis INCR to ensure atomic increments of a counter, the counter has an indeterminate start value less than Int64.MaxValue.

Does Redis reset the value when it reaches Int64.MaxValue or throw an error?

I read the documentation but it does not say what happens, and I really do want to maintain the atomic nature at rollover

Upvotes: 13

Views: 7610

Answers (1)

Karthikeyan Gopall
Karthikeyan Gopall

Reputation: 5689

It will throw an error. I did a small experiment for your use case

127.0.0.1:6379> set value 9223372036854775807 (2 power 63 -1)
OK
127.0.0.1:6379> incr value
(error) ERR increment or decrement would overflow
127.0.0.1:6379> 

Redis can hold upto 2 power 63. and throws error if it exceeds that limit. It might be either "out of range" error or "overflow" error

Upon error you can catch that exception and reset the value in your application logic.

Upvotes: 29

Related Questions