O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cross-Language Development with Facebook Thrift

Mark Slee (see his bio in Contributors)

Problem

I’m developing a large software system, and I’d rather not standardize on one programming language. I’d like to be able to make service calls and move data between languages, but it needs to be fast and easy to maintain.

Solution

Use Thrift, an open source framework developed by Facebook for cross-language data serialization and remote procedure calls in a high-performance environment.

Thrift facilitates performant interoperability between C++, Java, PHP, Python, Ruby, Erlang, Haskell, C#, Cocoa, Smalltalk, and even OCaml.

Discussion

Thrift uses code generation to enable rapid development of software spanning multiple programming languages. Data types and service interfaces are defined in .thrift files, which use a simple, language-neutral grammar. The Thrift compiler then generates all the necessary plumbing code in the languages of your choice to encode and transport data objects and make service calls, letting the developer focus on writing the actual application code.

For example, suppose you have created a web frontend in PHP. You want to build a service to efficiently search an object store, and you’d like to build this service in C++ to optimize memory usage and data structure layout. Here’s how you might define such a service in Thrift:

search.thrift:
struct search_result_elem {
  1: i64 object_id,
  2: list<string> terms.
  3: i64 weight
}

struct search_result {
  1: i32 total,
  2: list<search_result_elem> matches
}

service search {
  search_result query(1:list<string> terms)
}

To generate all the framework code you need, you’d simply execute the following:

thrift -cpp -php search.thrift

Your C++ server stub is generated, and you just need to fill in the method implementation:

class searchHandler : virtual public searchIf {
 public:
  searchHandler() {
    // Your initialization goes here
  }

  void query(search_result& _return, const std::vector<std::string> & terms) {
    // Your implementation goes here
    printf("query\n");
  }
};

Similarly, all your PHP client code is generated. To make a call to the service, you simply write the following:

function call_search() {
  // Specify the server to connect to
  $transport = new TSocket('192.168.1.1', '9090');
  $transport->open();
  // Pick the protocol you want to use
  $protocol = new TBinaryProtocol($transport);
  // Make a client for your service
  $client = new searchClient($protocol);
  // Make the call!
  $results = $client->query(array('term1', 'term2'));
}

Thrift supports many advanced features, such as:

  • Service inheritance

  • Cross-language support for application-level exceptions

  • Compatibility across application version changes

  • Multiple encoding protocols (binary, JSON, etc.)

  • Multiple transport mechanisms (TCP sockets, files, HTTP)

  • Multiple server implementations (threaded, libevent, pooling)

For more information on Thrift, visit http://developers.facebook.com/thift/.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required