Bounded sorted set

53 views
Skip to first unread message

Adityanarayan Vaidya

unread,
Apr 15, 2016, 2:09:26 PM4/15/16
to Redis DB
Hi All,

Does redis have a bounded sorted set ?

Regards,
Aditya

Itamar Haber

unread,
Apr 15, 2016, 2:42:26 PM4/15/16
to Redis DB
Hi Aditya,

All sets in Redis are bounded - they can grow "only" up to 2^32 members. I suspect, however, that that isn't your intention.

What do you mean by "bounded sorted set"? Is it bounded in terms of cardinality, scores, both? Also, once the bounds are met, what do you expect will happen when trying to add a new member - an error, discard the new member, or remove an existing member according to some logic & then add the new one?

Cheers,
Itamar

--
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.

AlexanderB

unread,
Apr 15, 2016, 2:58:38 PM4/15/16
to Redis DB
You might want to look into redis lua scripting. While Redis probably doesn't natively support a data type that exactly fits your needs, with scripting you could build a higher level interface to get / set from a normal sorted set that also enforces the extra logic you want.

 This redis green post is a pretty good reference to get a sense of what's possible. https://www.redisgreen.net/blog/intro-to-lua-for-redis-programmers/

Adityanarayan Vaidya

unread,
Apr 15, 2016, 4:17:35 PM4/15/16
to Redis DB
Hi Ita,

I was more specifically looking at bound in terms of cardinality. In terms of adding a new member when a bound is met I was looking to remove member by least score if the score of the new member is higher.

The use case is related to TOP N view by score. So I say need at max TOP 100 (N) in the sorted set at any point in time. Currently I am trimming the sorted set every x secs to 100 (N) just to
keep it bounded to 100 (N).

Regards,
Aditya

Adityanarayan Vaidya

unread,
Apr 15, 2016, 4:28:51 PM4/15/16
to Redis DB
Sure Alex.

Regards,
Aditya

Itamar Haber

unread,
Apr 15, 2016, 5:07:39 PM4/15/16
to Redis DB
Thanks for the clarification. As Alex had suggested, the way to go here is Lua. Specifically, with a script such as the following:

$ cat ~/zaddcapped.lua 
--[[
usage: EVAL "thisscript" 1 <sorted-set-key> <cap> <score> <member> [<score> <member> ...]
reply: integer, the number of members added (not final set cardinality)
performs **no** error/type checking
]]--

local cap = table.remove(ARGV,1)
local added = redis.call('ZADD',KEYS[1],unpack(ARGV))
redis.call('ZREMRANGEBYRANK',KEYS[1],0,-(cap+1))
return added

$ ./redis-cli DEL z
(integer) 1
$ ./redis-cli ZADD z 1 a 2 b 3 c
(integer) 3
./redis-cli --eval ~/zaddcapped.lua z , 5 4 d 5 e 6 f 7 g
(integer) 4
./redis-cli ZRANGE z 0 -1 WITHSCORES
 1) "c"
 2) "3"
 3) "d"
 4) "4"
 5) "e"
 6) "5"
 7) "f"
 8) "6"
 9) "g"
10) "7"

--
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 Developer Advocate
Redis Watch Newsletter | &&(curat||edit||janit||)
Redis Labs | ~ of Redis

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

Blog  |  Twitter  |  LinkedIn

 #RedisConf

Adityanarayan Vaidya

unread,
Apr 15, 2016, 5:12:26 PM4/15/16
to Redis DB

Also i need to pop the element with the least score from the sorted set.

Is there any other way I might be missing then using ZRANGE & then ZREM?

Regards,
Aditya

AlexanderB

unread,
Apr 15, 2016, 5:18:45 PM4/15/16
to Redis DB
Either a transaction or a lua script with those two commands is exactly the way to go. 

Zpop has been one of the classic use cases for scripting, here's a quick one that's been sitting around on github for close to 5 years. https://gist.github.com/droot/1033867

You can also see a way of doing it with a WATCH style transaction in the redis docs. 

Adityanarayan Vaidya

unread,
Apr 15, 2016, 6:06:34 PM4/15/16
to Redis DB
Thanks Ita.

Regards,
Aditya

Adityanarayan Vaidya

unread,
Apr 15, 2016, 6:07:02 PM4/15/16
to Redis DB
Thanks Alex.

Regards,
Aditya
Reply all
Reply to author
Forward
0 new messages