Facebook uses functional programming to make News Feeds run smoothly

Functional programming has been instrumental in helping debug complex code issues, one Facebook development team has found

Facebook went with a novel programming methodology, called functional programming, to update the News Feed for its Apple iOS client, which helped considerably in tuning and debugging the app.

Functional programming has "increased our reliability in a really big way," said Adam Ernst, an engineer based at Facebook's New York office, who oversaw the effort to rebuild the News Feed for Apple iOS devices.

Facebook's embrace is the latest indication that the functional style of programming, though a radically different approach for many programmers, may offer greater benefits than the declarative style of programming favored today.

Other organizations may also find investigating this approach is of value, in terms of offering better performance and easier debugging.

Ernst and his team turned to functional programming, of which he is an admitted enthusiast, because of the growing headaches around managing and debugging the code used for populating each user's News Feeds with new items.

While the feed may appear to be a simple feature of Facebook, its simplicity is built on a complex set of gears that keep the material up to date -- no easy task given Facebook's billion-plus users.

The News Feed includes status updates, photos, videos, location check-ins and various items of interest that users find on the Internet. Each time a logs into Facebook, a new customized feed of items posted by their friends is created. (Ernst's team is not involved in the sometimes controversial work Facebook does in deciding which items to show in user News Feed).

This operation must happen almost instantaneously because even a 100 millisecond delay can be felt by the user as latency. Making things even more complicated is that with Facebook, users add additional material to News Feed Items, either in the comment section for each entry, or through the "like" button that allows users to express satisfaction that a particular item has been posted. All this material must be updated on the fly, in as close to real-time as possible.

To date, Facebook has used the traditional Model-View-Controller (MVC) approach to coding the News Feed.

The MVC approach can present some challenges, Ernst explained. In particular, it can be difficult to pinpoint subtle errors that result in race conditions, in which two user interactions may interact with one another in unexpected ways. Race conditions are particularly difficult to diagnose, because they may happen only occasionally, based on the unique state of the machine, which is difficult to pinpoint at any given moment in an MVC setup.

Functional programming eases this problem immensely, Ernst said.

"Functional programming allows you to understand how state flows through code and how to reproduce things easily, which is vital," he said.

Using functional programming, a developer composes a program by assembling a series of functions. A functional program does not use the standard loops that a declarative program does and, once assigned a value, a variable cannot be changed -- a design feature often called immutability.

The academic community has long championed functional programming as a superior technique, though until recently, it has never seen much usage in industry until recently. Ernst attributes the growing popularity to the use of multicore processors, which make it more difficult to program for, at least for complex programs, using standard declarative methods.

With the new Facebook iOS client -- which has already been deployed on many user's iPhones and iPads -- every new user interaction, even a simple "like," calls for the user's entire News Feed to be laid out again. This approach requires optimization to make the code run as quickly as possible. Getting the News Feed to run at an acceptable speed wasn't an insurmountable challenge for Facebook. And using functional programming dramatically reduced the effort required in debugging problems, Ernst said.

Nor was memory an issue.

One of the concerns around functional programming has been in the amount of memory a functional program can use -- which often can be more than a standard decorative language. Ernst said this is not a concern. A photo taken on a device can take up megabytes of space, so in comparison, storing the text-based directory tree of a News Feed does not take up that much memory.

There have been a number of programming languages that are specifically designed for functional programming, some of which have been around for decades, such as Haskell, Lisp, Scheme, ML and, more recently, Clojure and Microsoft's increasingly popular F# language. Many other languages can be used for functional programming tasks, however, such as Java, JavaScript, Ruby, Scala, Python and Apple's recently launched Swift.

The team did not use a special functional language, such as Eiffel or Scheme, but rather Objective C++, a mixture of C++ classes and Apple's Objective C classes. C++ is what Ernst called a "multiple paradigm language," one able to support multiple programming models. So it supports functional programming as easily as it does as object oriented programming.

"It's definitely more challenging [writing functional in C++] compared to writing in a full functional language, but it also retains some familiarity for developers," Ernst said.

This is not the first use of functional programming for the social media giant. Elsewhere in the company, engineers developed React, a user interface library that is built on the functional architectural style. React inspired Ernst to try functional programming for his own task. Facebook engineers also developed and released Haxl, a Haskell library for querying data sets.

Ernst would like to release as open source the functional code used for the News Feed, though the code isn't ready for public release. But other development units within Facebook have taken an interest in the approach, Ernst said. For instance, the Facebook team working on the Android client is investing the possibility of using functional programming for its News Feed.

"Facebook is cross-pollinating. The nice thing about Facebook is you can share ideas between groups. So it's been exciting that we've been able to apply that to the iOS app," Ernst said.

Joab Jackson covers enterprise software and general technology breaking news for The IDG News Service. Follow Joab on Twitter at @Joab_Jackson. Joab's e-mail address is Joab_Jackson@idg.com

Insider: How the basic tech behind the Internet works
Join the discussion
Be the first to comment on this article. Our Commenting Policies