Skip to content

Apache Thrift

Apache Thrift is a Remote Procedure Call (RPC) framework developed by Facebook, designed for cross-language service development.^[600-developer-big-data-java-serializable.md] It functions as an interface definition language (IDL) and binary communication protocol that allows disparate systems to communicate efficiently, often categorized alongside other serialization solutions like Google's Protocol Buffers and Apache Avro.^[600-developer-big-data-java-serializable.md]

Architecture and Components

The Thrift stack consists of three primary layers that determine how data is formatted, transmitted, and processed.^[600-developer-big-data-java-serializable.md]

Protocol Layer (传输格式)

The protocol layer defines how data is encoded for transport. Thrift supports several protocols to balance compatibility and performance: * TBinaryProtocol: Encodes binary data in a fast, compact format. * TCompactProtocol: A more efficient binary encoding that typically results in smaller message sizes. * TJsonProtocol: Encodes data in JSON format for better interoperability with web technologies. * TSimpleJsonProtocol: A simplified JSON output format that is writable but not readable by Thrift (often used for debugging). * TDebugProtocol: A human-readable text format used for debugging purposes.^[600-developer-big-data-java-serializable.md]

Transport Layer (传输方式)

The transport layer manages the I/O channels for reading and writing data. Thrift offers various transport implementations depending on the use case: * TSocket: Standard blocking socket I/O. * TFramedTransport: Sends data in framed chunks (required for use with the non-blocking server). * TFileTransport: Writes data to a file. * TMemoryTransport: Uses memory (I/O streams) for data transfer, typically for in-memory processing.^[600-developer-big-data-java-serializable.md]

Server Model (服务模型)

Thrift provides several server implementations to handle concurrent client requests: * TSimpleServer: A simple single-threaded server, primarily used for testing. * TThreadPoolServer: A multi-threaded server using a standard thread pool. * TNonblockingServer: A multi-threaded server that uses non-blocking I/O (NIO). * THsHaServer (Half-Sync, Half-Async): Combines the thread pool and non-blocking I/O models for high efficiency.^[600-developer-big-data-java-serializable.md]

Sources

^[600-developer-big-data-java-serializable.md]