11-18-2019, 07:01 PM
gRPC vs HTTP APIs
<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/11/grpc-vs-http-apis.png" width="58" height="58" title="" alt="" /></div><div><div class="row justify-content-center">
<div class="col-md-4">
<div><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/11/grpc-vs-http-apis.png" width="58" height="58" alt="Avatar" class="avatar avatar-58 wp-user-avatar wp-user-avatar-58 photo avatar-default"></p>
<p>James</p>
</div>
</div>
</div>
<div class="entry-meta">
<p>November 18th, 2019</p>
</p></div>
<p><!-- .entry-meta --> </p>
<p>ASP.NET Core now enables developers to build gRPC services. <a href="https://docs.microsoft.com/aspnet/core/grpc/">gRPC</a> is an opinionated contract-first remote procedure call framework, with a focus on performance and developer productivity. gRPC integrates with ASP.NET Core 3.0, so you can use your existing ASP.NET Core logging, configuration, authentication patterns to build new gRPC services.</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/11/grpc-vs-http-apis-1.png" alt width="300" height="176" class="aligncenter size-medium wp-image-22791"></p>
<p>This blog post compares gRPC to JSON HTTP APIs, discusses gRPC’s strengths and weaknesses, and when you could use gRPC to build your apps.</p>
<h2>gRPC strengths</h2>
<h3>Developer productivity</h3>
<p>With gRPC services, a client application can directly call methods on a server app on a different machine as if it was a local object. gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. The server implements this interface and runs a gRPC server to handle client calls. On the client, a strongly-typed gRPC client is available that provides the same methods as the server.</p>
<p>gRPC is able to achieve this through first-class support for code generation. A core file to gRPC development is the <a href="https://developers.google.com/protocol-buffers/docs/proto3"><em>.proto</em> file</a>, which defines the contract of gRPC services and messages using Protobuf interface definition language (IDL):</p>
<p><em>Greet.proto</em></p>
<pre><code class="protobuf">// The greeting service definition.
service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply);
} // The request message containing the user's name.
message HelloRequest { string name = 1;
} // The response message containing the greetings
message HelloReply { string message = 1;
}
</code></pre>
<p>Protobuf IDL is a language neutral syntax, so it can be shared between gRPC services and clients implemented in different languages. gRPC frameworks use the <em>.proto</em> file to code generate a service base class, messages, and a complete client. Using the generated strongly-typed Greeter client to call the service:</p>
<p><em>Program.cs</em></p>
<pre><code class="csharp">var channel = GrpcChannel.ForAddress("https://localhost:5001")
var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = "World" });
Console.WriteLine("Greeting: " + reply.Message);
</code></pre>
<p>By sharing the <em>.proto</em> file between the server and client, messages and client code can be generated from end to end. Code generation of the client eliminates duplication of messages on the client and server, and creates a strongly-typed client for you. Not having to write a client saves significant development time in applications with many services.</p>
<h3>Performance</h3>
<p>gRPC messages are serialized using <a href="https://developers.google.com/protocol-buffers/docs/overview">Protobuf</a>, an efficient binary message format. Protobuf serializes very quickly on the server and client. Protobuf serialization results in small message payloads, important in limited bandwidth scenarios like mobile apps.</p>
<p>gRPC requires HTTP/2, a major revision of HTTP that provides significant performance benefits over HTTP 1.x:</p>
<ul>
<li>Binary framing and compression. HTTP/2 protocol is compact and efficient both in sending and receiving.</li>
<li>Multiplexing of multiple HTTP/2 calls over a single TCP connection. Multiplexing eliminates <a href="https://en.wikipedia.org/wiki/Head-of-line_blocking">head-of-line blocking</a> at the application layer.</li>
</ul>
<h3>Real-time services</h3>
<p>HTTP/2 provides a foundation for long-lived, real-time communication streams. gRPC provides first-class support for streaming through HTTP/2.</p>
<p>A gRPC service supports all streaming combinations:</p>
<ul>
<li>Unary (no streaming)</li>
<li>Server to client streaming</li>
<li>Client to server streaming</li>
<li>Bidirectional streaming</li>
</ul>
<p>Note that the concept of broadcasting a message out to multiple connections doesn’t exist natively in gRPC. For example, in a chat room where new chat messages should be sent to all clients in the chat room, each gRPC call is required to individually stream new chat messages to the client. <a href="https://docs.microsoft.com/aspnet/core/signalr/introduction">SignalR</a> is a useful framework for this scenario. SignalR has the concept of persistent connections and built-in support for broadcasting messages.</p>
<h3>Deadline/timeouts and cancellation</h3>
<p>gRPC allows clients to specify how long they are willing to wait for an RPC to complete. The <a href="https://grpc.io/blog/deadlines">deadline</a> is sent to the server, and the server can decide what action to take if it exceeds the deadline. For example, the server might cancel in-progress gRPC/HTTP/database requests on timeout.</p>
<p>Propagating the deadline and cancellation through child gRPC calls helps enforce resource usage limits.</p>
<h2>gRPC weaknesses</h2>
<h3>Limited browser support</h3>
<p>gRPC has excellent cross-platform support! gRPC implementations are available for every programming language in common usage today. However one place you can’t call a gRPC service from is a browser. gRPC heavily uses HTTP/2 features and no browser provides the level of control required over web requests to support a gRPC client. For example, browsers do not allow a caller to require that HTTP/2 be used, or provide access to underlying HTTP/2 frames.</p>
<p><a href="https://grpc.io/docs/tutorials/basic/web.html">gRPC-Web</a> is an additional technology from the gRPC team that provides limited gRPC support in the browser. gRPC-Web consists of two parts: a JavaScript client that supports all modern browsers, and a gRPC-Web proxy on the server. The gRPC-Web client calls the proxy and the proxy will forward on the gRPC requests to the gRPC server.</p>
<p>Not all of gRPC’s features are supported by gRPC-Web. Client and bidirectional streaming isn’t supported, and there is limited support for server streaming.</p>
<h3>Not human readable</h3>
<p>HTTP API requests using JSON are sent as text and can be read and created by humans.</p>
<p>gRPC messages are encoded with Protobuf by default. While Protobuf is efficient to send and receive, its binary format isn’t human readable. Protobuf requires the message’s interface description specified in the <em>.proto</em> file to properly deserialize. Additional tooling is required to analyze Protobuf payloads on the wire and to compose requests by hand.</p>
<p>Features such as <a href="https://github.com/grpc/grpc/blob/master/doc/server-reflection.md">server reflection</a> and the <a href="https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md">gRPC command line tool</a> exist to assist with binary Protobuf messages. Also, Protobuf messages support <a href="https://developers.google.com/protocol-buffers/docs/proto3#json">conversion to and from JSON</a>. The built-in JSON conversion provides an efficient way to convert Protobuf messages to and from human readable form when debugging.</p>
<h2>gRPC recommended scenarios</h2>
<p>gRPC is well suited to the following scenarios:</p>
<ul>
<li><strong>Microservices</strong> – gRPC is designed for low latency and high throughput communication. gRPC is great for lightweight microservices where efficiency is critical.</li>
<li><strong>Point-to-point real-time communication</strong> – gRPC has excellent support for bidirectional streaming. gRPC services can push messages in real-time without polling.</li>
<li><strong>Polyglot environments</strong> – gRPC tooling supports all popular development languages, making gRPC a good choice for multi-language environments.</li>
<li><strong>Network constrained environments</strong> – gRPC messages are serialized with Protobuf, a lightweight message format. A gRPC message is always smaller than an equivalent JSON message.</li>
</ul>
<h2>Conclusion</h2>
<p>gRPC is a powerful new tool for ASP.NET Core developers. While gRPC is not a complete replacement for HTTP APIs, it offers improved productivity and performance benefits in some scenarios.</p>
<p>gRPC on ASP.NET Core is available now! If you are interested in learning more about gRPC, check out these resources:</p>
<div class="authorinfoarea">
<div><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/11/grpc-vs-http-apis-2.png" width="96" height="96" alt="Avatar" class="avatar avatar-96 wp-user-avatar wp-user-avatar-96 photo avatar-default"></div>
</p></div>
</div>
https://www.sickgaming.net/blog/2019/11/...http-apis/
<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/11/grpc-vs-http-apis.png" width="58" height="58" title="" alt="" /></div><div><div class="row justify-content-center">
<div class="col-md-4">
<div><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/11/grpc-vs-http-apis.png" width="58" height="58" alt="Avatar" class="avatar avatar-58 wp-user-avatar wp-user-avatar-58 photo avatar-default"></p>
<p>James</p>
</div>
</div>
</div>
<div class="entry-meta">
<p>November 18th, 2019</p>
</p></div>
<p><!-- .entry-meta --> </p>
<p>ASP.NET Core now enables developers to build gRPC services. <a href="https://docs.microsoft.com/aspnet/core/grpc/">gRPC</a> is an opinionated contract-first remote procedure call framework, with a focus on performance and developer productivity. gRPC integrates with ASP.NET Core 3.0, so you can use your existing ASP.NET Core logging, configuration, authentication patterns to build new gRPC services.</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/11/grpc-vs-http-apis-1.png" alt width="300" height="176" class="aligncenter size-medium wp-image-22791"></p>
<p>This blog post compares gRPC to JSON HTTP APIs, discusses gRPC’s strengths and weaknesses, and when you could use gRPC to build your apps.</p>
<h2>gRPC strengths</h2>
<h3>Developer productivity</h3>
<p>With gRPC services, a client application can directly call methods on a server app on a different machine as if it was a local object. gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. The server implements this interface and runs a gRPC server to handle client calls. On the client, a strongly-typed gRPC client is available that provides the same methods as the server.</p>
<p>gRPC is able to achieve this through first-class support for code generation. A core file to gRPC development is the <a href="https://developers.google.com/protocol-buffers/docs/proto3"><em>.proto</em> file</a>, which defines the contract of gRPC services and messages using Protobuf interface definition language (IDL):</p>
<p><em>Greet.proto</em></p>
<pre><code class="protobuf">// The greeting service definition.
service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply);
} // The request message containing the user's name.
message HelloRequest { string name = 1;
} // The response message containing the greetings
message HelloReply { string message = 1;
}
</code></pre>
<p>Protobuf IDL is a language neutral syntax, so it can be shared between gRPC services and clients implemented in different languages. gRPC frameworks use the <em>.proto</em> file to code generate a service base class, messages, and a complete client. Using the generated strongly-typed Greeter client to call the service:</p>
<p><em>Program.cs</em></p>
<pre><code class="csharp">var channel = GrpcChannel.ForAddress("https://localhost:5001")
var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = "World" });
Console.WriteLine("Greeting: " + reply.Message);
</code></pre>
<p>By sharing the <em>.proto</em> file between the server and client, messages and client code can be generated from end to end. Code generation of the client eliminates duplication of messages on the client and server, and creates a strongly-typed client for you. Not having to write a client saves significant development time in applications with many services.</p>
<h3>Performance</h3>
<p>gRPC messages are serialized using <a href="https://developers.google.com/protocol-buffers/docs/overview">Protobuf</a>, an efficient binary message format. Protobuf serializes very quickly on the server and client. Protobuf serialization results in small message payloads, important in limited bandwidth scenarios like mobile apps.</p>
<p>gRPC requires HTTP/2, a major revision of HTTP that provides significant performance benefits over HTTP 1.x:</p>
<ul>
<li>Binary framing and compression. HTTP/2 protocol is compact and efficient both in sending and receiving.</li>
<li>Multiplexing of multiple HTTP/2 calls over a single TCP connection. Multiplexing eliminates <a href="https://en.wikipedia.org/wiki/Head-of-line_blocking">head-of-line blocking</a> at the application layer.</li>
</ul>
<h3>Real-time services</h3>
<p>HTTP/2 provides a foundation for long-lived, real-time communication streams. gRPC provides first-class support for streaming through HTTP/2.</p>
<p>A gRPC service supports all streaming combinations:</p>
<ul>
<li>Unary (no streaming)</li>
<li>Server to client streaming</li>
<li>Client to server streaming</li>
<li>Bidirectional streaming</li>
</ul>
<p>Note that the concept of broadcasting a message out to multiple connections doesn’t exist natively in gRPC. For example, in a chat room where new chat messages should be sent to all clients in the chat room, each gRPC call is required to individually stream new chat messages to the client. <a href="https://docs.microsoft.com/aspnet/core/signalr/introduction">SignalR</a> is a useful framework for this scenario. SignalR has the concept of persistent connections and built-in support for broadcasting messages.</p>
<h3>Deadline/timeouts and cancellation</h3>
<p>gRPC allows clients to specify how long they are willing to wait for an RPC to complete. The <a href="https://grpc.io/blog/deadlines">deadline</a> is sent to the server, and the server can decide what action to take if it exceeds the deadline. For example, the server might cancel in-progress gRPC/HTTP/database requests on timeout.</p>
<p>Propagating the deadline and cancellation through child gRPC calls helps enforce resource usage limits.</p>
<h2>gRPC weaknesses</h2>
<h3>Limited browser support</h3>
<p>gRPC has excellent cross-platform support! gRPC implementations are available for every programming language in common usage today. However one place you can’t call a gRPC service from is a browser. gRPC heavily uses HTTP/2 features and no browser provides the level of control required over web requests to support a gRPC client. For example, browsers do not allow a caller to require that HTTP/2 be used, or provide access to underlying HTTP/2 frames.</p>
<p><a href="https://grpc.io/docs/tutorials/basic/web.html">gRPC-Web</a> is an additional technology from the gRPC team that provides limited gRPC support in the browser. gRPC-Web consists of two parts: a JavaScript client that supports all modern browsers, and a gRPC-Web proxy on the server. The gRPC-Web client calls the proxy and the proxy will forward on the gRPC requests to the gRPC server.</p>
<p>Not all of gRPC’s features are supported by gRPC-Web. Client and bidirectional streaming isn’t supported, and there is limited support for server streaming.</p>
<h3>Not human readable</h3>
<p>HTTP API requests using JSON are sent as text and can be read and created by humans.</p>
<p>gRPC messages are encoded with Protobuf by default. While Protobuf is efficient to send and receive, its binary format isn’t human readable. Protobuf requires the message’s interface description specified in the <em>.proto</em> file to properly deserialize. Additional tooling is required to analyze Protobuf payloads on the wire and to compose requests by hand.</p>
<p>Features such as <a href="https://github.com/grpc/grpc/blob/master/doc/server-reflection.md">server reflection</a> and the <a href="https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md">gRPC command line tool</a> exist to assist with binary Protobuf messages. Also, Protobuf messages support <a href="https://developers.google.com/protocol-buffers/docs/proto3#json">conversion to and from JSON</a>. The built-in JSON conversion provides an efficient way to convert Protobuf messages to and from human readable form when debugging.</p>
<h2>gRPC recommended scenarios</h2>
<p>gRPC is well suited to the following scenarios:</p>
<ul>
<li><strong>Microservices</strong> – gRPC is designed for low latency and high throughput communication. gRPC is great for lightweight microservices where efficiency is critical.</li>
<li><strong>Point-to-point real-time communication</strong> – gRPC has excellent support for bidirectional streaming. gRPC services can push messages in real-time without polling.</li>
<li><strong>Polyglot environments</strong> – gRPC tooling supports all popular development languages, making gRPC a good choice for multi-language environments.</li>
<li><strong>Network constrained environments</strong> – gRPC messages are serialized with Protobuf, a lightweight message format. A gRPC message is always smaller than an equivalent JSON message.</li>
</ul>
<h2>Conclusion</h2>
<p>gRPC is a powerful new tool for ASP.NET Core developers. While gRPC is not a complete replacement for HTTP APIs, it offers improved productivity and performance benefits in some scenarios.</p>
<p>gRPC on ASP.NET Core is available now! If you are interested in learning more about gRPC, check out these resources:</p>
<div class="authorinfoarea">
<div><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/11/grpc-vs-http-apis-2.png" width="96" height="96" alt="Avatar" class="avatar avatar-96 wp-user-avatar wp-user-avatar-96 photo avatar-default"></div>
</p></div>
</div>
https://www.sickgaming.net/blog/2019/11/...http-apis/