Thrift includes a complete stack for creating clients and servers. The top part is generated code from the Thrift definition. From this file, the services generate client and processor code. In contrast to built-in types, created data structures are sent as result in generated code. The protocol and transport layer are part of the runtime library. With Thrift, it is possible to define a service and change the protocol and transport without recompiling the code. Besides the client part, Thrift includes server infrastructure to tie protocols and transports together, like blocking, non-blocking, and multi-threaded servers. The underlying I/O part of the stack is implemented differently for different languages. Thrift supports a number of protocols:
TBinaryProtocol – A straightforward binary format, simple, but not optimized for space efficiency. Faster to process than the text protocol but more difficult to debug.
TCompactProtocol – More compact binary format; typically more efficient to process as well
TSimpleJSONProtocol – A write-only protocol that cannot be parsed by Thrift because it drops metadata using JSON. Suitable for parsing by scripting languages.
The supported transports are:
TSimpleFileTransport – This transport writes to a file.
TFramedTransport – This transport is required when using a non-blocking server. It sends data in frames, where each frame is preceded by length information.
TMemoryTransport – Uses memory for I/O. The Java implementation uses a simple internally.
TSocket – Uses blocking socket I/O for transport.
TZlibTransport – Performs compression using zlib. Used in conjunction with another transport.
Thrift also provides a number of servers, which are
TNonblockingServer – A multi-threaded server using non-blocking I/O. TFramedTransport must be used with this server.
TSimpleServer – A single-threaded server using standard blocking I/O. Useful for testing.
TThreadedServer – A multi-threaded server using a thread per connection model and standard blocking I/O.
TThreadPoolServer – A multi-threaded server using a thread pool and standard blocking I/O.
Benefits
Some stated benefits of Thrift include:
Cross-language serialization with lower overhead than alternatives such as SOAP due to use of binary format.
No build dependencies or non-standard software. No mix of incompatible software licenses.
Creating a Thrift service
Thrift is written in C++, but can create code for a number of languages. To create a Thrift service, one has to write Thrift files that describe it, generate the code in the destination language, write some code to start the server, and call it from the client. Here is a code example of such a description file: enum PhoneType struct Phone service PhoneService
Thrift will generate the code out of this descriptive information. For instance, in Java, the PhoneType will be a simple enum inside the Phone class.