Confluent.Kafka
.Net programming
Kafka TopicPartitionOffset
Kafka consumption
Programming tutorials

How to Consume from specific TopicPartitionOffset with Confluent.Kafka in .Net

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Confluent.Kafka is a robust .NET client for Apache Kafka and is part of the larger Confluent Platform which enhances the Kafka integration for other languages. It allows .NET developers to publish and consume messages efficiently to and from a Kafka cluster. Of particular importance for some applications is the ability to consume messages from a specific point in a Kafka topic's partition, known as consuming from a specific TopicPartitionOffset.

Understanding TopicPartitionOffset

Before diving into the specifics of implementation with Confluent.Kafka, it's important to understand the main components:

  • Topic: The category or feed name to which messages are published.
  • Partition: Kafka topics are split into partitions to allow for data scalability and parallel processing.
  • Offset: Each message within a partition is assigned a unique sequence id called an offset.

When consuming messages in Kafka, one can specify from exactly which topic, partition, and offset to start. This functionality is crucial for precise control over message consumption, such as when you need to replay or skip messages, handle failures, or simply start processing from a known position.

Consuming from a Specific TopicPartitionOffset

Here’s how to implement this using Confluent.Kafka in a .NET environment:

Step 1: Create a Consumer Configuration

Firstly, you need to create a consumer configuration specifying various settings such as bootstrap servers, group id, auto offset reset, etc.

csharp
1var config = new ConsumerConfig
2{
3    BootstrapServers = "localhost:9092",
4    GroupId = "example-consumer-group",
5    AutoOffsetReset = AutoOffsetReset.Earliest,
6    EnableAutoCommit = false
7};

Step 2: Define the TopicPartitionOffset

Specify the exact topic, partition, and offset from where you want to start consuming messages.

csharp
var topicPartitionOffset = new TopicPartitionOffset("example-topic", 0, 10);

Step 3: Create a Kafka Consumer and Assign it to the TopicPartitionOffset

Instantiate the Kafka consumer and manually assign it to the TopicPartitionOffset. This assignment tells the consumer exactly where it should start reading.

csharp
1using (var consumer = new ConsumerBuilder<Ignore, string>(config).Build())
2{
3    consumer.Assign(topicPartitionOffset);
4
5    while (true)
6    {
7        var consumeResult = consumer.Consume(CancellationToken.None);
8        Console.WriteLine($"Received message at {consumeResult.TopicPartitionOffset}: {consumeResult.Message.Value}");
9
10        // Commit the offset
11        try
12        {
13            consumer.Commit(consumeResult);
14        }
15        catch (KafkaException e)
16        {
17            Console.WriteLine($"Commit error: {e.Error.Reason}");
18        }
19    }
20}

Note that in this example, we use a manual commit of offsets (consumer.Commit(consumeResult)) to ensure exactly-once processing semantics.

Handling Errors and Exceptions

When consuming from a specific offset, there are potential errors to handle:

  • OffsetOutOfRange: This occurs if the offset no longer exists on the server (usually because it has been deleted according to the topic's retention policy).

Errors should be caught and handled to prevent the consumer from crashing and to allow for possible recovery or alternative actions to be decided.

Summary of Key Points

PropertyDescription
BootstrapServersComma-separated list of initial Kafka broker IPs to connect to.
GroupIdConsumer group ID to which the consumer belongs.
AutoOffsetResetPolicy for resetting offsets on OffsetOutOfRange error or when no initial offset is available.
EnableAutoCommitWhether the consumer's offset is periodically committed in the background.
TopicPartitionOffsetCombination of topic, partition, and offset indicating where exactly to start consuming messages.

By understanding and using TopicPartitionOffset with Confluent.Kafka in .NET, developers can leverage Apache Kafka with enhanced precision and effectiveness, crucial for scenarios requiring specific data handling and process recovery capabilities in distributed systems.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.