How to check Equals on two lists in Redis

44 views
Skip to first unread message

prabhakaran s

unread,
Mar 15, 2017, 6:37:13 AM3/15/17
to Redis DB

I have two lists in Redis. How can I check those lists are equal? Is the only option is to get elements from the list one-by-one and compare to the list in memory?

Is Redis provide any workarounds?



Thanks,

Marc Gravell

unread,
Mar 15, 2017, 6:50:00 AM3/15/17
to redi...@googlegroups.com
This is probably a bad idea, but you could check that "DUMP x" gives the same result as "DUMP y" - however, you should note that the "DUMP" format is opaque and doesn't explicitly offer any guarantees other than it will work with RESTORE.

This *might* be something that you could add via a module? (I'm not saying it would be trivial or efficient)

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+unsubscribe@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc

Itamar Haber

unread,
Mar 15, 2017, 7:02:22 AM3/15/17
to Redis DB
There are no workarounds, Redis has no digest function for Lists (or other data types for that matter) and since these are doubly-linked there's no other way to compare them other than element by element.

You could make the comparison in Lua however, to make it more efficient compared to performing it client-side. Something like the following should work:

if redis.call('LLEN',KEYS[1]) == redis.call('LLEN', KEYS[2]) and
    redis.call('LRANGE',KEYS[1],0,0) == redis.call('LRANGE',KEYS[2],0,0) and
    redis.call('LRANGE',KEYS[1],-1,-1) == redis.call('LRANGE',KEYS[2],-1,-1) and
    redis.call('DUMP',KEYS[1]) == redis.call('DUMP',KEYS[1]) then
    return 'Lists are the same'
end
return 'Lists are not the same'

A module-based implementation is likely to be faster - there's the https://github.com/redislabsmodules/redex/ repo that is the perfect place to open an FR/PR to ;)

Itamar Haber | Chief OSS Education Officer
Redis Labs ~/redis

Mobile: +972 (54) 567 9692
Email: ita...@redislabs.com
Twitter: @itamarhaber
Skype: itamar.haber

Blog  |  Twitter  |  LinkedIn


Itamar Haber

unread,
Mar 15, 2017, 7:03:38 AM3/15/17
to Redis DB
^    redis.call('DUMP',KEYS[1]) == redis.call('DUMP',KEYS[2]) then

prabhakaran s

unread,
Mar 16, 2017, 3:11:26 AM3/16/17
to Redis DB
Hi,

Thanks, Itamar Haber. I have one more doubt on the list. How to check members exist in Redis list ?

Thanks. 


On Wednesday, 15 March 2017 16:33:38 UTC+5:30, Itamar Haber wrote:
^    redis.call('DUMP',KEYS[1]) == redis.call('DUMP',KEYS[2]) then
On Wed, Mar 15, 2017 at 1:01 PM, Itamar Haber <ita...@redislabs.com> wrote:
There are no workarounds, Redis has no digest function for Lists (or other data types for that matter) and since these are doubly-linked there's no other way to compare them other than element by element.

You could make the comparison in Lua however, to make it more efficient compared to performing it client-side. Something like the following should work:

if redis.call('LLEN',KEYS[1]) == redis.call('LLEN', KEYS[2]) and
    redis.call('LRANGE',KEYS[1],0,0) == redis.call('LRANGE',KEYS[2],0,0) and
    redis.call('LRANGE',KEYS[1],-1,-1) == redis.call('LRANGE',KEYS[2],-1,-1) and
    redis.call('DUMP',KEYS[1]) == redis.call('DUMP',KEYS[1]) then
    return 'Lists are the same'
end
return 'Lists are not the same'

A module-based implementation is likely to be faster - there's the https://github.com/redislabsmodules/redex/ repo that is the perfect place to open an FR/PR to ;)
On Wed, Mar 15, 2017 at 12:49 PM, Marc Gravell <marc.g...@gmail.com> wrote:
This is probably a bad idea, but you could check that "DUMP x" gives the same result as "DUMP y" - however, you should note that the "DUMP" format is opaque and doesn't explicitly offer any guarantees other than it will work with RESTORE.

This *might* be something that you could add via a module? (I'm not saying it would be trivial or efficient)
On 15 March 2017 at 10:29, prabhakaran s <prabhak...@gmail.com> wrote:

I have two lists in Redis. How can I check those lists are equal? Is the only option is to get elements from the list one-by-one and compare to the list in memory?

Is Redis provide any workarounds?



Thanks,

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.
--

Itamar Haber | Chief OSS Education Officer
Redis Labs ~/redis

Mobile: +972 (54) 567 9692
Email: ita...@redislabs.com
Twitter: @itamarhaber
Skype: itamar.haber

Blog  |  Twitter  |  LinkedIn


prabhakaran s

unread,
Mar 16, 2017, 3:32:09 AM3/16/17
to Redis DB
Hi Itamar Haber, 

The below comparison has failed while execute the program.

redis.call('LRANGE',KEYS[1],0,0) == redis.call('LRANGE',KEYS[2],0,0) and 
redis.call('LRANGE',KEYS[1],-1,-1) == redis.call('LRANGE',KEYS[2],-1,-1)

Thanks.


On Wednesday, 15 March 2017 16:32:22 UTC+5:30, Itamar Haber wrote:
There are no workarounds, Redis has no digest function for Lists (or other data types for that matter) and since these are doubly-linked there's no other way to compare them other than element by element.

You could make the comparison in Lua however, to make it more efficient compared to performing it client-side. Something like the following should work:

if redis.call('LLEN',KEYS[1]) == redis.call('LLEN', KEYS[2]) and
    redis.call('LRANGE',KEYS[1],0,0) == redis.call('LRANGE',KEYS[2],0,0) and
    redis.call('LRANGE',KEYS[1],-1,-1) == redis.call('LRANGE',KEYS[2],-1,-1) and
    redis.call('DUMP',KEYS[1]) == redis.call('DUMP',KEYS[1]) then
    return 'Lists are the same'
end
return 'Lists are not the same'

A module-based implementation is likely to be faster - there's the https://github.com/redislabsmodules/redex/ repo that is the perfect place to open an FR/PR to ;)
On Wed, Mar 15, 2017 at 12:49 PM, Marc Gravell <marc.g...@gmail.com> wrote:
This is probably a bad idea, but you could check that "DUMP x" gives the same result as "DUMP y" - however, you should note that the "DUMP" format is opaque and doesn't explicitly offer any guarantees other than it will work with RESTORE.

This *might* be something that you could add via a module? (I'm not saying it would be trivial or efficient)
On 15 March 2017 at 10:29, prabhakaran s <prabhak...@gmail.com> wrote:

I have two lists in Redis. How can I check those lists are equal? Is the only option is to get elements from the list one-by-one and compare to the list in memory?

Is Redis provide any workarounds?



Thanks,

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

Itamar Haber

unread,
Mar 16, 2017, 8:19:10 AM3/16/17
to Redis DB
Hi Prabhakaran,

Firstly, there's a mistake in the script as you've found out - the calls to LRANGE return a Lua table so the correct script is:

if redis.call('LLEN',KEYS[1]) == redis.call('LLEN',KEYS[2]) and
    redis.call('LRANGE',KEYS[1],0,0)[1] == redis.call('LRANGE',KEYS[2],0,0)[1] and
    redis.call('LRANGE',KEYS[1],-1,-1)[1] == redis.call('LRANGE',KEYS[2],-1,-1)[1] and
    redis.call('DUMP',KEYS[1]) == redis.call('DUMP',KEYS[2]) then
    return 'Lists are the same'
end
return 'Lists are not the same'

Secondly, I do not understand what you mean by "How to check members exist in Redis list" - please explain.


To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+unsubscribe@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

prabhakaran s

unread,
Mar 16, 2017, 11:12:38 AM3/16/17
to Redis DB
Hi Itamar Haber,

I want to check a value exists in a list already Redis. (Like SISMEMBER function) 


Thanks.

Itamar Haber

unread,
Mar 16, 2017, 11:21:14 AM3/16/17
to Redis DB
That's another negative - to check existence of an element in a List you'll need to traverse it (i.e. either with an LRANGE or by duplicating the List and using LREM's result on it). Again, Lua will be your friend with that.

An alternative approach, which is more space consuming but time efficient for querying, is to also store the List's elements in a data structure that is more suitable for membership tests, i.e. either a Set or a Hash. You could also, if absolute accuracy isn't needed, consider using the HyperLogLog or a Bloom filter implementation for that.

To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+unsubscribe@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

prabhakaran s

unread,
Mar 17, 2017, 2:15:15 AM3/17/17
to Redis DB
Thank you so much, Itamar Haber.
Reply all
Reply to author
Forward
0 new messages