Post

What are Protocol Buffers?

Hey folks!

We would look into protocol buffers and some prerequisites in this article.

What is RPC?

RPCs are request-response protocols known as remote procedure calls. The client starts an RPC by sending a message to a known remote server asking it to run a procedure with the given parameters. The remote server sends a response to the client, and the application continues its process.

Now we know that there’s an exchange between two nodes or client and server to execute a remote procedure. To make an RPC we must know serialisation.

What is serialization?

Serialisation is the process of transforming a data structure or object state into a format that can be stored or communicated (for example, as data streams via computer networks) and later rebuilt. This is also commonly known as marshalling, encoding and more. There are many serialisation formats available like XML AND JSON.

JSON is very widely used human readable format for communication between microservices but is slow and bigger when it comes to huge payloads. Not only this, it also has both forward and backward compatibility issues.

Let’s deep dive into protocol buffers. Its a must know before getting into gRPC in next article.

What are Protocol Buffers?

To overcome issues with other serialisation techniques, Google introduced a language and platform independent technique called Protocol Buffers or Protobuf. It is a means of data serialization similar to XML and JSON, boasting exceptional efficiency and high-speed performance when transmitted across networks.

This allows you to define desired structure of data once and use the generated source code to easily read and write data from a variety data streams using a range of languages like Ruby, Python, GO, Java, Objective-C, Kotlin, C#, PHP and more.

Protocol buffers are a combination of:

  • definition language (created in .proto files),
  • code which proto compiler generates to interface with data,
  • language-specific runtime libraries,
  • and the serialization format for data written to a file (transmitted on a network connection).

Protocol buffer messages and services are described by engineer-authored .proto files.

A sample protobuf messages and service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
syntax = "proto3";

service UserService {
  rpc GetUser (UserID) returns (User) {}
}

message User {
  optional int32 id = 1;
  optional string name = 2;
  optional string email = 3;
}

message UserID {
  int32 id = 1;
}

Why should we use protocol buffers?

  • Faster: It is a binary format that does not require parsing like JSON. Because the payload size is smaller than those of other serialisation mechanisms, it is faster. It’s a no-brainer for internal communication and communicating with mobile devices on slow networks.
  • Schema: Encoding the semantics in the proto file ensures that no data is lost.
  • Cross-language interoperability: It can be used in a variety of languages, including Ruby, Python, GO, and others. If the mobile app is in Objective-C then you can implement in it, if backend is in Ruby you can again use it.
  • Backward and forward compatibilty: Makes it easier to rollout changes.

Cheers!

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.