Apache Thrift

{{Short description|Open-source remote procedure call framework}}

{{Use mdy dates|date=June 2024}}{{Multiple issues|

{{Promotional|date=August 2019}}

{{how-to|date=August 2019}}

}}

{{Infobox software

| name = Apache Thrift

| logo = Apache Thrift logo.svg

| screenshot =

| caption =

| collapsible =

| author = Facebook, Inc.

| developer = Apache Software Foundation

| latest_release_version = 0.21.0

| latest_release_date = {{Start date and age|2024|09|09|df=yes}}{{cite web|url=https://thrift.apache.org/download|title=Apache Thrift - Downloads|access-date=27 September 2024}}

| latest_preview_version =

| latest_preview_date =

| operating_system =

| repo = {{URL|https://github.com/apache/thrift|Thrift repository}}

| programming_language = ActionScript, C, C#, C++, D, Dart, Delphi, Erlang, Go, Haskell, Haxe, Java, JavaScript, Node.js, OCaml, Perl, PHP, Python, Rust, Scala, Smalltalk

| genre = Remote procedure call framework

| license = Apache 2.0

| website = {{URL|//thrift.apache.org/}}

}}

Thrift is an IDL (Interface Definition Language) and binary communication protocol{{cite web |title=Installing and using Apache Cassandra With Java Part 4 (Thrift Client) |url=http://www.sodeso.nl/?p=251 |url-status=live |archive-url=https://web.archive.org/web/20220331115308/http://www.sodeso.nl/?p=251 |archive-date=March 31, 2022 |access-date=2011-03-30 |publisher=Sodeso – Software Development Solutions |quote=Thrift is a separate Apache project which is a binary communication protocol}} used for defining and creating services for programming languages.{{cite web |author=Prunicki |first=Andrew |title=Apache Thrift: Introduction |url=http://jnb.ociweb.com/jnb/jnbJun2009.html |url-status=dead |archive-url=https://web.archive.org/web/20110723051326/http://jnb.ociweb.com/jnb/jnbJun2009.html |archive-date=2011-07-23 |access-date=2011-04-11 |website=Object Computing |quote=Using code generation, Thrift creates a set of files which can then be used for creating clients and/or servers.}} It was developed by Facebook. Since 2020, it is an open source project in the Apache Software Foundation.

It uses a remote procedure call (RPC) framework and combines a software stack with a code generation engine to build cross-platform services. Thrift can connect applications written in a variety of languages and frameworks, including ActionScript, C, C++,{{Cite web |title=ThriftRequirements |url=https://cwiki.apache.org/confluence/display/thrift/ThriftRequirements |url-status=unfit |archive-url=https://web.archive.org/web/20240226144131/https://cwiki.apache.org/confluence/display/THRIFT/ThriftRequirements |archive-date=February 26, 2024 |access-date=2024-06-22 |website=Apache Software Foundation}} C#,Fred Potter, [http://blog.parallel48.com/using-thrift-with-cappuccino Using Thrift with Cappuccino] {{webarchive|url=https://web.archive.org/web/20110812154611/http://blog.parallel48.com/using-thrift-with-cappuccino |date=2011-08-12 }}, parallel48's posterously luscious blog, 10 June 2010. Cocoa, Delphi, Erlang, Go, Haskell, Java, JavaScript, Objective-C, OCaml, Perl, PHP, Python, Ruby, Elixir, Rust, Scala, Smalltalk, and Swift.{{cite web |author=Andrew Prunicki |title=Apache Thrift: Code Generation |url=http://jnb.ociweb.com/jnb/jnbJun2009.html |url-status=dead |archive-url=https://web.archive.org/web/20110723051326/http://jnb.ociweb.com/jnb/jnbJun2009.html |archive-date=2011-07-23 |access-date=2011-04-12 |website=Object Computing}} The implementation was described in an April 2007 technical paper released by Facebook, now hosted on Apache.Mark Slee, Aditya Agarwal, Marc Kwiatkowski, [http://thrift.apache.org/static/files/thrift-20070401.pdf Thrift: Scalable Cross-Language Services Implementation]{{Cite web |date=July 11, 2019 |title=LibraryFeatures |url=https://wiki.apache.org/thrift/LibraryFeatures?action=show&redirect=LanguageSupport |url-status=live |archive-url=https://web.archive.org/web/20230503083525/https://cwiki.apache.org/confluence/display/thrift/LibraryFeatures?action=show&redirect=LanguageSupport |archive-date=May 3, 2023 |access-date=2016-04-21 |website=Apache Software Foundation}}

Architecture

File:Apache Thrift architecture.png

Thrift includes a complete stack for creating clients and servers.{{cite web |author=Andrew Prunicki |title=Apache Thrift: Introduction |url=http://jnb.ociweb.com/jnb/jnbJun2009.html |url-status=dead |archive-url=https://web.archive.org/web/20110723051326/http://jnb.ociweb.com/jnb/jnbJun2009.html |archive-date=2011-07-23 |access-date=2011-04-11 |website=Object Computing}} The top part is generated code from the Thrift definition. From this file, the services generate client and processor codes. In contrast to built-in types, created data structures are sent as a result of 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 such as blocking, non-blocking, and multi-threaded servers. The underlying I/O part of the stack is implemented differently for different programming languages.

Thrift supports a number of protocols:

  • TBinaryProtocol – A binary format not optimized for space efficiency. Faster to process than the text protocol.
  • TCompactProtocol – More compact binary format.
  • TJSONProtocol – Uses JSON for the encoding of data.
  • TSimpleJSONProtocol – A write-only protocol that cannot be parsed by Thrift because it drops metadata using JSON. Suitable for parsing by scripting languages.{{cite web |last1=Skelton |first1=Steven |date=3 August 2013 |title=Developer Friendly Thrift Request Logging |url=http://stevenskelton.ca/developer-friendly-thrift-request-logging/ |url-status=live |archive-url=https://web.archive.org/web/20240124062123/https://www.stevenskelton.ca/developer-friendly-thrift-request-logging/ |archive-date=January 24, 2024 |access-date=3 July 2014 |website=stevenskelton.ca}}

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, each preceded by length information.
  • TMemoryTransport – Uses memory for I/O. The Java implementation uses a simple {{Javadoc:SE|java/io|ByteArrayOutputStream}} 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 (Java implementation uses NIO channels). TFramedTransport must be used with this server.
  • TSimpleServer – A single-threaded server using standard blocking I/O.
  • 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.

Thrift is written in C++, but can create code for some other programming 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. An example of such a description file:

enum PhoneType {

HOME,

WORK,

MOBILE,

OTHER

}

struct Phone {

1: i32 id,

2: string number,

3: PhoneType type

}

service PhoneService {

Phone findById(1: i32 id),

list findAll()

}

Thrift will generate the code out of this descriptive information. For instance, in Java, the PhoneType will be enum inside the Phone class.

See also

References

{{Reflist|30em}}