I’m having trouble thinking of how to implement an online friends list with Ruby and Redis (or with any NoSQL solution), just like any chat IM i.e. Facebook Chat. My requirements are:
- Approximately 1 million total users
- DB stores only the user friends’ ids (a Set of integer values)
I’m thinking of using a Redis cluster (which I actually don’t know too much about) and implement something along the lines of http://www.lukemelia.com/blog/archives/2010/01/17/redis-in-practice-whos-online/.
UPDATE: Our application really won’t use Redis for anything else other than potentially for the online friends list. Additionally, it’s really not write heavy (most of our queries, I anticipate, will be reads for online friends).
After discussing this question in the Redis DB Google Groups, my proposed solution (inspired by this article) is to
SADDall my online users into a single set, and for each of my users, I create auser:#{user_id}:friends_listand store their friends list as another set. Whenever a user logs in, I wouldSINTERthe 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 theSINTERJosiah Carlson suggested a more refined approach: