Redis: How do I count the elements in a stream in a certain range?

Alvaro Alday

Bussiness Objective

I'm creating a dashboard that will depend on some time-series and I'll use Redis to implement it. I'm new to using Redis and I'm trying to use Redis-Streams to count the elements in a stream.

XADD conversation:9:chat_messages * id 2583 user_type Bot
XADD conversation:9:chat_messages * id 732016 user_type User
XADD conversation:9:chat_messages * id 732017 user_type Staff
XRANGE conversation:9:chat_messages - +

I'm aware that I can get the total count of the elements using the XLEN command like this:

XLEN conversation:9:chat_messages

but I want to also know the elements in a period, for example:

XLEN conversation:9:chat_messages 1579551316273 1579551321872

I know I can use LUA to count those elements but I want some REALLY fast way to achieve this and I know that using Redis markup will be the fastest way.

Is there any way to achieve this with a straight forward Redis command? Or do I have to write a Lua script to do this?

Additional information

I'm limited by AWS' ElastiCache to use the only Redis 5.0.6, I cannot install other modules such as the RedisTimeSeries module. I'd like to use that module but it's not possible at the moment.

LeoMurillo

Sorry, there is no commands-way to achieve this.

Your best option with Redis Streams would be to use a Lua script. You will get O(N) with N being the number of elements being counted, instead of O(log N) if a command existed.

local T = redis.call('XRANGE', KEYS[1], ARGV[1], ARGV[2])
local count = 0
for _ in pairs(T) do count = count + 1 end
return count

Note the difference between O(N) and O(log(N)) is significant for a large N, but for a chat application, if tracked by conversation, this won't make that big of a difference if chats have hundreds or even thousands of entries, once you account total command time including Round Trip Time which takes most of the time. The Lua script above removes network-payload and client-processing time.

You can switch to sorted sets if you really want O(log N) and you don't need consumer groups and other stream features. See How to store in Redis sorted set with server-side timestamp as score? if you want to use Redis server timestamp atomically.

Then you can use ZCOUNT which is O(log(N)).

If you do need Stream features, then you would need to keep the sorted set as a secondary index.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I count the last elements in a list after a certain element?

Selenium C# : How do I count elements whose href contains a certain substring?

How do I efficiently count elements less than a certain value in a sorted array?

How can I count the number of rows that are not zero in a certain range in python?

How to count certain elements in array?

How do I restrict access to certain topics in redis pubsub with acl?

How do I filter a stream in dart after a certain criteria?

Java: how do I check if a Date is within a certain range?

How do I detect unfocus outside of a certain range?

How do I use the RandomRangedNumberCustomization in Autofixture to ensure parameters are in a certain range?

How do I check that the value of a param is a number in a certain range

How do I squash a certain range of history in git? (Expert)

How do I get certain Time Range in Python

How Do I Exclude Certain Data Types in a Range in Google Sheets?

How to use stream to do one thing on elements not-in-index-range and another on yes-in-range?

how to do i keep a count on certain attributes in objects

redis c# client, how do i get Subscribers count?

How to count values in a certain range in a Numpy array?

How do I print certain elements of a struct in Swift?

How do I assert an Iterable contains elements with a certain property?

How do I ignore certain elements when comparing XML?

How do I get the minimum of certain elements of an array with Python?

How do I extract certain elements from this webscraped HTML

How do I append certain elements in Json dictionary based on the ID?

How do I delete certain elements of my list?

Lua: How do I shuffle certain elements in an Array?

How do I skip certain elements in a list in a python for loop?

How do I group and count values by value range in MongoDB

How do I clone a range of array elements to a new array?