Best way to send binary data with Thrift

DanilaNV picture DanilaNV · Dec 14, 2012 · Viewed 13.6k times · Source

I have a structure in c++ which stores bytes like this:

struct RemoteData 
{
    /// some other fields here

    unsigned char* buf;
    int bufLen;     
};

And I need to send this data to remote service, written in C++, via thrift. I found three ways how to map this structure to thrift idl:

  1. Using container types like this:

    struct RemoteData 
    {
        1: list<BYTE> buf,
        ...
    }
    
  2. Using binary type:

    struct RemoteData 
    {
        1: binary buf,
        ...
    }
    
  3. Storing data in string type:

    struct RemoteData 
    {
        1: string buf,
        ...
    }
    

What is the best way?

Answer

Wildfire picture Wildfire · Dec 14, 2012

The value contained in thrift string type must be UTF8-encoded, otherwise some client won't be able to read it (Java thrift client for example).

Thrift list<byte> type will be converted into std::vector<int8_t> in c++, but in other languages it will be not so good (for example, in java it will be compiled into suboptimal List<Byte>.

The binary type is the best choice in terms of interoperability with clients in other languages and the correctness of protocol definition.

Since all 3 definitions produce messages of roughly the same size, I'd go with binary: even if you don't need interoperability with other languages now, you may need it in future.