BT

Facilitating the spread of knowledge and innovation in professional software development

Contribute

Topics

Choose your language

InfoQ Homepage News Thrift for Haskell Aims to Eliminate Bugs from RPC Code

Thrift for Haskell Aims to Eliminate Bugs from RPC Code

This item in japanese

Bookmarks

Originally created at Facebook and now part of Apache, Thrift is an interface definition language (IDL) and binary communication protocol aimed to enable efficient RPC at scale across services written in multiple languages. Facebook has recently open sourced hsthrift, which makes it possible to use Thrift in Haskell projects and take advantage of its dependent types to eliminate bugs in production.

Thrift provides a language-agnostic IDL that can be compiled into client and server RPC code.

The Haskell Thrift compiler generates the Haskell code needed to communicate with other Thrift services, and the included libraries allow you to build both Thrift clients and servers in Haskell.

With the open-sourcing of Thrift for Haskell, it is now possible to have Haskell projects communicate using Thrift with services written in other languages. The Haskell Thrift toolchain includes additional features not found in the standard Thrift toolchain. In particular, the Haskell Thrift compiler can prune the generated code to remove unused symbols prior to type checking; supports transforming the Thrift abstract syntax tree using an exactprint-inspired library; and includes a mechanism to specify type checking plugins to tailor the type checking rules to the target language.

One of the major promises of Thrift for Haskell is the possibility of eliminating bugs in production systems thanks to Haskell strong correctness guarantees. Specifically, Facebook claims that using Haskell dependent types they have been able to find and eliminate bugs in real-world code. Dependent types provide the means to express invariants about code at the type level, which the Haskell compiler checks. While Haskell is not truly a dependent type language, Thrift has provided the perfect playground for show the usefulness of this approach:

Deploying the Haskell Thrift compiler exposed many bugs in the existing C++ implementation which is used to generate Thrift code for languages other than Haskell. These involved accepting ill-typed inputs, infinite looping, mistaken coercions, and ambiguous behavior

It is important to take into account that Thrift for Haskell requires Facebook variant of Thrift, fbthrift, and is not compatible with Apache Thrift clients and servers.

Thrift supports a wide number of target languages, including C/C++, C#, Erlang, Go, Java, JavaScript and many more.

As a quick example of a Thrift interface, this is how you can define a simple multiplication service for Java and Python:

namespace java tutorial
namespace py tutorial

typedef i32 int
service MultiplicationService
{
        int multiply(1:int n1, 2:int n2),
}

Besides the IDL compiler, Thrift also provides a runtime library that implements several protocols and transport layers that can be used interchangeably without requiring the developer for recompile their code. For example, Thrift provides a TBinaryProtocol using a binary format to improve performance, as well as a TJSONProtocol using JSON for encoding data. Similarly, Thrift can use a file-based transport, blocking sockets, memory, and so on. Additionally, Thrift also includes a number of ready-to-use servers to make it easier to implement your service using different request-serving strategies, including a simple serial server; a multi-threaded, non-blocking server; a multi-threaded server using a thread pool, and so on.

Thrift is not the only available option for language-agnostic RPC. Most notably, Google open-sourced its gRPC framework, based on Protocol Buffers, with the same goal. gRPC is in use at many organizations including Square, Netflix, and others. For a comparison between different RPC code-generation solutions, check out Rethinking CodeGen: IDL, Thrift, gRPC, and Ohh My.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.