How do you stop redis from changing the number format? - redis

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

Related

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

Redis-cli - list of running queues command?

I want to see the List of queues in the Redis server using Redis-cli. I am using this command to just monitor the queue.
redis-cli MONITOR | grep queuename
Please tell me if we have the any cli comamnd which meet my requirement.
I don't seem to have enough reputation to clarify in a comment how you have implemented your queue, so I'll provide a few thoughts below assuming you have your queue implemented as a FIFO queue using RPUSH and LPOP to add and remove items from your queue.
> RPUSH queue-1 "task-a"
(integer) 1
>LPOP queue-1
"task-a"
If you use a standard naming convention for your lists that represent queues, you could get them by name from the KEYS command with something like KEYS queue-*. A couple of notes on this approach. First, this has some performance concerns if you have a large number of keys in your production instance the best use is for ad-hoc troubleshooting when the rest of your team is aware there may be some performance hit to your redis instance. Second, this will only show keys where the list contains elements. If you have drained a queue it will not appear in the returned values.
An alternative using sorted sets to hold the keys for the lists used as queues, and modifying the score associated with the queue to give you an idea of the queue size. When adding or removing a message to a queue, you would also use ZADD to increment the score by the number of elements added or returned. This would allow you to quickly get the set of lists used as queues by decreasing queue size with ZREVRANGE at any point.
> RPUSH queue-1 "task-a"
(integer) 1
> ZADD queues INCR 1 queue-1
"1"
> RPUSH queue-1 "task-b"
(integer) 2
> ZADD queues INCR 1 queue-1
"2"
> RPUSH queue-2 "message-a"
(integer) 1
> ZADD queues INCR 1 queue-2
"1"
> RPUSH queue-2 "message-b"
(integer) 2
> ZADD queues INCR 1 queue-2
"2"
> LPOP queue-2
"message-a"
> ZADD queues INCR -1 queue-2
"1"
> ZREVRANGE queues 0 -1 WITHSCORES
1) "queue-1"
2) "2"
3) "queue-2"
4) "1"

Not sure how to run the CAS (compare and swap) code snippet from Redis documentation page

I am trying to run the code from the redis transactions page. Specifically, this part:
WATCH zset
element = ZRANGE zset 0 0
MULTI
ZREM zset element
EXEC
If I try to do it from the cli, line by line, I get this:
localhost:6380> zadd set 1 a
(integer) 1
localhost:6380> WATCH zset
localhost:6380> element = ZRANGE zset 0 0
(error) ERR unknown command 'element'
OK
which probably means I'm doing something wrong? I remember working with lua about 9 years ago, so this doesn't really look like lua either to me.
How does someone run that snippet? Is it only some kind of pseudocode?
As #Dinei said, the example given is pseudocode.
Let's look at it (I added line numbers for us to refer to):
1 WATCH zset
2 element = ZRANGE zset 0 0
3 MULTI
4 ZREM zset element
5 EXEC
The point of the exercise is to solve the race condition that would occur if we only read the key (with ZRANGE, in line 2), and then modify the key (with ZREM in line 4). I assume you understand the problem if we didn't use the "CAS" semantics, so no need to get into it.
As pointed out, redis-cli just gives you the ability to run redis commands and see their replies, but not save values in variables, etc.
So the idea of the example is that in line 2, we are "saving" the result of the "read" operation, into a pseudo-variable element.
Then, in line 4, we are using that value in our "set" operation, and of course lines 1, 3 and 5 are just the "CAS" commands to ensure there is no race condition.
Presumably the actual usage of such commands would be done from a redis client in a programming language that would allow us to save the return value of the ZRANGE and then use it later in the ZREM command.
But if you wanted to run it in redis-cli, you'd see this, where we pretend that our client-side code would have read and saved "a" that was returned from zrange and then passed that value to the zrem command:
127.0.0.1:6379> zadd zset 1 a
(integer) 1
127.0.0.1:6379> watch zset
OK
127.0.0.1:6379> zrange zset 0 0
1) "a"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> zrem zset a
QUEUED
127.0.0.1:6379> exec
1) (integer) 1
127.0.0.1:6379>
Yes, it is some kind of pseudocode.
redis-cli only accepts Redis commands, it is not a full-fledged editor nor supports direct Lua scripting (neither variables like the element variable in the pseudocode).
I remember working with lua about 9 years ago, so this doesn't really look like lua either to me.
This is not Lua, it is pseudocode. Actually, the Redis Transactions page you linked to does not refer to Lua at all (and that's why #Piglet's comment in your post makes sense).
However, it is possible to execute Lua scripts by using Redis' EVAL command.

Redis sorted set wrong score

Let's try this.
redis 127.0.0.1:6379> zadd aaa 999999999999991.1 one
(integer) 0
redis 127.0.0.1:6379> zscore aaa one
"999999999999991.12"
redis 127.0.0.1:6379> zadd aaa 999999999999991.2 one
(integer) 0
redis 127.0.0.1:6379> zscore aaa one
"999999999999991.25"
Any solution?
Thanks.
Your redis is using IEEE 754 64-bit numbers.
When you try to represent 999999999999991.1 in a 64-bit floating-point register, the closest hex representation is 430c6bf52633ffb9. The exact decimal representation for that is 999999999999991.125 which your redis-cli is displaying as 999999999999991.12.
Similarly when you enter 999999999999991.2 the closest bit pattern found is 430c6bf52633ffba, which is exactly 999999999999991.25 -- which your redis-cli is able to show.
This loss-of-precision because of fixed register size shows up in nearly every system, Redis is no exception.

Copy a redis sorted set to a set

How do I copy a sorted set in redis to a regular, unsorted set? Is there a redis command that can do this? I can manually iterate through the sorted set and manually insert in the unsorted set, but it seems like there might be a better way to do this.
I don't think there is any command to do this directly.
But you can write simple lua script to do it on server instead downloading the sorted sets content to client and then pushing it back to new set.
Redis commands:
SCRIPT LOAD "for i,v in ipairs(redis.call('zrange', KEYS[1], 0, -1)) do redis.call('sadd', KEYS[2], v) end"
ZADD zset 1 first
ZADD zset 2 second
ZADD zset 3 third
EVALSHA dd1c22a22108d758b93c26eb92d1ef2933cec314 2 zset set
SMEMBERS set
Result:
"dd1c22a22108d758b93c26eb92d1ef2933cec314"
(integer) 0
(integer) 0
(integer) 0
(nil)
1) "second"
2) "first"
3) "third"
SCRIPT LOAD defines the script and returns its sha hash, EVALSHA than executes. Arguments are 2 to indicate that 2 key names follows, first is sorted set to copy from, second is set to copy to.