Facebook has released the source code that allows it to assemble personalised real time news feeds its billion-plus users.
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 code cut the average latency of Facebook news feed by two-thirds, and significantly reduced the number of time-out errors that users see. The company has been increasingly open about its use of C++ in recent weeks.
"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 utilisation of the suggested user service bounced to 90 percent from the 10 to 15 percent range. Better CPU utilisation 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.