What is RPC framework and Apache Thrift?
Categories:
Understanding RPC Frameworks and Apache Thrift

Explore the fundamentals of Remote Procedure Call (RPC) frameworks and dive deep into Apache Thrift, a powerful tool for building cross-language services.
In distributed systems, different components often need to communicate with each other across networks. Remote Procedure Call (RPC) frameworks provide a way for a program to request a service from a program located on another computer on a network without having to understand the network's details. This article will demystify RPC, explain its core concepts, and then focus on Apache Thrift, a widely used RPC framework.
What is an RPC Framework?
An RPC framework simplifies inter-process communication by allowing a client program to execute a procedure (or function) in a remote server program as if it were a local procedure. The client doesn't need to know the underlying network protocols, data serialization, or server location. The RPC framework handles all these complexities, making distributed programming feel more like local programming.
sequenceDiagram participant Client participant RPCStub participant Network participant RPCServer participant ServerLogic Client->>RPCStub: Call remote_function(args) RPCStub->>RPCStub: Serialize args RPCStub->>Network: Send serialized request Network->>RPCServer: Receive serialized request RPCServer->>RPCServer: Deserialize args RPCServer->>ServerLogic: Call remote_function(args) ServerLogic-->>RPCServer: Return result RPCServer->>RPCServer: Serialize result RPCServer->>Network: Send serialized response Network->>RPCStub: Receive serialized response RPCStub->>RPCStub: Deserialize result RPCStub-->>Client: Return result
Simplified RPC Communication Flow
Key components of an RPC framework typically include:
- Interface Definition Language (IDL): A language-agnostic way to define the services and data types.
- Code Generator: Takes the IDL definition and generates client-side 'stubs' and server-side 'skeletons' in various programming languages.
- Serialization/Deserialization: Mechanisms to convert data structures into a format suitable for network transmission and vice-versa.
- Transport Layer: Handles the actual network communication (e.g., TCP, HTTP).
- Protocol Layer: Defines how messages are structured over the transport.
Introducing Apache Thrift
Apache Thrift is a powerful, cross-language RPC framework developed at Facebook for 'scalable cross-language services development'. It combines a software stack with a code generation engine to build services that can communicate efficiently between different programming languages. Thrift supports a wide range of languages, including C++, Java, Python, PHP, Ruby, C#, JavaScript, Go, and more.
The core of Thrift lies in its IDL, which allows developers to define data types and service interfaces in a single, language-neutral file. From this .thrift
file, Thrift's code generator creates the necessary code for both client and server applications in the target languages. This generated code handles all the boilerplate for serialization, deserialization, and network communication, allowing developers to focus on the business logic.
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
struct Work {
1: i32 num1,
2: i32 num2,
3: Operation op,
4: optional string comment
}
exception InvalidOperation {
1: i32 what,
2: string why
}
service Calculator {
void ping(),
i32 add(1: i32 num1, 2: i32 num2),
i32 calculate(1: i32 logid, 2: Work w) throws (1: InvalidOperation o),
void zip()
}
.thrift
file.How Apache Thrift Works
The workflow with Apache Thrift typically involves these steps:
- Define Service and Data Types: Write a
.thrift
file to define your service interface and data structures using Thrift's IDL. - Generate Code: Use the Thrift compiler to generate client and server code in your desired programming languages from the
.thrift
file. - Implement Server: Write the server-side logic by implementing the generated service interface.
- Implement Client: Write the client-side application that uses the generated client stub to call remote procedures.
- Run Services: Start the Thrift server, and then run the client to communicate with it.
flowchart TD A[Define .thrift IDL] --> B{Thrift Compiler} B --> C[Generated Client Code (e.g., Java)] B --> D[Generated Server Code (e.g., Python)] C --> E[Client Application (Java)] D --> F[Server Implementation (Python)] E -- RPC Call --> F
Apache Thrift Development Workflow
Thrift offers various transport protocols (e.g., TServerSocket
, TFileTransport
, THttpClient
) and serialization protocols (e.g., TBinaryProtocol
, TCompactProtocol
, TJSONProtocol
) to optimize for different use cases, balancing between performance and human readability. This flexibility allows developers to choose the best combination for their specific needs.
TCompactProtocol
is often preferred for high-performance scenarios due to its efficient byte-packing, resulting in smaller message sizes and faster transmission compared to TBinaryProtocol
or TJSONProtocol
.