Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: BackgroundService


Consuming Kafka in a .NET 5 console service with automatic reconnection

This article introduces a practical solution for building a highly available Kafka consumer background service in .NET 5 using Microsoft.Extensions.Hosting and Confluent.Kafka. The core implementation is based on BackgroundService, employing an outer daemon loop to catch exceptions and delay reconnection, ensuring that the service can automatically recover during network fluctuations or Broker failures. The code uses `await Task.Yield()` to avoid blocking host startup and disables automatic commit to manually control Offset, thereby ensuring that data is not lost when message processing fails. Regarding graceful shutdown, it utilizes CancellationToken to respond to Docker SIGTERM signals, ensuring that `consumer.Close()` is executed in the finally block to promptly notify the Broker to trigger Rebalance and exit cleanly. Additionally, the article emphasizes critical considerations for production environments: externalizing configuration, using IServiceScopeFactory to resolve Scoped service dependency issues, and adopting batch commit to optimize performance in high-volume scenarios.

Views 200
.NET .NET 5 BackgroundService Confluent.Kafka Docker Kafka