BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

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

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.

Rate this Article

Adoption
Style

BT