Facebook enhances C++ with parallel programming tool

19.06.2015
Ever wondered how Facebook is able to assemble personalized news feeds, in real time, for any of its billion-plus users who may log in at any given moment Now we know, because the company has just released the source code behind the magic.

Drawing from their considerable expertise running large workloads, Facebook engineers developed a framework, called Folly Futures, that allows programmers to easily break apart their C++ applications so they can run more quickly in parallel, or asynchronously.

The framework allowed Facebook to cut the average latency of its news feed by two-thirds, and significantly reduced the number of time-out errors that users see.

"Futures made it natural to express the asynchronous computation," Hans Fugal, a Facebook engineer who helped create the framework, said in a blog post.

"When developers have the tools to understand and express asynchrony better, they write lower-latency services that are easier to maintain," he wrote.

Facebook's Instagram service also uses the farmework, to improve performance and reduce the number of servers it needs for a service that recommends to users who they should follow.

The new tool tackles ones of the most difficult problems in computer programming, that of parallel computing.

Parallel computing allows multiple parts of a program to run simultaneously to speed performance. The practice can be fraught with difficulty, however, if many services within a program depend on each other to complete their jobs.

A service for rendering a Web page, for example, may need content from another service, such as a database, before it can complete its task. Until it gets the information, the entire program may stall.

"The result is wasted resources, reduced throughput, and increased latency (because requests are in a queue, waiting to be serviced)," Fugal wrote.

The programmer can work around this manually, through standard parallel programming practices, but the resulting code can quickly become a tangled mess, a condition sometimes referred to as callback hell.

"Traditional asynchronous code is more efficient than synchronous code, but it is not as easy to read," Fugal noted.

The latest release of the C++ programming language, version C++11, includes a capability called Futures that allows programmers to neatly program in parallel, so a program can continue to execute new work even as it awaits the completion of individual operations.

In essence, a "future" is a stub that can be held by part of a program until the results are returned by another operation, allowing the program to continue on to additional tasks.

Facebook's Futures, part of its Folly library of open source programming tools, provides a way to easily use C++ futures. It extends the generic C++11 futures capabilities to enhance readability of the code while maintaining compatibility of the C++ API (application programming interface). The development team used the Twitter Futures, written for the Scala language, as a point of reference.

Instagram used Folly Futures to create a set of recommendation services that can handle tens of thousands of queries per second and generate tens of millions of connections per day.

The services generate lists of additional Instagram accounts that users could follow, drawing from a pool of known friends, friends of friends, and popular accounts in the user's area.

"After moving to Futures, we had a performant system with clear, readable code," Instagram said. "Our services can fully exploit system resources and are more reliable and efficient."

Because of the restructuring of the code, CPU utilization of the suggested user service bounced to 90 percent from the 10 to 15 percent range. Better CPU utilization meant fewer copies of the program needed to be run at once, from 720 to 38, thus requiring fewer servers. Each program is capable of handling 800 requests per second, with an average latency of 40 milliseconds.

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

Joab Jackson