This content originally appeared on DEV Community and was authored by Yash-007
Most of us start building APIs with REST. It’s easy, works well in browsers and is simple to test using tools like postman or curl. you send some JSON, get a response that’s it.
But recently I came across gRPC. At first, it seemed complicated. but once I understood the basics, it started to make sense. This post is just a quick summary of what I learned not a deep dive, just something simple to help others get started.
First, What is RPC?
before understanding gRPC, let’s look at RPC (Remote Procedure Call).
RPC is when one program calls a function that runs on another machine, like it’s calling a local function. You don’t worry about the network or HTTP stuff just call a method, pass some data, and get back a result.
It’s like saying:
“Run this function somewhere else and send me the result.”
that makes it a good fit for when services need to talk to each other often.
gRPC is an RPC framework
REST – Easy, Familiar, Works Everywhere
REST is the most common way to build APIs. It uses HTTP/1.1 and sends data using JSON. JSON is text-based, easy to read, and works with browsers, mobile apps, and tools.
Why REST is popular:
- easy to read and write
- simple to test (Postman, curl, browser)
- great for frontend-backend connections
- If your API is going to be used by web or mobile apps. REST is the obvious choice.
gRPC – Some Setup, But Super Fast
gRPC is a better choice when your own backend services need to talk to each other like microservices or internal APIs.
Instead of JSON, gRPC uses a smaller binary format called protobuf. it also uses HTTP/2, which allows faster, multiple connections and streaming.
It made backend communication really fast and clean and used a lot by companies with big systems, where many services need to talk to each other quickly.
How Does gRPC Work?
Step 1: write a .proto file
syntax = "proto3";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
Step 2: Generate code
You run a command to generate client and server code from the proto file.
Step 3: Use that code
Server code (in Go):
func (s *MyGreeterServer) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + req.Name}, nil
}
Client code:
conn, _ := grpc.Dial("localhost:50051", grpc.WithInsecure())
client := pb.NewGreeterClient(conn)
res, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: "Yash"})
fmt.Println(res.Message) // "Hello Yash"
You call a function like normal, and gRPC handles everything behind the scenes.
When to Use What?
If your service talks to browsers or mobile apps → REST is easier.
If your services talk to each other a lot → gRPC is faster and uses less data.
for example, a frontend app calling your API → REST.
two backend services sending data to each other all day → gRPC.
This content originally appeared on DEV Community and was authored by Yash-007