How do redis increment commands react to overflow? - redis

In redis integer increment commands, more specifically:
INCR / INCRBY
HINCRBY
what is the behavior when incrementing 1-past the maximum value (ie 64bit signed range)?

It would error saying that the increment would overflow:
127.0.0.1:6379> set foo 9223372036854775807
OK
127.0.0.1:6379> incr foo
(error) ERR increment or decrement would overflow

Related

Redis bitcount command returns a syntax error

after set myKey as key in Redis with foobar value
I want to get the BITCOUNT of myKey.
this command in Redis-CLI giving me an error: BITCOUNT myKey 2 3 BYTE
(error) ERR syntax error
how can I solve this?
BYTE argument is fairly new and (will be) added in Redis 7.0. So far, only 7.0.0rc3 is released but no GA.
I assume you are using a pre Redis 7 version?

Redis inconsistency between bigkeys and llen

When I scan entire redis instance using redis-cli --bigkeys following shortened result is returned
-------- summary -------
Sampled 241145 keys in the keyspace!
Total key length in bytes is 13013217 (avg len 53.96)
Biggest string found 'celery-task-meta-52b14b66-b924-4c40-b7dc-7d5b9b633470' has 6510 bytes
**Biggest list found 'celery9' has 156519 items**
Biggest set found '_kombu.binding.celeryev' has 52 members
Biggest hash found 'unacked' has 544 fields
Biggest zset found 'unacked_index' has 550 members
As you can see my biggest list is celery9 with length 156519. I am using only one keyspace
127.0.0.1:6379> info keyspace
# Keyspace
db0:keys=256672,expires=256659,avg_ttl=1701804
But when I connect to redis instance using redis-cli or even with redis connector from python and run following commands
127.0.0.1:6379> get celery9
(nil)
127.0.0.1:6379> llen celery9
(integer) 0
127.0.0.1:6379>
nil or zero is returned as if there was no key celery9.
So the question is, how to get correct length of this key? All others keys are working perfectly

How to know a redis command is write or readonly?

I mean is there a command used to check whether a redis command is readonly?
is_readonly(command)
It will better if there is command in Jedis.
with COMMAND you can get list of all commands with command flags such as
readonly
so here's the list I've end up with:
publish bgsave sunion readonly exists hstrlen lindex scan ping latency ttl wait zscore zrevrangebylex sscan geohash getbit hkeys zrange monitor llen save auth zcard shutdown sync dbsize subscribe zrangebylex zlexcount mget getrange bitpos config lrange replconf discard asking client pfselftest bgrewriteaof unsubscribe zrank readwrite hget bitcount randomkey time zrevrank sinter dump strlen unwatch smembers georadius lastsave slowlog sismember hexists multi sdiff geopos hscan script keys hvals pfcount zscan echo command select zcount substr pttl hlen info scard geodist srandmember hgetall cluster psync pubsub psubscribe zrevrange hmget object watch zrangebyscore get type zrevrangebyscore punsubscribe georadiusbymember
though you might want to exclude readonly admin commands - they don't change data but can cause side effects. here's list with excluded admin:
publish sunion readonly exists hstrlen lindex scan ping ttl wait zscore zrevrangebylex sscan geohash getbit hkeys zrange llen auth zcard dbsize subscribe zrangebylex zlexcount mget getrange bitpos lrange discard asking client pfselftest unsubscribe zrank readwrite hget bitcount randomkey time zrevrank sinter dump strlen unwatch smembers georadius lastsave slowlog sismember hexists multi sdiff geopos hscan script keys hvals pfcount zscan echo command select zcount substr pttl hlen info scard geodist srandmember hgetall pubsub psubscribe zrevrange hmget object watch zrangebyscore get type zrevrangebyscore punsubscribe georadiusbymember
more here: http://redis.io/commands/command#examples
I think you might also check which commands is available for specific redis server version.

How do you stop redis from changing the number format?

redis 127.0.0.1:6379> zadd somekey 12.54 value
(integer) 1
redis 127.0.0.1:6379> zrevrange somekey 0 -1 withscores
1) "value"
2) "12.539999999999999"
How do I keep the original number format in redis?
Redis relies on client for number precision conversion, so this might not be possible to achieve using Redis default cli using python.
A similar thread for same :
http://www.manning-sandbox.com/thread.jspa?messageID=159190

How to use counters in a redis key?

Is there a way to do this in redis ?
SET counter 0
INCR counter
SET KEY:{counter} "Content of line 1"
INCR counter
SET KEY:{counter} "Different content of line 2"
My example code should be substituted (i.e., transformed at runtime by the redis-cli) into:
SET counter 0
INCR counter
SET KEY:1 "Content of line 1"
INCR counter
SET KEY:2 "Different content of line 2"
etc.
My problem is NOT how to auto-increment the counter.
My problem is syntax: How to include a generic {wildcard} into something like:
SET keyname:{currentcounter} "value" ...
Any help is appreciated. Thanks a lot !
bernie
If you are using redis 2.6+ then you can use lua scripting along with EVAL command like the following:
eval "local c = redis.call('incr', KEYS[1]);
return redis.call('set', KEYS[2] .. ':' .. c, ARGV[1])"
2 counter KEY "Content of line 1"
I broke it up onto multiple lines to make it easier to read.
EDIT
Sorry, I was away on business for a few days. Here is a sample showing that it works.
redis 127.0.0.1:6379> flushdb
OK
redis 127.0.0.1:6379> eval "local c = redis.call('incr', KEYS[1]); return redis.call('set', KEYS[2] .. ':' .. c, ARGV[1])" 2 counter KEY "Content of line 1"
OK
redis 127.0.0.1:6379> keys *
1) "KEY:1"
2) "counter"
redis 127.0.0.1:6379> get counter
"1"
redis 127.0.0.1:6379> get KEY:1
"Content of line 1"
redis 127.0.0.1:6379> eval "local c = redis.call('incr', KEYS[1]); return redis.call('set', KEYS[2] .. ':' .. c, ARGV[1])" 2 counter KEY "Content of the next thing"
OK
redis 127.0.0.1:6379> keys *
1) "KEY:1"
2) "KEY:2"
3) "counter"
redis 127.0.0.1:6379> get counter
"2"
redis 127.0.0.1:6379> get KEY:2
"Content of the next thing"
Nope, SET/GET commands don't support this.
You can do similar things using LUA scripts in redis, or even simpler; you can issue the commands as redis expect them using trivial programming/scripting