Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How does Facebook calculate mutual friends?

Does it cache all mutual friends for each user? Does it use MySQL to calculate mutual friends with a query?

share|improve this question
I've not used facebook, but I'll still guess they use some fairly standard graph theory technique. Take a look at to get some ideas. – Steve314 Jun 12 '11 at 17:57
Facebook uses their proprietary graph database to do these calculations. A more interesting question would be how one could do them with a SQL (or other non-proprietary) database. – Rein Henrichs Jun 12 '11 at 18:33
It is an M-M relationship. Same question as: – NoChance Sep 10 '12 at 2:55

According to Facebook,

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.

So, the set of mutual friends is the intersection of the set of your friends and the set of friends of the person you are viewing. If we assume that Facebook maintains an active cache of your friends, then when you view a person's page, another list containing that person's friends is retrieved, and the intersection of these two lists is the list of mutual friends.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.