views:

141

answers:

4

I have a running django/apache2 + memcached app (ubuntu) and would like to keep track of logged in users that are online.

What would be the best way to track this?

I would prefer not writing to the database each time a logged in user loads a page; but what other options are there?

+2  A: 

An approach might be:

you create a middleware that does the following on process_response:

  • check for a cookie called 'online', but only if the user is authenticated
  • if the cookie is not there,
    • set a cookie called 'online' with value '1'
    • set the lifespan of the cookie to 10 minutes
    • update the 'last_login' field of auth.User for this user with the current datetime

now you have all currently logged in users in your auth.User table. All Users that have a last_login newer than datetime.now()-interval(15minutes) might be considered "online".

The database will be written for every logged in user about every 10 minutes. Adjust the values "10" and "15" to your needs.

The advantage here is that database writes are rare (according to your two numeric settings 10/15). And for speed optimization make sure that last_login is indexed, so a filter on this field including Count is really fast.

Hope this helps.

mawimawi
I like this idea except I would not update auth.User's last_login field. I'd update a user profile model or perhaps another model.How would you extend this to non-authenticated users? I'm assuming many bots would not be using cookies. You might have to track via IP address somehow.
Brian Neal
A: 

A hashmap or a queue in memory with a task running every hour or so to persist it.

Peter Tillemans
A: 

You can't do that in django without using a database/persistent-storage because of the same reason why django sessions are stored in database: There can be multiple instances of your applications running and the must synchronize their states+data through a single persistence source [1]

Alternatively, you might want to write this information in a folder in a file named with user id and then check its create/modified date to find the required information.

sharjeel
Django sessions do not have to be stored in the database. They can be stored in the cache.
Brian Neal
+1  A: 

You need to persist the info server-side, integrity isn't critical, throughput and latency are important. That means you should use some sort of key-value store.

Memcached and redis have keys that expire. You probably have memcached already installed, so use that.

You can reset expiry time of the user:last-seen:$username key every visit, or you can use mawimawi's cookie technique and have expiry = 4 * cookie-lifetime.

Tobu
But how would you retrieve which users are online from the cache, say for display in a template tag?
Brian Neal
I understood the OP only wanted an “online right now” lightbulb on user thumbnails and profile pages, and that there were too many online users to show a list. Others have answered with different assumptions.
Tobu