Featured image of post gRPC and Google Protobuf are NOT the same things

gRPC and Google Protobuf are NOT the same things

Clearing up the confusion some people have over Google Protobuf vs gRPC

I have heard more than a few people talk about these in conversation like they are the same thing..

They are not.. :)

But the names can make it sound like it perhaps..

One is a super-efficient data format, and the other is an RPC framework on steroids.

Let’s break it down like a pro…

or at least like someone who googled it once.

πŸ€– A Little History (Because Every Superhero Has an Origin Story)

Protocol Buffers (Protobuf):

Google’s brainchild, launched in 2008 (yep, that was the year of iPhone 3G, fun times).

Protobuf came into existence to solve the “how do we send data across services without writing custom parsers?” dilemma.

More details here.

gRPC:
Enter gRPC in 2015.

Built on HTTP/2 and Protobuf, gRPC was Google saying, “Let’s make microservices talk to each other like gossiping neighbors.”

More here.

πŸš€ What’s the Difference?

FeatureProtobufgRPC
PurposeData serializationRemote Procedure Calls (RPC)
TransportDoesn’t care about transportUses HTTP/2
LanguageLanguage-agnostic schemaMulti-language stubs
UsageSerialize/Deserialize dataCall methods on remote apps

So Protobuf is like the DJ making sure the music (data) plays smoothly, while gRPC is the party organizer getting people (services) talking.

πŸ› οΈ Code Time- See! Tottaly Different things :)

1. Python: Protobuf Serialization

1
2
3
4
5
6
7
8
9
import example_pb2

person = example_pb2.Person()
person.name = "John Doe"
person.id = 123
person.email = "john.doe@example.com"

serialized = person.SerializeToString()
print("Serialized:", serialized)

2. Python: gRPC Client

1
2
3
4
5
6
7
8
import grpc
import example_pb2
import example_pb2_grpc

channel = grpc.insecure_channel('localhost:50051')
stub = example_pb2_grpc.MyServiceStub(channel)
response = stub.MyMethod(example_pb2.MyRequest(name="Python Client"))
print(response.message)

3. C#: Protobuf Serialization

1
2
3
4
var person = new Person { Name = "Jane Doe", Id = 456, Email = "jane.doe@example.com" };
using var stream = new MemoryStream();
person.WriteTo(stream);
Console.WriteLine("Serialized: " + Convert.ToBase64String(stream.ToArray()));

4. C#: gRPC Client

1
2
3
4
var channel = GrpcChannel.ForAddress("http://localhost:50051");
var client = new MyService.MyServiceClient(channel);
var reply = client.MyMethod(new MyRequest { Name = "C# Client" });
Console.WriteLine(reply.Message);

5. Go: Protobuf Serialization

1
2
3
4
5
6
7
person := &example.Person{
    Name:  "Go Lang",
    Id:    789,
    Email: "go.lang@example.com",
}
data, _ := proto.Marshal(person)
fmt.Printf("Serialized: %x\n", data)

6. Go: gRPC Client

1
2
3
4
5
conn, _ := grpc.Dial("localhost:50051", grpc.WithInsecure())
defer conn.Close()
client := example.NewMyServiceClient(conn)
resp, _ := client.MyMethod(context.Background(), &example.MyRequest{Name: "Go Client"})
fmt.Println(resp.Message)

7. Python: Protobuf Schema Definition

1
2
3
4
5
6
syntax = "proto3";
message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;
}

8. Python: gRPC Service Definition

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
syntax = "proto3";
service MyService {
  rpc MyMethod(MyRequest) returns (MyResponse);
}
message MyRequest {
  string name = 1;
}
message MyResponse {
  string message = 1;
}

9. C#: gRPC Server Setup

1
2
3
4
5
public class MyServiceImpl : MyService.MyServiceBase {
    public override Task<MyResponse> MyMethod(MyRequest request, ServerCallContext context) {
        return Task.FromResult(new MyResponse { Message = $"Hello {request.Name}!" });
    }
}

10. Go: gRPC Server Setup

1
2
3
4
type server struct{}
func (s *server) MyMethod(ctx context.Context, req *example.MyRequest) (*example.MyResponse, error) {
    return &example.MyResponse{Message: "Hello " + req.Name}, nil
}

πŸ”‘ Key Ideas

ConceptExplanation
ProtobufEfficient binary serialization for data structures
gRPCRPC framework for inter-service communication
HTTP/2gRPC uses HTTP/2 for multiplexed communication
Cross-languageBoth work across multiple languages
PerformanceProtobuf is faster than JSON; gRPC adds RPC magic

πŸ“š References