Live data from Hacker News

Finally Redis collections are iterable

antirez.com

21–27 of 27 posts

Re: Finally Redis collections are iterable

#21
post #6

Elements added during the iteration may be returned, or not, at random. I can't be the only one who thinks this is crazy behaviour.

It is actually very reasonable. Other alternative is that iterator returns an error if underlying collection was modified (fail fast iterators).

.NET's collections all fail fast for seemingly this reason - they can't really provide anything else without huge performance consequences (even the fail fast behavior comes with a cost, due to the check...)

Re: Finally Redis collections are iterable

#22

Elements added during the iteration may be returned, or not, at random. I can't be the only one who thinks this is crazy behaviour.

Redis has keyspace notifications [0], so you can make your iterator aware that it should possibly restart.

[0] http://redis.io/topics/notifications

Re: Finally Redis collections are iterable

#23

Elements added during the iteration may be returned, or not, at random. I can't be the only one who thinks this is crazy behaviour.

I'm very happy with this compromise.

Schema migrations involving tens of millions of keys can be challenging. The KEYS command returns all matching keys, so you can roll your own iterator with KEYS AAA* then KEYS AAB* etc. But keys aren't evenly distributed so some result sets are zero and others are still too large.

I've been indexing keys with Redis lists and iterate through the index with LRANGE. That uses extra memory and causes more roundtrip calls.

I can deal with this compromise since SCAN makes Redis much more useful for my applications.

Re: Finally Redis collections are iterable

#25

Elements added during the iteration may be returned, or not, at random. I can't be the only one who thinks this is crazy behaviour.

How is this any different from an iterator in Java? IIRC, if you have the iterator of a Collection and add elements to it during iteration, the results are undefined. You can, however, safely remove elements even while iterating.

I don't use Java.

Re: Finally Redis collections are iterable

#26

Elements added during the iteration may be returned, or not, at random. I can't be the only one who thinks this is crazy behaviour.

I can't see why they don't just restart the cursor at the beginning when the collection goes up a size in the middle of an iteration.

Encode the size of the collection in the first 8 bits of the cursor value, and when the client gives you an cursor value with the first 8 bits set to a smaller size than the current size of the collection, start it over from scratch with the new size as the top 8 bits.

If the collection has shrunk, no big deal, just keep going from where the client was because of the mentioned properties of using a reversed bit set.

Re: Finally Redis collections are iterable

#27
post #13
post #8

This is cool and helpful, but not a game-changer. For things like garbage-collecting I've been using probabilistic techniques (just get a random element and check it, make sure you check enough to have guarantees you need) with great success. The new scanning doesn't provide any tight guarantees (it can't, really, without sacrificing a lot of what Redis stands for), so it will be more of a convenience than a really n…

I agree that for some data type like Sets you could use SRANDMEMBER, however to sample all the elements by random sampling you need to perform a lot more work. For example for a 10000 elements collection in average you are going to need around 10 times the requests.

Yes. I was lucky to have applications where that wasn't a problem — mostly because this kind of work was done regularly and incrementally, rather than rarely and in a big chunk.
Post reply on HN