Crunch Mode Blog - A State of Mind by Developers at D2Soft Technologies

Like us on Facebook

Facebook Coding: Too Many Friends? Be Scalable

I developed a Facebook application a few weeks back.  I wrote it using C# and the Facebook Developer Toolkit.  Querying the Facebook API is a snap, thanks to this toolkit.

However, I recently received complains that my Faceboook app was generating an error, something like:

Error while loading page from MyApplication

There are still a few kinks Facebook and the makers of MyApplication are trying to iron out. We appreciate your patience as we try to fix these issues. Your problem has been logged - if it persists, please come back in a few days. Thanks!

Of course, I didn't have that problem when trying my application.  So debugging was quite problematic.  The problem was happening on a specific page, where I list some friends of the user so that he/she can invite them (up to 10 at a time).  Also, this specific error happens when my server doesn't return the FBML data fast enough to the Facebook server.

Then, I understood the problem.  The user reporting the problem had 458 friends!  That's right.

Taking a closer look at my source code, this method looked suspicious:


And quite true.  GetFriends() actually returns ALL data of ALL friends of the user.  Make this 458 friends, and this call is very slow.  In this page, I only needed to get a few friends from this list (about 20).  So this call is overkill.

The solution is to replace this call with two other methods:



So, by calling GetFriendIds(), only IDs are returned, which is very fast.  Then, simply fill a list of friendIds that you want to get the full details, and then call GetUserInfo() with this list as parameter.

Not only do you improve query speed, but you also lower the request size.

I hope this will help some developers out there.