NoSQL. Redis. The number of online-people

Asked byanna landers

A question that has been tormenting for a couple of days.

There is an application that uses mysql to store information about users and nosql / redis to provide quick write / read information.

There are a lot of these procedures (read / write), so radishes have been chosen.

Part of the task is completely ported to redis. but there is a question on which I can not yet find a solution.

The application should show the number of people "online."

If you can update the online field with the current timestamp on the sql when prompted and then do where online & gt; timestamp-15m and show everything

how can I be in the key-value repository yet I can not understand.

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 there +1 if the request was in these 15 minutes.

But this option is not acceptable, since when you move from 14 to 15 minutes the counter will show 0.

In general, the question is this. How to withdraw and save "online" people.

(PS: There is an option to write current seconds, and then in a descending cycle, read the last 900 (15 minutes) and read them, but it seems to me through the ass solution)

Comments:
Actually the question decided the following way:
 When a key is formed, the key online_20110602_13_1, where the user id is written, the key is created for 15 minutes.
 Every minute is a new key.
 When you need to get a list - the last 15 keys pass and merge. It turns out that some values ​​are duplicated, but not critical.
 Since the key is made on 15M, they are cleaned and there is always a maximum of 15 keys per number of online ones in the radish.

At the moment, I consider the solution acceptable. - m

Answers

momo
Ok, a better option :) store user IDs in per-minute keys online_20110602_13_00, ..., online_20110602_13_59, setting each TTL for 15 minutes, extract with the help of sunion from the last 15 keys. If accuracy is not particularly important, then you can not store, but minute by minute.
Replies:
in, I think this option is better. 3-5m step is not very critical and at the same time will not generate a bunch of keys.

thank - francesca
delana
According to your version - if you write +1 for each request, then there will be not the 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 in the following way: I write down the radish (sadd) ID and put / prolonged the “delayed” task (memcacheq) to delete it (srem) after 15 minutes.
Replies:
that is, it turns out a certain list of "online" which is cleaned by a third party. option. need to think about, maybe there are still ways without additional connections. by - kristaps
jill anne
You can do this:
on hit - ZADD guys_online & lt; unix_timestamp & gt; & lt; user_id & gt;
get the number of guys online - ZCOUNT guys_online & lt; unix_timestamp-15 * 60 & gt; + inf
clean old records from time to time
 - ZREMRANGEBYSCORE guys_online -inf (& lt; unix_timestamp-15 * 60 & gt;
Replies:
in fact, I suggested exactly that :) - ashlar
bkwyrm
You can use zadd and zremrangebyscore, for example, like this: github.com/donnerjack13589/redis.tracker/blob/master/lib/tracker/announcer.js#L44-53
What exactly, when and where is NoSQL-solutions preferable to traditional SQL? :: Coworking Center in Moscow? :: Prompt service for the accounting of working time :: Doctrine 2, optimization of communication sampling :: Where better to look for such developers?
Leave Repply forNoSQL. Redis. The number of online-people
Useful Links