Building High‑Performance gRPC Streaming Services with .NET 8, Load Balancing & Health Checks

Mahmut Sarıkaya 3 dk okuma 10 Görüntülenme 0
Building High‑Performance gRPC Streaming Services with .NET 8, Load Balancing & Health Checks

Why streaming matters for modern APIs

Enterprises are shifting 70% of their real‑time traffic to streaming APIs to cut latency by up to 40% and to reduce server‑side polling overhead. In a .NET‑centric ecosystem, gRPC streaming is the most efficient way to push continuous data—think telemetry, live market feeds, or multiplayer game state—while keeping bandwidth usage low.

Getting started with .NET 8 gRPC streaming

.NET 8 introduces native support for HTTP/2 without extra configuration, which means a gRPC service can be hosted on Kestrel with a single line in Program.cs. The following minimal service demonstrates a server‑side streaming method that emits a timestamped value every second.

using Microsoft.AspNetCore.Builder;<br>using Microsoft.Extensions.DependencyInjection;<br>using Grpc.Core;<br>using Grpc.Net.ClientFactory;<br><br>public class WeatherStream : Weather.WeatherBase{<br>    public override async Task Subscribe(SubscribeRequest request, IServerStreamWriter<WeatherUpdate> responseStream, ServerCallContext context){<br>        while (!context.CancellationToken.IsCancellationRequested){<br>            var update = new WeatherUpdate { Temperature = GetTemp(), Timestamp = Timestamp.FromDateTime(DateTime.UtcNow) };<br>            await responseStream.WriteAsync(update);<br>            await Task.Delay(TimeSpan.FromSeconds(1), context.CancellationToken);<br>        }<br>    }<br>}

Notice the use of IServerStreamWriter and the cancellation token, which are essential for graceful shutdown when the client disconnects.

Configuring client‑side load balancing in .NET 8

.NET 8’s Grpc.Net.ClientFactory now supports the round_robin load‑balancing policy out of the box. First, register multiple service addresses in appsettings.json, then enable the resolver and balancer when building the client.

// appsettings.json<br>{<br>  "GrpcServices": {<br>    "Weather": {<br>      "Addresses": [ "https://svc1.example.com:5001", "https://svc2.example.com:5001" ]<br>    }<br>  }<br>}<br><br>// Program.cs<br>builder.Services.AddGrpcClient<Weather.WeatherClient>(options =><br>{<br>    options.Address = new Uri("https://placeholder"); // placeholder, resolved by balancer<br>})<br>.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator })<br>.AddLoadBalancingPolicy("round_robin");

When the client calls Subscribe, the underlying channel rotates through the two endpoints, distributing the load evenly. Monitoring tools such as Prometheus can verify that request counts are balanced across instances.

Implementing health checks for gRPC services

Health checks are critical for the load balancer to avoid routing traffic to unhealthy pods. .NET 8 integrates Microsoft.Extensions.Diagnostics.HealthChecks with gRPC automatically. Add a health‑check endpoint in Program.cs and expose the standard gRPC health‑checking service.

builder.Services.AddHealthChecks()<br>    .AddGrpcHealthCheck("grpc");<br>builder.Services.AddGrpcHealthChecks();<br>app.MapGrpcHealthChecksService();

The client can now query grpc.health.v1.Health/Check before establishing a streaming session. If the response status is UNHEALTHY, the client can fallback to another endpoint or raise an alert.

Performance tuning tips for production

1. **Enable compression** – gRPC supports gzip out of the box. Set EnableCallCompression = true on the client and CompressionProviders on the server to shave 15‑20% off bandwidth.

2. **Adjust MaxConcurrentStreams** – Kestrel’s default of 100 may be insufficient for high‑throughput telemetry. Update KestrelServerOptions.Limits.Http2.MaxConcurrentStreams to 1000 in appsettings.json for large fan‑out scenarios.

3. **Use server‑side buffering** – By calling responseStream.WriteOptions = new WriteOptions(WriteFlags.NoCompress) you can control per‑message compression, useful when payloads are already compact.

Testing the end‑to‑end flow

Run three Docker containers: two instances of the gRPC service and one client. Use the following commands to spin them up quickly.

docker network create grpc-demo<br>docker run -d --name svc1 --network grpc-demo -p 5001:80 yourrepo/weather-service:latest<br>docker run -d --name svc2 --network grpc-demo -p 5002:80 yourrepo/weather-service:latest<br>docker run --rm --network grpc-demo yourrepo/weather-client:latest

Observe the client logs; they should show alternating connections to svc1 and svc2, confirming the round‑robin policy works. Health‑check failures appear as warnings in the client output.

Sources

Microsoft Docs – gRPC for .NET, Google Cloud – gRPC Load Balancing, .NET Blog – .NET 8 Performance Improvements

Author: Mahmut Sarıkaya — sarikayadev.com

Etiketler: #.NET 8 #gRPC #streaming APIs #client-side load balancing #health checks
Paylaş:
M

Yazar

Mahmut Sarıkaya

yazılım Geliştirici

Yorumlar

Henüz yorum yok. İlk yorumu siz yapın!

Yorum Bırakın

2 + 3 =