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/.

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.