<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gautham Suresh on Medium]]></title>
        <description><![CDATA[Stories by Gautham Suresh on Medium]]></description>
        <link>https://medium.com/@gautham.suresh09?source=rss-f5e0d0d94999------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*G6T3xsbjbq9szRL21WiEiQ@2x.jpeg</url>
            <title>Stories by Gautham Suresh on Medium</title>
            <link>https://medium.com/@gautham.suresh09?source=rss-f5e0d0d94999------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 11:10:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gautham.suresh09/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Protobuf, gRPC and Code Generation]]></title>
            <link>https://medium.com/@gautham.suresh09/code-generation-why-you-need-it-dba9169e28ef?source=rss-f5e0d0d94999------2</link>
            <guid isPermaLink="false">https://medium.com/p/dba9169e28ef</guid>
            <category><![CDATA[protobuf]]></category>
            <category><![CDATA[grpc]]></category>
            <category><![CDATA[code-generation]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Gautham Suresh]]></dc:creator>
            <pubDate>Mon, 30 Sep 2019 05:37:44 GMT</pubDate>
            <atom:updated>2019-10-01T15:03:41.167Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Protobuf, gRPC and Code Generation</strong></p><p>Have you heard of code generation? Unless you have worked on <a href="https://microservices.io/">microservices</a>, it’s unlikely that you know about code generation. Unlike in <a href="https://microservices.io/patterns/monolithic.html">monolithic</a> application, having multiple internal services that connect each other using API needs a standard way of sharing data. An approach for solving such a requirement is by having REST APIs with JSON or XML for serializing/ de-serializing data. But there are a lot of issues that arise doing so, starting from writing boilerplate code for both server and client-side services which also needs to check whether the data is of expected schema, performance issues. Of course, the client library needs to be re-written for each programming language we use.</p><p>Here, we are going to use <a href="https://developers.google.com/protocol-buffers/">Protocol Buffers</a>, a.k.a protobuf to serialize the data instead of JSON used in REST APIs. We are also going to use gRPC for communication between the services. If you don’t have much idea about gRPC, I would recommend you to go through its <a href="https://grpc.io/docs/quickstart/">official website</a>, which contains few really useful tutorials (even this blog is based on their getting started example). Now, coming back to our topic, we will go ahead by building a simple hello world application using gRPC and protobuf, mentioning three main advantages that you get by doing so :</p><ol><li>Code Generation</li><li>Language independence</li><li>Input validation</li></ol><p>We will be using examples from official grpc repositories for Server and Client code.</p><h3>What is Protobuf ?</h3><p>In layman’s terms, protobuf is a way of converting data from data structure in a program to a format capable of storing or transmission (data serialization) or vice-versa (data deserialization). It has been introduced as a replacement to XML in 2001. When protobuf is used along with grpc for communicating with services, it has found to be very useful and efficient.</p><blockquote>Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data — think XML, but smaller, faster, and simpler.</blockquote><h3>Setting up the environment</h3><p>Before going ahead, lets setup our environment first. Install <a href="https://www.python.org/downloads/">Python 3</a> and <a href="https://pip.pypa.io/en/stable/installing/">pip</a> for python package management if you don’t have those already. Also, install <a href="https://golang.org/doc/install">golang</a> compiler.</p><p>There are two packages you need to install now :</p><ol><li><strong>Protocol buffer compiler</strong></li></ol><p>Install the protobuf compiler (protoc), written in C++ for generating the code from .proto files.</p><p>You can install the compiler using the latest release from <a href="https://github.com/protocolbuffers/protobuf/releases/latest">https://github.com/protocolbuffers/protobuf/releases/latest</a></p><p>2. <strong>Protobuf runtime &amp; gRPC</strong></p><p>Now you need to install Protobuf runtime for each of the languages you wish to use protoc to generate code for. In this case, we are going to use Python and Go.</p><p>For Python :</p><pre>pip install grpcio protobuf</pre><p>For Go :</p><pre>go get -u google.golang.org/grpc<br>go get -u github.com/golang/protobuf/protoc-gen-go</pre><p>You can also follow detailed instructions at <a href="https://github.com/golang/protobuf/#installation">https://github.com/golang/protobuf/#installation</a> .</p><h3>Defining our proto</h3><p>Let’s define a simple protocol buffer for Hello world.</p><pre>syntax = &quot;proto3&quot;;</pre><pre>package helloworld;</pre><pre>// The greeting service definition.<br>service Greeter {<br>  // Sends a greeting<br>  rpc SayHello (HelloRequest) returns (HelloReply) {}<br>}</pre><pre>// The request message containing the user&#39;s name.<br>message HelloRequest {<br>  string name = 1;<br>}</pre><pre>// The response message containing the greetings<br>message HelloReply {<br>  string message = 1;<br>}</pre><p>Here, the syntax refers to the version of protobuf we are using, in this case we use version 3. This is required for ensuring backward compatibility with previous versions of protobuf. Message is one of the base objects in protobuf. Each Message can have multiple fields or components, in our case, it’s just Name for Request message. You can also see that field has a type, which in this case is string for Name.</p><h3>Generating the code</h3><p>Now, here comes the magic of using protobuf. You can run a single command and get your server &amp; client <strong>code auto-generated</strong>.</p><pre>python -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. helloworld.proto</pre><p>You can see files generated in same folder</p><pre>helloworld_pb2.py<br>helloworld_pb2_grpc.py</pre><p>If you want to generate code for Go (optional since we fetch client proto from grpc examples), run the following code:</p><pre>protoc --go_out=. ./helloworld.proto</pre><p>This means anyone who wants to consume the service that you build can just import the auto-generated client code and start using it, saving a lot of time for you as well as them. Using auto-generated code also makes sure the client sends the request that the server expects including the data type if the request contains any attributes. Also, you might have noticed something while executing the compiler, it automatically generated code for Python as well as Go. This is where <strong>Language Independence</strong> comes into the picture. Having <em>proto</em> to define the <em>schema</em> of your API means you can generate server and client code in any of the supported languages and start from there. This saves a lot of time while building the application as well as while making changes. Also, even if you want to change your server or client written in python to Go, that’s easily possible now, thanks to protobuf. Also, the services that use your API have the independence of writing code in any language, as long as client code for that language is generated.</p><h3>Using the generated code</h3><p>Let’s create a server in Python and client in Go using the generated code. An example server written in Python is available in <a href="https://github.com/grpc/grpc/tree/master/examples/python/helloworld">grpc GitHub repository</a>. You can start the server using the following code :</p><pre>python greeter_server.py</pre><p>This will start the grpc server on <em>localhost</em> port <em>50051</em>. Now, let’s run a client that connects to this server for greeting message. The repository you cloned for installing grpc already has code for Go client.</p><pre>cd GOPATH/src/google.golang.org/grpc/examples/helloworld/greeter_client</pre><pre># Run the client<br>go run main.go</pre><p>This gives the output :</p><pre>2019/09/29 14:35:40 Greeting: Hello, world!</pre><p>One of the issues I always found while writing JSON REST API is with <strong>input validation</strong>. At both server as well as client-side, there’s a need to know whether you are sending and receiving data with the expected <em>schema</em>. This includes whether the structure of inputs is correct, all the required fields are there and if the data types are as expected. Yes, if you’re asking whether protobuf solves this, of course, it does. Basic validations such as <em>optional</em>, <em>required</em>, <em>repeated fields</em>, <em>data type checks</em> are all covered by protobuf. Of course, you could do this in JSON by using something like <a href="https://json-schema.org/">JSON schema,</a> but it doesn’t offer the easiness and visibility that protobuf provides.</p><p>Try changing your Go client to send an <em>integer</em> instead of <em>string</em> for the <em>name </em>field. Executing the client will give you the following error:</p><pre>$ go run main.go<br>./main.go:50:45: cannot use name (type int) as type string in field value</pre><p>That’s some useful feedback that the client is getting from generated code.</p><h3>Conclusion</h3><p>The combination of JSON with REST APIs has been awesome in the monolithic world. But, coming to the world of microservices, it seems like Protobuf with gRPC or similar would be a good alternative to look out for. An alternative that you can check for apart from Protobuf is <a href="https://thrift.apache.org/">Apache Thrift</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dba9169e28ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>