Answer the question
In order to leave comments, you need to log in
NoSQL. Redis. Number of online people
This is a question that has been bothering me for a couple of days now.
There is an application that uses mysql to store information about users and nosql / redis to provide fast writing / reading information.
There are a lot of these procedures (read / write), so radish was chosen.
part of the task is completely ported to redis. But I have a question that I can't find a solution for yet.
The application should show the number of people "online".
If in sql it is possible to update the online field with the current timestamp when querying and then do where online> timestamp-15m and everything will show
how to be in the key-value storage, I still can’t figure it out.
there was an option to create a key that would change every 15 minutes. For example:
online_20110602_13_0
online_20110602_13_1
online_20110602_13_2
online_20110602_13_3
online_20110602_14_0
online_20110602_14_1
...
and write +1 there if the request was in these 15 minutes.
But this option is not acceptable, since when switching from 14 to 15 minutes, the counter will show 0.
In general, the question is this. How to display and save "online" people.
(PS: There is an option to write the current seconds, and then in a descending loop to read the last 900 (15 minutes) and count them, but it seems to me a solution through the ass)
Answer the question
In order to leave comments, you need to log in
Ok, a better option :) store user IDs in per-minute keys online_20110602_13_00, ..., online_20110602_13_59, setting each TTL to 15 minutes, extract using sunion from the last 15 keys. If accuracy is not particularly important, then you can store it for five minutes instead of every minute.
According to your version - if you write +1 for each request, then there will be not a number of online users, but hits. It turns out that there are no duplicates in any way, you need to store user IDs.
I solved the problem like this: I write down the ID in the radish (sadd) and set / prolong the “deferred” task (memcacheq) to delete it (srem) after 15 minutes.
You can do this:
on a hit - ZADD guys_online <unix_timestamp> <user_id>
get the number of online guys - ZCOUNT guys_online <unix_timestamp-15*60> +inf
clean up old records from time to time
- ZREMRANGEBYSCORE guys_online -inf (<unix_timestamp-15*60>
You can use zadd and zremrangebyscore like this: github.com/donnerjack13589/redis.tracker/blob/master/lib/tracker/announcer.js#L44-53
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question