Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wondering how Facebook does the "Mutual friends" feature

I'm currently developing an application to allow students to manage their courses, and I don't really know how to design the database for a specific feature. The client wants, a lot like Facebook, that when a student displays the list of people currently in a specific course, the people with the most mutual courses with the logged in user are displayed first. Almost the same as Facebook feature "Friend suggestions" with an additional filter.

As an additional feature, I would like to add a search feature to allow students to search for another one, and displaying first in the search results the people with most mutual courses with the logged in user.

I currently use MySQL, I plan to use Cassandra for some other features, and I also use Memcached for result caching and Sphinx for the search.

Thanks.

--

The app is developed in Python, BTW

And I forgot to mention that the standard approach (using a nice MySQL query to calculate all this with an ORDER BY clause) is wayyyys too slow. So as reads are a lot more frequent than reads, I would like most of the logic to take place once, when the relation people <-> course is added.

I thought about updating a "mutual courses" counter specific to one tuple (user, course) that will be increased for all users of a course when the logged in user joins a new course (or decreased when he leaves it).

like image 490
Pierre Avatar asked Mar 29 '10 09:03

Pierre


People also ask

How does Facebook mutual friends work?

Mutual friends are the people who are Facebook friends with both you and the person whose profile you are viewing. For instance, if you are friends with Chris, and Mark is friends with Chris, then Chris will be shown as a mutual friend when you are viewing Mark's profile.

What determines the order of mutual friends on Facebook?

The Facebook friends list arrangement is determined using activity – interactions, photos, communication, and more. This decides the order of your friends and their priority. Those who you interact with the most will be at the top of your friends list and also appear the most on your Facebook feed.

How can you tell if two people have mutual friends on Facebook?

Head to the Timeline of any current Facebook friend, and click the "Friends" tab. Before you even click, you'll see a number indicating how many mutual friends you two share. Now click "Mutual Friends," and you'll be greeted with a full list of all the connections you two have in common.

Do mutual friends always show on Facebook?

Also, if the person's full list of friends is hidden to you, a mutual friend who also has their full list of friends hidden to you will not be displayed as a mutual friend. In other words, if a mutual friend's friend list is hidden, it will not be show up.


2 Answers

Say you have a table that is named Users and the Primary Key is UserID. Then you have a table called Friends with 2 columns called UserID (PK) and FriendUserID.

Say you have 2 users, 20 and 50.

When 20 adds 50 as friend, the application adds a new row:

INSERT INTO `Friends` (`UserID`, `FriendUserID`) VALUES (20, 50)

and when 50 confirms friendship, you add another row with values switched:

INSERT INTO `Friends` (`UserID`, `FriendUserID`) VALUES (50, 20)

When you want to find mutual friends between 20 and 50, simply:

SELECT `UserID` FROM `Friends` AS `A`, `Friends` AS B WHERE `A`.`FriendUserID` = 20 AND `A`.`UserID` = `B`.`UserID` AND `B`.`FriendUserID` = 50
like image 87
mauris Avatar answered Oct 18 '22 03:10

mauris


If you already have your solution, but the problem is just the speed of that query, try doing it sooner. When a user's friendships change, rerun a job that calculates these things and store all the results away. Don't runt his as a result of a request, when you need the result so quickly. Do such expensive things only once and do them before a request is ever made.

like image 4
ironfroggy Avatar answered Oct 18 '22 01:10

ironfroggy