Skip to content

dennispronin/ThriftOverview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thrift brief summary:

Apache Thrift - is a multi-protocol RPC framework (open sourced by Facebook in 2007). It can generate language-specific client/server libraries based on .thrift file, which describes future API with Thrift IDL.

Main components:

  • Thrift Types - language-agnostic data types, which turn into data types of the specified language after code generation. Example: i32 and map<type1,type2> will become int and HashMap<type1,type2> in Java respectively. More info
  • Service - can be easily explained with OOP terms: Thrift Service is an interface with abstract methods. After code generation Service is implemented by server and called by client through stubs
  • .thrift file - describes Services and data structures
  • Thrift compiler - generates language-specific code with the following command thrift --gen {language} {filename}.thrift
  • Protocol - abstraction, which defines how data should be encoded/decoded. Some examples: TBinaryProtocol, TCompactProtocol, TJSONProtocol
  • Transport - abstraction, which defines how data should be transferred. Some examples: TSocket (blocking socket I/O), TFileTransport (read/write to file)
  • Processor - simple middle layer between input and output streams. Automatically generated by compiler

These components are used by both Client and Server. Note that client and server should use the same transport and protocol stack for communication.

Pros:

  • Consistency of communication between client and server based on rules described in .thrift file
  • Language-agnostic way of describing API
  • Less boilerplate thanks to library generation
  • Binary data exchange support, which leads to faster communication (compared to JSON)
  • Wide range of protocols supported (compared to gRPC+HTTP/2 and REST+HTTP/1.1)
  • Solution for both data format and the way to transfer it

Cons:

  • Doesn't suit public API's due to complexity unlike REST
  • Serialization/deserialization process is slightly slower than protobuf: Article, presentation
  • Serialized data is slightly bigger than protobuf
  • No data structures inheritance (unlike protobuf), as a result, polymorphism isn't possible. However, there is a Service inheritance
  • No method overloading - all Service methods should have unique name
  • Limited documentation
  • Aging technology, not active development. Newer projects tend to choose gRPC or OpenAPI

Alternatives:

  • gRPC with protobuf
  • REST with HTTP + OpenAPI (Swagger)
  • JSON-RPC

Code summary:

  1. Client requests Server to calculate Cargo's shippingCost. Server returns shippingCost or throws exceptions if the Cargo is too heavy or empty.
  2. Client checks the shippingCost. If it is not too high, Client sends Cargo to Server to execute delivery.
  3. Server imitates work by making pauses between changing DeliveryStatus. Client periodically requests DeliveryStatus of previously sent Cargo.
  4. If the Cargo's DeliveryStatus is DELIVERED, client clears its cache.
  5. Whole process repeats until the program is terminated.

Thrift features covered:

  • namespaces
  • base types
  • constants and enumerations
  • structs
  • containers
  • type definitions
  • services
  • service inheritance
  • exceptions

Command to generate java classes based on hello.thrift file: thrift --out GeneratedThrift/src/main/java --gen java GeneratedThrift/src/main/resources/hello.thrift

Very simple Protocol Benchmark:

Execution time of calling checkShippingCost(Cargo cargo) with 10_000_000 Products in Cargo:

TJSONProtocol TCompactProtocol TBinaryProtocol
7796 ms 1119 ms 1345 ms
8478 ms 1407 ms 1695 ms
7992 ms 1291 ms 1602 ms

About

Thrift overview and benchmark

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published