Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Capped Sorted Set, List, or Queue?

Tags:

redis

lua

Has anyone implemented a capped data-structure of any kind in Redis? I'm working on building something like a news feed. The feed will wind up being manipulated and read from very frequently, and holding it in a sorted set in Redis would be cheap and perfect for my use case. The only issue is I only ever need n items per feed, and I'm worried about memory overflow, so I'd like to ensure each feed never gets above n items. It seems pretty trivial to make a capped sorted collection in Redis with Lua:

redis-cli EVAL "$(cat update_feed.lua)" 1 feeds:some_feed "thing_to_add", n

Where update_feed.lua looks something like (without testing it):

redis.call('ZADD', KEYS[1], os.time(), ARGV[1])
local num = redis.call('ZCARD', KEYS[1])
if num > ARGV[2]:
    redis.call('ZREMRANGEBYRANK', KEYS[1], -n, -inf)

That's not bad at all, and pretty cheap, but it seems like such a basic thing that could be doable much more cheaply by instantiating the sorted set with only n buckets to begin with. I can't find a way to do that in redis, so I guess my question is: did I miss something, and if I didn't, why is there no structure for this in redis, even if it just ran the basic Lua script I described, it seems like it would be a typical enough use-case that it ought to be implemented as an option for redis data structures?

like image 779
Eli Avatar asked May 20 '13 00:05

Eli


People also ask

What is sorted set in Redis?

In Redis, sorted sets are a data type similar to sets in that both are non repeating groups of strings. The difference is that each member of a sorted set is associated with a score, allowing them to be sorted from the smallest score to the largest.

What does Redis use to sort the elements of a sorted set?

Redis Sorted Sets are similar to Redis Sets with the unique feature of values stored in a set. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.

How are Redis sorted sets implemented?

In redis documentation is said that: "Sorted sets are implemented via a dual-ported data structure containing both a skip list and a hash table, so every time we add an element Redis performs an O(log(N)) operation".

Is Redis ordered?

The leaderboard has been sorted in ascending order, based on the scores. Scores can be duplicated, but the members can not. If two members have the same score, Redis sorts them based on members' lexicographical order.


1 Answers

You can use LTRIM if it is a list.

Excerpt from the documentation.

LPUSH mylist someelement
LTRIM mylist 0 99

This pair of commands will push a new element on the list, while making sure that the list will not grow larger than 100 elements. This is very useful when using Redis to store logs for example. It is important to note that when used in this way LTRIM is an O(1) operation because in the average case just one element is removed from the tail of the list.

like image 168
gkamal Avatar answered Sep 28 '22 05:09

gkamal