On Net 6

Time:2022-1-12

Grpc is a modern, cross platform and high-performance RPC framework. Grpc is built on ASP Net core, which is also recommended by us Net to build RPC service.

. net 6 further improves the already excellent performance of grpc and adds a series of new functions to make grpc better than ever in modern cloud native applications. In this article, I will describe these new features and how we can lead the industry through the first grpc implementation that supports end-to-end http / 3.

GPRC client load balancing

Client load balancing is a feature that allows grpc clients to optimally distribute load among available servers. Client load balancing eliminates the need for load balancing agents. This has several benefits:

  • Improved performance. Agentless means eliminating additional network hops and reducing latency because RPC is sent directly to the grpc server.
  • Make effective use of server resources. The load balancing agent must parse and resend each HTTP request sent through it. Deleting agents can save CPU and memory resources.
  • Simpler application architecture. The proxy server must be set up and configured correctly. No proxy server means fewer active parts!

Client load balancing is configured when a channel is created. There are two components to consider when using load balancing:

  • The parser parses the address of the channel. The parser supports obtaining addresses from external sources. This is also called service discovery.
  • Load balancer, which creates a connection and selects the address that grpc calls will use.

The following code example configures the channel to use DNS service discovery with cyclic load balancing:

var channel = GrpcChannel.ForAddress(

"dns:///my-example-host",
newGrpcChannelOptions
{
    Credentials = ChannelCredentials.Insecure,
    ServiceConfig = newServiceConfig { LoadBalancingConfigs = { newRoundRobinConfig() } }
});

var client = newGreet.GreeterClient(channel);
var response = await client.SayHelloAsync(newHelloRequest { Name = “world” });

For more information, see GPRC client load balancing.

Instantaneous fault handling with retry

Grpc calls may be interrupted by transient failures. Transient faults include:

  • The network connection was temporarily interrupted.
  • The service is temporarily unavailable.
  • Timeout due to server load.

When the grpc call is interrupted, the client throws an rpcexception with error details. The client application must catch exceptions and choose how to handle errors.
var client =newGreeter.GreeterClient(channel);
try
{

var response =await client.SayHelloAsync(
    newHelloRequest{Name=".NET"});

Console.WriteLine("From server: "+ response.Message);

}
catch(RpcException ex)
{

// Write logic to inspect the error and retry
// if the error is from a transient fault.

}

Replication retry logic is verbose and error prone throughout the application. Fortunately Net grpc client now has built-in support for automatic retry. Retry is centrally configured on the channel, and there are many options available to customize the retry behavior using retrypolicy.

var defaultMethodConfig =newMethodConfig
{

Names={MethodName.Default},
RetryPolicy=newRetryPolicy
{
    MaxAttempts=5,
    InitialBackoff=TimeSpan.FromSeconds(1),
    MaxBackoff=TimeSpan.FromSeconds(5),
    BackoffMultiplier=1.5,
    RetryableStatusCodes={StatusCode.Unavailable}
}

};

// Clients created with this channel will automatically retry failed calls.
var channel =GrpcChannel.ForAddress(“https://localhost:5001”,newGrpcChannelOptions
{

ServiceConfig=newServiceConfig{MethodConfigs={ defaultMethodConfig }}

});

For more information, see transient troubleshooting using grpc retry.

Protobuf performance

About Net using Google The protobuf package serves as the default serializer for messages. Protobuf is an efficient binary serialization format. Google. Protobuf is designed to improve performance by using code generation rather than reflection for serialization Net object. Yes Net 5, we worked with the protobuf team to add support for modern memory APIs (such as span < T >, readonlysequence < T > and ibufferwriter < T >) for serializers Net 6 optimizes an already fast serializer.
Protocol Buffers / protobuf#8147 adds vectorized string serialization. The SIMD instruction allows multiple characters to be processed in parallel, significantly improving performance when serializing certain string values.

privatestring _value =newstring(‘ ‘,10080);
privatebyte[] _outputBuffer =newbyte[10080];
[Benchmark]
publicvoidWriteString()
{

var span =newSpan<byte>(_outputBuffer);
WriteContext.Initialize(ref span,outWriteContext ctx);
ctx.WriteString(_value);
ctx.Flush();

}

On Net 6

Protocol Buffers / protobuf#7645 adds a new API for creating bytestring instances. If you know that the underlying data will not change, use unsafebyteoperations Unsafewrap to create a bytestring without copying the underlying data. This is useful if your application handles large byte payloads and you want to reduce the frequency of garbage collection.

GPRC download speed

Grpc users report sometimes slower downloads. Our investigation found that http / 2 traffic control restricts downloads when there is a delay between the client and the server. The server fills the receive buffer window before the client can run out, causing the server to pause sending data. Grpc messages are downloaded in a start / stop burst mode.

This has been fixed in dotnet / runtime #54755. Httpclient now dynamically scales the receive buffer window. After the HTTP / 2 connection is established, the client will send a ping to the server to measure the delay. If there is a high delay, the client will automatically increase the receive buffer window to achieve fast and continuous download.

privateGrpcChannel _channel =GrpcChannel.ForAddress(…);
privateDownloadClient _client =newDownloadClient(_channel);

[Benchmark]
publicTaskGrpcLargeDownload()=>

_client.DownloadLargeMessageAsync(newEmptyMessage());

On Net 6

Http / 3 support

Net now supports HTTP / 3. Grpc is built on Net 6 Net core and HTTP / 3 support of httpclient. For more information, see Net 6.
. net is the first grpc implementation that supports end-to-end http / 3. We have submitted grfc for other platforms to support HTTP / 3 in the future. Grpc with HTTP / 3 is a highly requested function of the developer community. I’m glad to see it Net is in a leading position in this field.
On Net 6

summary

The performance is Net and grpc, and Net 6 is faster than ever. New performance oriented features such as client load balancing and HTTP / 3 mean lower latency, higher throughput and fewer servers. This is an opportunity to save money, reduce energy consumption and build more environmentally friendly cloud native applications.

To try out the new features and start at Net, the best starting point is ASP Net core tutorial to create grpc clients and servers.

We look forward to hearing about using grpc and Net and your contribution to the dotnet and grpc repositories!

Grpc reference documentation on. Net