Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After discussing this question in the <a href="https://groups.google.com/forum/?fromgroups#!topic/redis-db/OsUylXWDxWU" rel="nofollow">Redis DB Google Groups</a>, my proposed solution (inspired by <a href="http://www.lukemelia.com/blog/archives/2010/01/17/redis-in-practice-whos-online/" rel="nofollow">this article</a>) is to <code>SADD</code> all my online users into a single set, and for each of my users, I create a <code>user:#{user_id}:friends_list</code> and store their friends list as another set. Whenever a user logs in, I would <code>SINTER</code> the user's friends list and the current online users set. Since we're read heavy and not write heavy, we would use a single master node for writes. To make it scale, we'd have a cluster of slave nodes replicating from the master, and our application will use a simple round-robin algorithm to do the <code>SINTER</code></p> <p><a href="http://twitter.com/#!/dr_josiah" rel="nofollow">Josiah Carlson</a> suggested a more refined approach:</p> <blockquote> <ol> <li>When a user X logs on, you intersect their friends set with the online users to get their initial online set, and you keep it with a Y-minute TTL (any time they do anything on the site, you could update the expire time to be Y more minutes into the future) </li> <li>For every user in that 'online' set, you find their similar 'initial set', and you add X to the set </li> <li>Any time a user Z logs out, you scan their friends set, and remove Z from them all (whether they exist or not) </li> </ol> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload