thrift clib Networking / RPC v1.0.0

Apache Thrift RPC framework. Define services once in IDL, generate clients and servers for 20+ languages. Multiple transports (Socket, Framed, HTTP) and protocols (Binary, Compact, JSON).

ppm install thrift

What is Apache Thrift?

Apache Thrift is a cross-language RPC framework originally developed at Facebook in 2007 and released as open source (later donated to the Apache Software Foundation). The core idea is simple: define your services and data types once in a language-neutral Interface Definition Language (.thrift IDL), then run the Thrift compiler to generate type-safe client and server code for any of 20+ supported languages — C++, Java, Python, Go, Ruby, PHP, Erlang, Haskell, and many more.

Thrift is used in production at Facebook, Evernote, HBase, Scribe, and Apache Cassandra. It is especially well-suited for high-performance internal microservices that need to communicate across language boundaries without sacrificing speed.

CLib package

Requires libthrift-dev. Ubuntu/Debian: sudo apt install libthrift-dev

Thrift IDL Example

Define your data types, exceptions, and services in a .thrift file:

namespace cpp tutorial

typedef i32 UserId

struct User {
  1: required UserId id,
  2: required string name,
  3: optional string email,
}

exception NotFoundException {
  1: string message,
}

service UserService {
  User getUser(1: UserId id) throws (1: NotFoundException ex),
  list<User> listUsers(),
  void createUser(1: User user),
}

Then generate code for any target language:

# Generate C++ client + server stubs
thrift --gen cpp user.thrift

# Generate Python
thrift --gen py user.thrift

# Generate Java
thrift --gen java user.thrift

Transports

Transports define how bytes move between client and server.

TransportDescription
SocketTCP socket — synchronous, most common for production services
FramedLength-prefixed frames over any transport; required for async/non-blocking servers
MemoryIn-memory buffer, useful for unit testing and struct serialization
HTTPHTTP POST transport for REST-like or firewall-friendly deployments
ZlibTransparent zlib compression wrapper over another transport

Protocols

Protocols define how data is serialized on the wire.

ProtocolDescription
BinaryCompact binary encoding — fastest, default choice for most services
CompactVariable-length integer encoding — approximately 30% smaller than Binary
JSONHuman-readable JSON — slowest but easiest to inspect and debug

Servers

ServerDescription
SimpleSingle-threaded — handles one client at a time; good for development
ThreadedSpawns one thread per client connection; simple concurrency model
ThreadPoolFixed worker thread pool — recommended for production workloads
NonBlockinglibevent-based async I/O — highest concurrency, lowest memory per connection

Thrift vs gRPC vs Protobuf

Framework comparison

gRPC uses HTTP/2 + Protocol Buffers (Protobuf) as its wire format. It has excellent browser support via gRPC-Web, built-in streaming, and a rich ecosystem of middleware. Thrift uses TCP directly with its own binary format. Thrift supports 20+ languages (often more than gRPC), offers multiple protocol options (Binary, Compact, JSON), and has simpler server setup without an HTTP/2 stack dependency. Protobuf alone is just a serialization library — add gRPC on top to get RPC semantics. Choose Thrift when you need maximum language coverage, multiple protocol options, or want to avoid the HTTP/2 dependency. Choose gRPC for browser support, HTTP/2 multiplexing, or tight Google Cloud integration.

Types

type
  TThriftTransport = Integer;  { transport handle }
  TThriftProtocol  = Integer;  { protocol handle }
  TThriftServer    = Integer;  { server handle }
  TThriftClient    = Integer;  { client connection handle }

  TThriftTransportType = (
    ttSocket  = 0,  { TCP socket }
    ttFramed  = 1,  { framed (required for non-blocking servers) }
    ttMemory  = 2,  { in-memory buffer }
    ttHTTP    = 3,  { HTTP POST }
    ttZlib    = 4   { zlib-compressed }
  );

  TThriftProtocolType = (
    tpBinary  = 0,  { fastest, default }
    tpCompact = 1,  { ~30% smaller than Binary }
    tpJSON    = 2   { human-readable }
  );

  TThriftServerType = (
    tsSimple     = 0,  { single-threaded }
    tsThreaded   = 1,  { one thread per client }
    tsThreadPool = 2,  { fixed pool — production }
    tsNonBlock   = 3   { libevent async }
  );

  TThriftFieldType = (
    tfBool   = 2,  tfByte   = 3,  tfDouble = 4,
    tfI16    = 6,  tfI32    = 8,  tfI64    = 10,
    tfString = 11, tfStruct = 12, tfMap    = 13,
    tfSet    = 14, tfList   = 15
  );

API Reference

Transport

function NewSocketTransport(Host: string; Port: Integer): TThriftTransport;
Create a TCP socket transport connecting to Host:Port.
function NewFramedTransport(T: TThriftTransport): TThriftTransport;
Wrap an existing transport with length-prefixed framing. Required when using non-blocking servers.
function NewMemoryTransport: TThriftTransport;
Create an in-memory transport. Useful for testing and standalone struct serialization.
procedure OpenTransport(T: TThriftTransport);
Open the transport connection (connect to the remote endpoint).
procedure CloseTransport(T: TThriftTransport);
Close the transport connection.
procedure FreeTransport(T: TThriftTransport);
Release all resources associated with the transport handle.

Protocol

function NewProtocol(T: TThriftTransport; Proto: TThriftProtocolType): TThriftProtocol;
Create a protocol layer over an (already open) transport.
procedure FreeProtocol(P: TThriftProtocol);
Release the protocol handle. Does not close or free the underlying transport.

Write

procedure WriteMessageBegin(P: TThriftProtocol; Name: string; MsgType, SeqID: Integer);
Begin an RPC message envelope. MsgType: 1=call, 2=reply, 3=exception, 4=oneway. SeqID: sequence number for correlating async replies.
procedure WriteMessageEnd(P: TThriftProtocol);
Finalize and flush the message envelope.
procedure WriteStructBegin(P: TThriftProtocol; Name: string);
Begin a struct (maps to a Thrift IDL struct).
procedure WriteStructEnd(P: TThriftProtocol);
End the current struct.
procedure WriteFieldBegin(P: TThriftProtocol; Name: string; FieldType: TThriftFieldType; FieldID: Integer);
Begin writing a field. FieldID must match the IDL field number (1, 2, 3 …).
procedure WriteFieldEnd(P: TThriftProtocol);   procedure WriteFieldStop(P: TThriftProtocol);
WriteFieldEnd closes a field; WriteFieldStop writes the stop marker after the last field in a struct.
procedure WriteString(P, Value: string);  procedure WriteI32(P, Value: Integer);  procedure WriteI64(P, Value: Int64);
Write a scalar field value. Also available: WriteDouble, WriteBool.

Read

procedure ReadMessageBegin(P: TThriftProtocol; out Name: string; out MsgType, SeqID: Integer);
Read an incoming message header. Name is the method name, MsgType identifies the message kind.
procedure ReadMessageEnd(P: TThriftProtocol);
Consume the message end marker.
procedure ReadStructBegin(P: TThriftProtocol; out Name: string);  procedure ReadStructEnd(P: TThriftProtocol);
Begin and end reading a struct block.
procedure ReadFieldBegin(P: TThriftProtocol; out Name: string; out FieldType: TThriftFieldType; out FieldID: Integer);
Read the next field header. Check FieldType = tfStop (or FieldID = 0) to detect the stop marker.
procedure ReadFieldEnd(P: TThriftProtocol);
Consume the field end marker.
function ReadString(P): string;  function ReadI32(P): Integer;  function ReadI64(P): Int64;  function ReadDouble(P): Double;  function ReadBool(P): Boolean;
Read a scalar field value after calling ReadFieldBegin.

Serialization (without RPC)

function SerializeToMemory(BuildProc: string; Proto: TThriftProtocolType): string;
Serialize a struct to a binary blob using a memory transport. BuildProc is the name of the procedure that writes the struct fields. Returns the raw bytes as a string.
function DeserializeFromMemory(Data: string; Proto: TThriftProtocolType): TThriftProtocol;
Wrap a binary blob in a memory transport and return a protocol handle ready for reading. Useful for struct deserialization without a live connection.

Server

function NewServer(ServerType: TThriftServerType; Port: Integer; Proto: TThriftProtocolType; ProcessorID: string): TThriftServer;
Create a Thrift server. ProcessorID identifies the request routing callback for dispatching incoming RPC calls.
function NewThreadPoolServer(Port, Threads: Integer; Proto: TThriftProtocolType; ProcessorID: string): TThriftServer;
Create a thread pool server with exactly Threads worker threads. Recommended for production.
procedure Serve(Server: TThriftServer);
Begin accepting connections. Blocks until StopServer is called from another thread.
procedure StopServer(Server: TThriftServer);  procedure FreeServer(Server: TThriftServer);
Stop and release a running server.

Code Example — Client RPC Call

uses thrift;

var
  Transport: TThriftTransport;
  Protocol: TThriftProtocol;
  MsgName: string;
  MsgType, SeqID: Integer;

begin
  { Connect: socket wrapped in framing for async-compatible server }
  Transport := NewSocketTransport('localhost', 9090);
  Transport := NewFramedTransport(Transport);
  OpenTransport(Transport);

  Protocol := NewProtocol(Transport, tpBinary);

  { Send RPC call: getUser(id=42) }
  WriteMessageBegin(Protocol, 'getUser', 1, 1);
  WriteStructBegin(Protocol, 'getUser_args');
  WriteFieldBegin(Protocol, 'id', tfI32, 1);
  WriteI32(Protocol, 42);
  WriteFieldEnd(Protocol);
  WriteFieldStop(Protocol);
  WriteStructEnd(Protocol);
  WriteMessageEnd(Protocol);

  { Read response }
  ReadMessageBegin(Protocol, MsgName, MsgType, SeqID);
  { ... read response fields (struct fields for the User result) ... }
  ReadMessageEnd(Protocol);

  FreeProtocol(Protocol);
  CloseTransport(Transport);
  FreeTransport(Transport);
end.

Package Info

Namethrift
Version1.0.0
Typeclib
Requireslibthrift-dev
CategoryNetworking / RPC

Install

$ ppm install thrift
System dep (Ubuntu):
sudo apt install libthrift-dev

Used By

Facebook — Evernote — HBase
Scribe — Apache Cassandra

Types

  • TThriftTransport
  • TThriftProtocol
  • TThriftServer
  • TThriftClient
  • TThriftTransportType
  • TThriftProtocolType
  • TThriftServerType
  • TThriftFieldType

Key Functions

  • NewSocketTransport
  • NewFramedTransport
  • NewMemoryTransport
  • OpenTransport / CloseTransport
  • FreeTransport
  • NewProtocol / FreeProtocol
  • WriteMessageBegin/End
  • WriteStructBegin/End
  • WriteFieldBegin/End/Stop
  • WriteString / WriteI32
  • WriteI64 / WriteDouble
  • WriteBool
  • ReadMessageBegin/End
  • ReadStructBegin/End
  • ReadFieldBegin/End
  • ReadString / ReadI32
  • ReadI64 / ReadDouble / ReadBool
  • SerializeToMemory
  • DeserializeFromMemory
  • NewServer
  • NewThreadPoolServer
  • Serve / StopServer / FreeServer
  • GetLastError / ThriftVersion