A very basic comparison/differences between gRPC and REST

Summary
gRPC and REST are two different popular protocols that can be used to access web services. Both are used to send and receive data over a network, but they have different methods of doing so. In this article, I will try to explain the differences between REST and gRPC to help you decide the best one for your project. At the end of the article, you will also find a basic “Hello world” service that is written by using gRPC and REST.

gRPC (gRPC Remote Procedure Call) is a open source protocol developed by Google. It allows a client to call a function on a server. The client sends a message to a server over an HTTP/2 connection and the server returns a response. This protocol uses compressed data to send data faster over the network and supports multiple language support.

REST (Representational State Transfer) API is a protocol used to access a web service’s data or processing. It works over HTTP and uses HTTP methods (such as GET, POST, PUT, and DELETE) to send and receive data. REST API generally supports CRUD (Create, Read, Update, Delete) operations and sends data in JSON or XML format.
Differences:
One of the main differences between gRPC and REST is their method of sending data. gRPC uses compressed data to send data faster, while REST sends data slower and uses HTTP methods to send and receive data. Additionally, gRPC has broader language support, and is easier to establish an agreement between the two sides, but REST is more widespread and is commonly used by web applications.
Here are some key differences between gRPC and REST:
- Data transmission: gRPC uses compressed data to send data faster over the network(It uses Protocol Buffers, a binary serialization format, to encode data before sending it over the network), while REST sends data(in JSON or XML format, which are text-based formats that are not as efficient as binary formats like Protocol Buffers.) slower and uses HTTP methods to send and receive data.
- Language support: gRPC has broader language support and is easier to establish agreement between the two sides, but REST is more widespread and is commonly used by web applications.
- Compatibility: REST is more compatible with older web browsers and devices, so it can reach a wider user base.
- CRUD operations: REST API generally supports CRUD (Create, Read, Update, Delete) operations, while gRPC does not have built-in support for these operations.
- Data format: REST sends data in JSON or XML format, while gRPC uses Protocol Buffers (a binary serialization format) by default.
Ultimately, which protocol you use depends on the needs of your application. For example, gRPC may be more suitable for an application that requires fast data transmission, but REST is more common and may be easier to understand and use for a web application. Both protocols have their own advantages. For example, for a mobile application, data transmission speed may be important and in that case gRPC may be more suitable. But for a web application, REST may be more widely used and easier to understand.
Let’s create the same “Hello World” service in both gRPC and REST API:
Basic gRPC example:
This gRPC service(HelloService) takes a request from a client and returns a response.
public class HelloService : Hello.HelloBase
{
public override Task<HelloResponse> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloResponse { Message = "Hello " + request.Name });
}
}Then, this service can be run on a gRPC server using the following server-side code:
var server = new Server
{
Services = { Hello.BindService(new HelloService()) },
Ports = { new ServerPort("localhost", 5000, ServerCredentials.Insecure) }
};
server.Start();
server.ShutdownAsync().Wait();This server listens for requests sent to “localhost” on port 5000.
Then, a gRPC client can be created to call this service.
var channel = GrpcChannel.ForAddress("https://localhost:5000");
var client = new HelloClient(channel);
var response = client.SayHello(new HelloRequest { Name = "World" });
console.WriteLine(response.Message);This client sends a “Hello World” message to the “HelloService” service and receives a response.
Basic REST API Example:
This REST API takes an HTTP request and returns a response.
[ApiController]
public class HelloController : ControllerBase
{
[HttpGet]
public string Get()
{
return "Hello World!";
}
}This class takes an HTTP GET request and returns a “Hello World!” message.
Then, this API can be run in an ASP.NET Core application with that basic configuration.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}This application takes an HTTP request and returns a response using the “HelloController” class.
Then, you can call this API using a web browser or a REST client. For example, you can go to the “http://localhost:5000/hello" address in a web browser to call this API and view the “Hello World!” message.
I hope these examples have been helpful and have helped you better understand how to use the gRPC and REST protocols in .NET.
Thanks for reading!
Sources:
gRPC Learn more gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any…grpc.io
https://blog.dreamfactory.com/grpc-vs-rest-how-does-grpc-compare-with-traditional-rest-apis/