R. C. Howell
R. C. Howell

Reputation: 1081

gRPC serialization behavior for local vs remote connections

In the gRPC Java implementation, are objects serialized when sent from a gRPC service with a local connection in the same process?

Suppose we have one java process with two classes A and B. If class A uses a gRPC client to send data to a gRPC server backed by class B, do the client and server share a Java object reference, or is the data serialized and sent over the established local Netty connection?

Upvotes: 0

Views: 874

Answers (1)

Eric Anderson
Eric Anderson

Reputation: 26434

When using the in-process transport, Protobuf serialization/deserialization will generally be avoided.

The Protobuf marshaller in gRPC generates an InputStream when serializing a message. When using the in-process transport, that InputStream is passed back to the Protobuf marshaller to deserialize a message. The Protobuf marshaller implements the InputStream directly and only serializes the message when bytes are actually read from the stream. When deserializing, the marshaller checks whether it generated the InputStream and, if so, grabs the message directly and returns it without ever converting the message to bytes. This is safe because Protobuf objects in Java are immutable.

This approach is unique to grpc-java with the in-process transport. Most other implementations require serializing to bytes and use mutable protocol buffers. grpc-netty encodes to HTTP/2, so always serializes to bytes.

Upvotes: 1

Related Questions