Backend Development 12 min read

Understanding Protobuf Serialization in Dubbo with Protostuff

The article explains how Dubbo can use the Protostuff library as a drop‑in replacement for .proto files to perform Protobuf TLV serialization, detailing the wire‑type encoding, Maven setup, POJO example, byte‑level analysis, Dubbo configuration, and tips for reducing serialization overhead.

DeWu Technology
DeWu Technology
DeWu Technology
Understanding Protobuf Serialization in Dubbo with Protostuff

Protobuf is a widely used serialization protocol that Dubbo supports early on. This article introduces protobuf, its TLV (Tag‑Length‑Value) storage format, and how the Protostuff library can be used as a drop‑in replacement for .proto files.

Protobuf requires a .proto definition and a conversion tool (e.g., the official releases at https://github.com/protocolbuffers/protobuf/releases). For simplicity, the article adopts Protostuff, which generates identical binary structures to protobuf by using schemas to simplify custom definitions.

The TLV format encodes each field as fieldIndex << 3 | wire_type . The article lists common Java types and their corresponding wire_type values (int32/varint = 0, int64/varint = 0, string/length‑delimited = 2, double/64‑bit = 1).

Example Maven dependencies for Protostuff:

<dependency>
    <groupId>io.protostuff</groupId>
    <artifactId>protostuff-runtime</artifactId>
    <version>1.7.2</version>
</dependency>
<dependency>
    <groupId>io.protostuff</groupId>
    <artifactId>protostuff-core</artifactId>
    <version>1.7.2</version>
</dependency>

A simple POJO to be serialized:

public class WishRequest implements Serializable {
    private Integer age;
    private Long money;
    private String msg;
}

Test code that serializes the object with Protostuff:

public class ProtostuffTest {
    public static void main(String[] args) {
        Schema<WishRequest> schema = RuntimeSchema.getSchema(WishRequest.class);
        WishRequest wishRequest = new WishRequest();
        wishRequest.setAge(18);
        wishRequest.setMoney(1314L);
        wishRequest.setMsg("happy new year");
        LinkedBuffer buffer = LinkedBuffer.allocate(1024);
        byte[] data = ProtobufIOUtil.toByteArray(wishRequest, schema, buffer);
        System.out.println(Arrays.toString(data));
        System.out.println(data.length);
    }
}

The resulting byte array (21 bytes) is analysed against the TLV rules: the first tag 8 corresponds to field 1 (age) with wire_type=0 , the second byte 18 encodes the value 18, and so on. The third field (msg) uses wire_type=2 , producing tag 26 and a length byte of 14 , followed by the ASCII bytes of "happy new year".

Integration with Dubbo is demonstrated by adding the protostuff serializer to the Dubbo protocol configuration and defining a service interface:

public interface HelloService {
    String sayHappyNewYear(WishRequest wish);
}

<dubbo:protocol serialization="protostuff" name="dubbo" port="20880"/>

The consumer creates a WishRequest , invokes the remote method, and prints the result. Packet capture with Wireshark shows the serialized byte sequence embedded in the Dubbo payload.

Key Dubbo source snippets are examined, such as ExchangeCodec#encodeRequest and ProtostuffObjectOutput#writeObject , highlighting how Dubbo writes the protocol header, version, method name, and then delegates object serialization to Protostuff.

Important utility methods like WireFormat.makeTag (which implements the (fieldNumber << 3) | wireType logic) and WriteSink.writeVarInt32 (which encodes integers using 7‑bit groups with continuation bits) are shown.

Finally, the article concludes that protobuf/Protostuff provides a compact representation for objects, while Dubbo adds additional overhead for service metadata. Reducing class names, field definitions, and unnecessary parameters can further improve performance.

JavaDubboProtostuffSerializationnetworkProtobuf
DeWu Technology
Written by

DeWu Technology

A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.