Error message

andrea.piolanti's picture

A common problem encountered in the development of any kind of distributed system is represented by the need of a portable and efficient mechanism for exchanging data. This is precisely what Apache Thrift provides: a language and a platform developed at Facebook to serialize structured data for use in communications protocols. Obviously, several people dealt with this problem over the years and, as a result, there are many good alternatives to choose from, including plain-JSON, Protocol Buffer or XML etc. However Thrift offers something more than a platform to serialize data. In fact it allows to build scalable back-end services efficiently, and provides the suitable code for building RPC clients and servers that communicate seamlessly across programming languages.
The whole Thrift configuration is included in a single file (with the extension .thrift). This file contains the definition of the software entities and sevices. Both are expressed using the interface definition language supplied by Thrift. In order to generate code for several target programming languages (C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk ), the Thrift IDL is very simple and provides only primitive data types such as: bool, byte, i16 (16 bit integer), i32 (32 bit integer), i64 (64 bit integer), double, string and containers list, set and map.
For example if we want to realize a thrift service that returns the details of an user with a specific “username”, we have to create a file with extension .thrift where we define the entity that represents the User and the service as follow:

struct UserTh {
       1:optional i64 idUser,
       2:string userName,
       3:string password,
       4:string name,
       5:string surname,
       6:string email,

service RetrieveUserInfoService {
       UserTh retrieveUser(1:string userName),

Taking this file as an input, the thrift engine generates the code to be used to create RPC client and server, hiding the serialization, deserialization and transport of data. In our example it will creates a class named UsertTh and a class named RetrieveUserInfoService that contain the stub necessary to create easily RPC client and server application.
For example, in order to build the server side we simply need to create an handler that implements the server stub (Iface) generated :

public class RetrieveUserInfoServiceHandler implements RetrieveUserInfoService.Iface {
	public  UserTh retrieveUser(String userName) {
		// application dependent implementation
		return  …;


This allow application developers to focus on application code without worrying about the sockets layer. In the next post we will explain better various aspects of the thrift services: transport protocol, service nature (blocking, non-blockng, sync async) and service security.

Add new comment

Filtered HTML

  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.