GithubHelp home page GithubHelp logo

kafka-parallel-flow's Introduction

Kafka Parallel Flow

Build status NuGet downloads GitHub license

This library provides a Kafka client for consuming messages in parallel from a single topic partition. Parallel consumption allows to process more data while at the same time improves CPU utilization.

This library is based on confluent-kafka-dotnet library.

Installation

Available on NuGet

dotnet add package Kafka.ParallelFlow
PM> Install-Package Kafka.ParallelFlow

Usage

Parallel consumer example

using Kafka.ParallelFlow;

var consumerConfig = new RecordConsumerConfig
{
    GroupId = "group-id-1",
    BootstrapServers = "localhost:9092",
    // Messages will be handled in parallel by up to 10 threads.
    MaxDegreeOfParallelism = 10,
    EnableAutoCommit = true,
    AutoCommitIntervalMs = 5_000,
    // If EnableAutoOffsetStore is true, messages will be stored for commit before they are handled.
    // If EnableAutoOffsetStore is false, messages will be stored for commit after they are handled.
    EnableAutoOffsetStore = false,
    AutoOffsetReset = AutoOffsetReset.Earliest
};

using var consumer = new RecordConsumer(consumerConfig);

// Messages with the same resolved key are handled in order.
// If `MemoryPartitionKeyResolver` is not set, no order 
// guarantees are provided - memory partitions are chosen in 
// round robin fashion.
consumer.MemoryPartitionKeyResolver = consumeResult => consumeResult.Message.Key;

consumer.ErrorHandler = ex => Environment.Exit(1);

consumer.ConsumeResultHandler = (consumeResult, _) => 
{
    Console.WriteLine(consumeResult.TopicPartitionOffset.ToString());
    return Task.CompletedTask;
};

// Starting non-blocking consumption.
consumer.Start("test-topic");

Console.ReadKey();

Dead letter topic example

using Kafka.ParallelFlow;

var consumerConfig = new RecordConsumerConfig
{
    GroupId = "group-id-1",
    BootstrapServers = "localhost:9092",
    MaxDegreeOfParallelism = 10,
    EnableAutoCommit = true,
    AutoCommitIntervalMs = 5_000,
    EnableAutoOffsetStore = false,
    AutoOffsetReset = AutoOffsetReset.Earliest,
    // Default dead letter topic name is <topic>__<consumer-group-id>__dlt.
    // It can be overriden by setting DeadLetterTopic value.
    DeadLetterTopic = "dead-letters"
};

using var consumer = new RecordConsumer(consumerConfig);

consumer.ErrorHandler = ex => Environment.Exit(1);

var producerConfig = new ProducerConfig
{
    BootstrapServers = BootstrapServers
};

using var producer = new ProducerBuilder<byte[], byte[]>(producerConfig).Build();

// Setting producer enables dead letter topic functionality. 
// If an error occurs while handling message, 
// the message will be produced to a dead letter topic. 
consumer.Producer = producer;

consumer.ConsumeResultHandler = (consumeResult, _) => 
{
    Console.WriteLine(consumeResult.TopicPartitionOffset.ToString());
    return Task.CompletedTask;
};

consumer.Start("test-topic");

Console.ReadKey();

Support

tautvydasverso

kafka-parallel-flow's People

Contributors

tautvydasversockas avatar

Stargazers

 avatar

Watchers

 avatar  avatar

kafka-parallel-flow's Issues

Add retry topic functionality.

Add automatic publishing and consuming from retry topics functionality ensuring that failed-to-handle messages are automatically retried after specified amount of time.

Add DLQ functionality.

Add automatic publishing to DLQ functionality in case messages fail to be deserialized or handled.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.