Skip to main content

Partitioned Async Processing

RunPartitionedAsync is the high-level consumer API for work that must stay ordered within each Kafka partition but can run in parallel across partitions.

Use it when:

  • a key, customer, tenant, aggregate, or stream shard must be processed in offset order
  • different partitions can run at the same time
  • offset commits must reflect completed processing, not just fetched records
  • partition revoke, lost, and shutdown behavior must be explicit

The method owns the consume loop while it runs. Do not call ConsumeAsync, ConsumeBatchAsync, ConsumeRawBatchAsync, consumer.Partitions.Assign, consumer.Partitions.Unassign, consumer.Partitions.Pause, or consumer.Partitions.Resume concurrently on the same consumer.

Basic Usage​

Use manual offset commits for at-least-once partitioned processing:

await using var consumer = await Kafka.CreateConsumer<string, Order>()
.WithBootstrapServers("localhost:9092")
.WithGroupId("order-workers")
.WithOffsetCommitMode(OffsetCommitMode.Manual)
.SubscribeTo("orders")
.BuildAsync();

var options = new PartitionedProcessingOptions
{
MaxBufferedRecordsPerPartition = 256,
BackpressureMode = PartitionBackpressureMode.PauseResume,
StopPolicy = PartitionStopPolicy.Drain,
CommitPolicy = PartitionCommitPolicy.CommitCompletedOnRevoke
};

await consumer.RunPartitionedAsync(
async (partition, ct) =>
{
await foreach (var message in partition.Messages.WithCancellation(ct))
{
await ProcessOrderAsync(message.Value, ct);
partition.MarkProcessed(message);
}
},
options,
stoppingToken);

Dekaf starts one processor invocation for each assigned TopicPartition. Each processor receives only the ordered stream for that partition. Processors for different partitions run concurrently.

Record Storage Lifetime​

Record and batch handlers can read raw keys, raw values, and lazy headers across asynchronous suspension until the handler returns. Dekaf retains the owning fetch storage while records are queued or being processed. For the long-lived partition processor's Messages stream, each record remains valid until the enumerator advances or is disposed. Copy borrowed data if it must outlive that boundary.

Batch handlers also borrow the IReadOnlyList view. Its storage is reused after the handler completes. Copy the records, along with any borrowed key/value/header data, before retaining a batch outside its handler.

Custom deserializers may return slices of their input: that fetch storage follows the same lifetime. A deserializer's own reusable scratch buffer is not fetch storage; return an owned value instead of exposing scratch memory that the next deserialization will overwrite.

Retention ends after completion, failure, or cancellation. A handler that ignores cancellation retains its active storage until it actually exits, even when the runtime's stop timeout has elapsed. Revoke and lost-assignment cleanup use the same ownership rules. Key ordering also retains the input backing a dictionary key until its key lane is removed.

For key ordering, keep each key's hash code and equality stable while records are queued or running. If cleanup cannot remove a key, dispatch fails and observes in-flight handlers before releasing retained storage.

MaxBufferedRecordsPerPartition bounds the partition queue, not retained bytes. Partition ordering additionally holds the active handler batch (one record for a record handler). Key ordering additionally holds at most that many dispatched records and one retained key per active key lane. Completed records release their dispatch slots and fetch storage immediately, including behind an unfinished earlier record; commits still wait for that gap to close. One borrowed record can pin an entire fetch response and its decompressed record storage. Budget those buffers separately from consumer prefetch; fetch-size settings and compression affect their byte cost. Slow handlers do not accumulate an unbounded history of completed fetches.

Ordering And Parallelism​

The processor callback is long-lived. It starts when a partition is assigned and ends when the partition is revoked, lost, stopped, failed, or when the whole consumer shuts down.

Within one partition:

  • messages are yielded in Kafka offset order
  • at most one processor invocation is active
  • MarkProcessed records completed offsets; commits advance only through contiguous completed offsets

Across partitions:

  • processors run independently
  • slow partitions do not block processing already queued for other partitions
  • shared application state must still be protected by your code
await consumer.RunPartitionedAsync(
async (partition, ct) =>
{
var topicPartition = partition.TopicPartition;

await foreach (var message in partition.Messages.WithCancellation(ct))
{
await HandlePartitionRecordAsync(
topicPartition,
message.Offset,
message.Value,
ct);

partition.MarkProcessed(message);
}
},
cancellationToken: stoppingToken);

This pattern lets partition orders-0 continue in offset order while orders-1, orders-2, and other assigned partitions run their own ordered lanes at the same time.

Key-ordered handlers​

Use the record-handler overload when you want Dekaf to invoke your handler per record and mark records processed after the handler returns successfully:

await consumer.RunPartitionedAsync(
async (partition, message, ct) =>
{
await HandleOrderAsync(message.Key, message.Value, ct);
},
new PartitionedProcessingOptions
{
Ordering = PartitionedProcessingOrder.Key,
MaxConcurrentHandlersPerPartition = 8,
MaxBufferedRecordsPerPartition = 512
},
stoppingToken);

With PartitionedProcessingOrder.Key, different keys from the same Kafka partition can run concurrently, but records with the same key are processed in offset order. Dekaf tracks offset gaps, so a later key cannot advance commits past an earlier unfinished record.

Key equality applies to deserialized keys within one partition. By default, byte[], ReadOnlyMemory<byte>, Memory<byte>, and ArraySegment<byte> keys compare their byte content, including slice boundaries. Equal bytes share a lane even when deserialization creates separate arrays or memory slices. Empty binary keys share a lane. Kafka null keys share a separate lane, including when a binary value-type deserializer represents both null and empty bytes as its default value. Other key types use EqualityComparer<TKey>.Default, preserving ordinal string and value-type equality. These binary defaults apply when TKey is one of the listed types; wrapper or polymorphic key types can supply a comparer.

Default binary hashing reads at most 64 bytes and includes the key length. Dispatch caches that hash through lane removal. Equality still compares every byte, so keys that differ outside the sampled regions remain distinct. Repeated keys and hash collisions can therefore still cost more as key length grows. For such workloads, consider deserializing a compact application identity and supplying a comparer that preserves the required equality. A hash alone does not establish identity.

For custom key equality, supply an IEqualityComparer<TKey> to either handler overload. For example, a string-keyed consumer can group case-insensitive keys:

await consumer.RunPartitionedAsync(
async (partition, message, ct) =>
{
await HandleOrderAsync(message.Key, message.Value, ct);
},
new PartitionedProcessingOptions
{
Ordering = PartitionedProcessingOrder.Key,
MaxConcurrentHandlersPerPartition = 8
},
StringComparer.OrdinalIgnoreCase,
stoppingToken);

The comparer must be thread-safe, give equal keys equal hash codes, and keep both equality and hash codes stable while a key's lane remains active. Do not mutate binary key bytes or fields used by the comparer during processing. Dekaf retains fetch storage backing a lane's representative key until that lane becomes idle; application-owned key storage must obey the same lifetime. Copy borrowed data if you keep it after processing completes.

Choose equality that matches your application's serialized Kafka key identity. A custom comparer can deliberately group different wire keys (for example, case-insensitive strings), but cannot impose order across different partitions. Custom deserializers that transform or discard key information must provide an appropriate comparer when their default equality does not preserve that identity. Retain the identity fields in the deserialized key, or use a binary key type; a comparer cannot recover discarded wire data. Comparers do not receive null keys and are ignored for partition ordering.

Batch handlers​

Use RunPartitionedBatchesAsync when your application works more efficiently on groups of records:

await consumer.RunPartitionedBatchesAsync(
async (partition, messages, ct) =>
{
await SaveBatchAsync(messages.Select(static m => m.Value), ct);
},
new PartitionedProcessingOptions
{
Ordering = PartitionedProcessingOrder.Key,
MaxConcurrentHandlersPerPartition = 4,
MaxBufferedRecordsPerPartition = 400,
MaxHandlerBatchSize = 100
},
stoppingToken);

Batch handlers receive up to MaxHandlerBatchSize records. In key-ordered mode, each batch contains records for one key lane. Records in a handler or batch are marked processed only after the callback completes successfully.

Key-ordered processing divides reusable batch storage across the configured workers. The effective worker count is the smaller of MaxConcurrentHandlersPerPartition and MaxBufferedRecordsPerPartition. The effective batch cap is the smaller of MaxHandlerBatchSize and MaxBufferedRecordsPerPartition / effectiveWorkerCount, using integer division. This cap applies even when fewer keys are active. With the default 256-record buffer and four workers, requesting 100-record batches gives a cap of 64. The example reserves 400 records to permit up to 100 per batch with four workers. A batch can still contain fewer records when its key has less work available.

Commit Semantics​

MarkProcessed(message) marks message.Offset + 1 as eligible for commit for the current partition. Dekaf never commits a record merely because it was fetched or yielded.

Commit policies:

PolicyBehavior
UserManagedDekaf tracks processed offsets, but only your code calls CommitProcessedAsync or another commit mechanism.
CommitCompletedOnRevokeDekaf commits completed offsets when a partition is revoked and during graceful shutdown. This is the default.
CommitCompletedPeriodicallyDekaf commits completed offsets on CommitInterval, then again on revoke and graceful shutdown.
CommitCompletedInTransactionDekaf commits completed offsets through TransactionalProducer's transactions on CommitInterval, on revoke, seek and graceful shutdown. See Exactly-once processing.

Manual per-partition commits are useful when you want a tighter commit cadence without waiting for a rebalance:

await consumer.RunPartitionedAsync(
async (partition, ct) =>
{
await foreach (var message in partition.Messages.WithCancellation(ct))
{
await SaveAsync(message.Value, ct);
partition.MarkProcessed(message);

if (ShouldFlushOffset(partition.LastProcessedOffset))
await partition.CommitProcessedAsync(ct);
}
},
new PartitionedProcessingOptions
{
CommitPolicy = PartitionCommitPolicy.UserManaged
},
stoppingToken);

CommitProcessedAsync commits only the current partition's contiguous completed offset. Runtime-managed revoke and shutdown commits may batch completed offsets for multiple partitions into one CommitAsync call.

Runtime-managed commits skip partitions whose completed offset has not moved since the last successful commit, so an idle partition does not send a commit every CommitInterval. When the group has fenced or expired the member (for example after a network outage longer than the session timeout), a runtime-managed commit is rejected with a GroupException. The runtime logs a warning and keeps running: the consumer reports the partitions through OnPartitionsLost and rejoins. No offset changes, and the new owner resumes from the last successful commit. A commit you request with CommitProcessedAsync still throws the GroupException to your handler.

A consumer without a group ID (manual assignment with offsets kept in your own storage) has nowhere to commit, so the runtime skips its revoke, seek, periodic and shutdown commits and the run ends on cancellation as usual. MarkProcessed still tracks progress, and CommitProcessedAsync throws InvalidOperationException because the consumer cannot commit.

Auto commit mode with automatic offset store (OffsetCommitMode.Auto + EnableAutoOffsetStore = true, the consumer defaults) stages offsets based on consume-loop progress. The partitioned runtime pulls records off the loop and dispatches them to workers, so loop progress no longer proves processing — the background loop would stage and commit records regardless of MarkProcessed, silently voiding the runtime's at-least-once tracking. This applies to both OffsetStoreTiming values, since neither ties staging to worker completion. RunPartitionedAsync therefore throws InvalidOperationException when the consumer uses that combination together with a runtime-managed commit policy (CommitCompletedOnRevoke, CommitCompletedPeriodically or CommitCompletedInTransaction). For at-least-once partitioned processing, configure the consumer with OffsetCommitMode.Manual, or with WithAutoOffsetStore(false) — with the automatic store disabled, the background loop has nothing of its own to commit, so only the runtime's MarkProcessed-tracked commits advance offsets. The combination of auto commit and PartitionCommitPolicy.UserManaged also remains allowed for applications that accept auto-commit semantics.

This check inspects the commit configuration of Dekaf's own consumer implementations (KafkaConsumer and the testing InMemoryConsumer). Custom implementations and wrappers can opt into the same guard by implementing IConsumerCommitConfiguration and forwarding OffsetCommitMode, EnableAutoOffsetStore, and HasConsumerGroup. Types that do not implement that interface cannot be inspected and will not fail fast, so ensure their underlying configuration follows the rules above.

Exactly-Once Processing​

CommitCompletedInTransaction commits completed offsets through a transactional producer, so records your handlers produce become visible together with the offsets of the records that produced them. Handlers produce with the producer's own ProduceAsync; those records join the transaction the runtime keeps open.

The example counts committed output. Each handler increments a shared pending count after a successful produce. The callback clears the count when the transaction ends and publishes it only when the producer confirms the commit.

using System.Diagnostics.Metrics;

using var meter = new Meter("Orders.Transformer");
var committedRecords = meter.CreateCounter<long>("orders.committed");
long pending = 0;

await using var producer = await Kafka.CreateProducer<string, string>()
.WithBootstrapServers("localhost:9092")
.WithTransactionalId($"orders-transformer-{Environment.MachineName}")
.BuildAsync();
await producer.InitTransactionsAsync();

await consumer.RunPartitionedAsync(
async (context, message, ct) =>
{
await producer.ProduceAsync("orders-enriched", message.Key, message.Value.ToUpperInvariant(), ct);
Interlocked.Increment(ref pending);
},
new PartitionedProcessingOptions
{
Ordering = PartitionedProcessingOrder.Key,
MaxConcurrentHandlersPerPartition = 8,
CommitPolicy = PartitionCommitPolicy.CommitCompletedInTransaction,
CommitInterval = TimeSpan.FromSeconds(1),
TransactionalProducer = PartitionedTransactionalProducer.Create(producer),
OnTransactionCompleted = outcome =>
{
var count = Interlocked.Exchange(ref pending, 0);
if (outcome == PartitionedTransactionOutcome.Committed)
{
committedRecords.Add(count);
}
}
},
stoppingToken);

One transaction is open at a time and covers every partition. At each commit, processors stop taking queued records, the runtime waits until every record they already took has completed, then sends the completed offsets with ConsumerGroupMetadata and commits. Key-ordered handlers can complete out of order, so waiting for the records already taken keeps committed output limited to the records below the committed offsets. Commits happen every CommitInterval, when a partition is revoked (waiting at most StopTimeout for in-flight records and the commit together), before a seek, and at graceful shutdown. CommitInterval sets the transaction length: a longer interval commits less often, and downstream readers see the output later.

When a commit fails, or a revoke's records and commit do not finish within StopTimeout, the runtime aborts the transaction and rewinds every partition it still owns to its committed offset, so those records are processed again in a new transaction. When the failed commit was a seek's, the seeked partition rewinds the same way and SeekAsync throws the commit's error instead of moving to the target. A lost partition aborts the transaction the same way, because its offsets can no longer be committed. When the abort itself fails, the run ends with that error. During a revoke or a lost partition the abort also gets one StopTimeout; an abort that does not finish in time ends the run with a TimeoutException.

The policy requires a record or batch handler overload, a consumer group and PartitionWorkerErrorPolicy.StopConsumer: a failed handler may have produced part of its output, so the run ends and the open transaction aborts. Handlers cannot call CommitProcessedAsync. Consumers of the output topics must read with IsolationLevel.ReadCommitted to skip aborted output. The run owns the producer's transactions while it is active, so do not begin transactions on it yourself.

The callback runs once per transaction before the next transaction starts, including when partition loss aborts a transaction before an offset commit. It also runs for empty transactions at shutdown. Aborted confirms the producer aborted it. Unknown means the producer could not confirm the outcome. Keep the callback short because it runs inline. Dekaf logs callback exceptions and continues handling the transaction outcome.

Assignment Lifecycle​

When partitions are assigned, Dekaf creates partition state, starts exactly one processor lane per partition, then routes records to those lanes.

When partitions are revoked during cooperative rebalance, Dekaf:

  1. Stops routing new records to the revoked partitions.
  2. Removes queued or prefetched records for partitions no longer assigned.
  3. Applies StopPolicy.
  4. Commits only offsets that were marked processed when the commit policy allows it.
  5. Disposes partition state and completes the partition message stream.

The revocation callback returns only after these steps finish, so the member releases the partitions to the group only once their lanes have stopped and the commit has been attempted. Another member therefore never processes a revoked partition while this member still drains it. Under PartitionStopPolicy.Drain the release waits for queued records, up to StopTimeout. The revoked lanes stop in parallel, and their offsets are then committed in one request, also bounded by StopTimeout. The wait is therefore at most about two StopTimeouts however many partitions are revoked. A commit that does not finish in time is abandoned, and the next owner processes those records again. Heartbeats continue while the revocation callback waits, reporting the previous ownership until the callback completes. The broker's rebalance timeout still bounds reconciliation, so keep the total revocation wait below ConsumerOptions.RebalanceTimeoutMs. A revocation caused by leaving the group (unsubscribe or close) does not wait, because the leave itself ends ownership.

When partitions are lost involuntarily, such as after a heartbeat timeout, Dekaf cancels those partition lanes and completes their streams. It does not commit offsets for lost partitions because ownership is no longer guaranteed.

Seek And Replay​

Calling consumer.Seek while RunPartitionedAsync runs leaves records from the old position in the partition's lane. To move a partition while processing runs, pass a PartitionedProcessingControl in the options and seek through it:

var control = new PartitionedProcessingControl();

var run = consumer.RunPartitionedAsync(
async (partition, message, ct) =>
{
await HandleOrderAsync(message.Key, message.Value, ct);
},
new PartitionedProcessingOptions { Control = control },
stoppingToken);

// Later, for example after fixing a bug in the handler:
await control.SeekAsync(new TopicPartitionOffset("orders", 3, 1_000), cancellationToken);
await control.SeekAsync(
new TopicPartitionTimestamp(new TopicPartition("orders", 4), DateTimeOffset.UtcNow.AddHours(-1)),
cancellationToken);

For the target partition, the seek:

  1. Stops dispatching the records already buffered for the partition. They are discarded, not processed.
  2. Stops the in-flight handlers under StopPolicy and StopTimeout: Drain lets them finish, Cancel cancels them.
  3. With CommitCompletedOnRevoke or CommitCompletedPeriodically, commits the lower of the partition's completed offset and the target, so a restart resumes from the seek position. When no record has completed, it commits the target only when the target is behind the group's committed offset, so a forward seek never commits past records that were not processed. Completion tracking for the old position is discarded. A failed commit is logged and does not fail the seek.
  4. Seeks the consumer and starts a new lane at the target.

The seek also restarts a partition that StopPartition stopped. Other partitions keep processing their buffered records, but Dekaf routes no new batches until the seek completes. A timestamp seek resolves the first offset at or after the timestamp before the partition stops, and seeks to the end when no record is that recent. TopicPartitionTimestamp.Earliest and Latest seek to the start and end of the partition.

SeekAsync throws InvalidOperationException when the partition is not assigned, when no run is active for the control, or when the run stops before the seek completes. Any other failure while the runtime seeks, for example the consumer rejecting the seek, fails both SeekAsync and the run. A control attaches to one active run at a time. Cancelling the seek's token before the runtime starts the seek prevents it; later cancellation stops only the caller's wait.

Shutdown​

Cancelling the token passed to RunPartitionedAsync stops the consume loop and then stops all active partition lanes.

PartitionStopPolicy.Drain:

  • completes each partition message stream
  • lets the processor finish already queued records
  • waits up to StopTimeout
  • commits completed offsets during graceful shutdown when the commit policy allows it

PartitionStopPolicy.Cancel:

  • cancels each processor's token immediately
  • does not wait for the remaining queued records to be processed
  • commits only offsets already marked processed when the commit policy allows it

The CancellationToken passed to the processor is the partition stopping token. With Drain, the normal signal is stream completion. With Cancel, lost partitions, processor failure cleanup, or drain timeout, the token is cancelled.

context.StopRequestedToken is cancelled as soon as a partition starts stopping for any reason (revoke, loss, seek, failure or shutdown), under either policy. A handler that is waiting on something outside Kafka, such as a dependency held behind an open circuit breaker, can watch it and throw OperationCanceledException to leave without completing its record. The stop then finishes promptly without failing the run, and the record is replayed from the committed offset:

// Completed by your circuit breaker when the dependency recovers.
var dependencyAvailable = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

await consumer.RunPartitionedAsync(
async (context, message, cancellationToken) =>
{
await dependencyAvailable.Task.WaitAsync(context.StopRequestedToken);
await ProcessOrderAsync(message.Value, cancellationToken);
},
new PartitionedProcessingOptions { StopPolicy = PartitionStopPolicy.Drain },
stoppingToken);

If a processor does not finish within StopTimeout, Dekaf treats the timeout as fatal because it can no longer guarantee a single active processor for that partition. During shutdown, StopTimeout also bounds the final runtime-managed commit attempt.

Backpressure​

Each partition lane has a bounded queue. MaxBufferedRecordsPerPartition limits how many records Dekaf buffers for one partition after fetching and before your processor handles them.

Memory for partition lanes is bounded by:

assigned partition count * MaxBufferedRecordsPerPartition * average record size

With MaxBufferedRecords set, the record count is also capped at MaxBufferedRecords, whatever the assignment size.

Backpressure modes:

ModeBehaviorUse when
PauseResumeDekaf pauses a partition when its lane is full and resumes it when capacity returns.Production default. It isolates slow partitions and keeps the shared consume loop fair.
AwaitCapacityDekaf waits for queue capacity without changing the consumer pause state.Tests or simple deployments where pause/resume side effects are undesirable.

While RunPartitionedAsync is active, Dekaf owns pause and resume for its backpressure. Do not manually pause or resume partitions on the same consumer.

Consumer-wide limits​

Per-partition limits grow with the assignment: 256 buffered records per partition across 200 partitions is 51,200 records. Set consumer-wide limits when memory or a shared downstream resource, such as a connection pool or a rate-limited API, needs a cap that does not depend on how many partitions the group assigns:

new PartitionedProcessingOptions
{
Ordering = PartitionedProcessingOrder.Key,
MaxConcurrentHandlersPerPartition = 4,
MaxConcurrentHandlers = 16, // across all partitions; null = unlimited
MaxBufferedRecords = 1024 // across all partitions; null = unlimited
};
  • MaxConcurrentHandlers caps record and batch handler invocations across all partitions. Partitions waiting for a handler are served in arrival order, and each partition waits for one handler at a time, so a busy partition cannot starve the others. It requires a record or batch handler overload.
  • MaxBufferedRecords caps records Dekaf holds for handlers across all partitions. A record counts from when the runtime queues it until a handler receives it, including records a key-ordered partition holds while their key or a handler is busy. When a record would exceed the limit, BackpressureMode applies to its partition: PauseResume pauses it until the consumer-wide buffer drains to half the limit, and AwaitCapacity waits for capacity.

The per-partition limits still apply. Revoke, drain and commit behavior does not change.

Bounded partition queues do not replace Kafka fetch limits such as QueuedMaxMessagesKbytes; they add an application-processing boundary after records have been fetched.

Error Policy​

PartitionWorkerErrorPolicy.StopConsumer is the default. A processor exception stops the whole partitioned run and propagates from RunPartitionedAsync.

StopPartition stops only the failed partition and pauses it while it remains assigned. Use this only when operations can tolerate lag on that partition until the next revoke or reassignment.

Ignore logs the exception, pauses the partition for an exponential backoff, and then resumes it and restarts the failed lane. Healthy partitions keep running during the backoff. The records the failed processor received, including the one it failed on, are skipped. The partition rewinds to the first record the processor never received, so the restarted lane processes every record that was still queued. Prefer handling retries and dead-letter routing inside your processor so unexpected exceptions remain visible.

Health Monitoring​

A consumer-wide "last progress" time hides a stuck partition: fast or idle partitions keep updating it while one handler hangs. To see in-flight work, pass a PartitionedProcessingMonitor to the run and read OldestOutstandingRecordAt from your health check:

var monitor = new PartitionedProcessingMonitor();

var run = consumer.RunPartitionedAsync(
async (context, message, cancellationToken) => await ProcessOrderAsync(message.Value, cancellationToken),
new PartitionedProcessingOptions { Monitor = monitor },
stoppingToken);

// In a health check, on any thread:
var oldest = monitor.OldestOutstandingRecordAt;
var stalled = oldest is { } receivedAt && DateTimeOffset.UtcNow - receivedAt > TimeSpan.FromMinutes(5);

The value is the time the runtime received the oldest record that is still queued or being processed and is not yet marked processed, across every assigned partition. It is null when nothing is outstanding or no run is attached. Record and batch handlers mark records processed when they return. A low-level PartitionProcessor marks them with context.MarkProcessed, so a record it never marks stays outstanding until its partition stops.

A monitor observes one run at a time. Reading it does not allocate, and a monitored run records one timestamp per partition per fetched batch, not per record.

Each partition keeps at most 1,024 batch timestamps. When more batches than that wait behind one unfinished record, for example behind a stuck key in key-ordered mode, neighbouring batches share the earlier timestamp. The reported time can then be earlier than the real arrival of later records, but never later, so a stuck record is never hidden.

Observing Runtime Commits​

Under CommitCompletedOnRevoke, CommitCompletedPeriodically or CommitCompletedInTransaction, the runtime sends commits itself; under the transactional policy each observed commit covers sending the offsets to the transaction and committing it. To measure them, for example with a span per commit, pass an IOffsetCommitObserver:

var source = new ActivitySource("Orders");

var run = consumer.RunPartitionedAsync(
async (context, message, cancellationToken) => await ProcessOrderAsync(message.Value, cancellationToken),
new PartitionedProcessingOptions
{
CommitPolicy = PartitionCommitPolicy.CommitCompletedPeriodically,
CommitObserver = new CommitTracing(source)
},
stoppingToken);

sealed class CommitTracing(ActivitySource source) : IOffsetCommitObserver
{
public object? OnCommitStarting(OffsetCommitObservation observation)
=> source.StartActivity($"commit {observation.Reason}");

public void OnCommitCompleted(OffsetCommitObservation observation, object? state, Exception? exception)
{
if (state is not Activity activity)
return;
activity.SetTag("messaging.batch.message_count", observation.CompletedRecordCount);
if (exception is not null)
activity.SetStatus(ActivityStatusCode.Error, exception.Message);
activity.Dispose();
}
}

OnCommitStarting runs before the commit request and OnCommitCompleted runs once after it, with the exception when it failed. The observation carries the offsets and the reason: Periodic, Revoke, Stop (a partition stopping, for example at shutdown) or Seek. Commits can overlap, so keep per-commit state in the value OnCommitStarting returns. The observer cannot change what is committed: an exception it throws is logged and the commit continues. Commits a handler requests with CommitProcessedAsync are not observed.

CompletedRecordCount is the number of completed records the commit newly covers: records below the committed offsets that completed since the previous commit of each partition, including a commit the handler requested. It comes from completion tracking rather than offset arithmetic, so compacted offsets and transaction markers add nothing. A seek commit reports zero unless it commits the completed frontier.

Low-Level Rebalance Callbacks​

RunPartitionedAsync builds on the lower-level rebalance lifecycle described in consumer groups. For most partitioned work, use RunPartitionedAsync instead of writing your own channel-per-partition dispatcher.

Use IRebalanceListener directly when you need full control over assignment state, custom queues, or integration with an existing processing runtime. The same safety rule applies: commit completed offsets on revoke or graceful stop, but do not commit offsets for lost partitions unless your application has a separate ownership guarantee.

Migrating From Other APIs​

Confluent.Kafka​

Map Confluent rebalance handlers to Dekaf callbacks:

Confluent.KafkaDekaf
SetPartitionsAssignedHandlerIRebalanceListener.OnPartitionsAssignedAsync
SetPartitionsRevokedHandlerIRebalanceListener.OnPartitionsRevokedAsync
SetPartitionsLostHandlerIRebalanceListener.OnPartitionsLostAsync

If your Confluent consumer starts one task or channel per assigned partition, replace that dispatcher with RunPartitionedAsync. Move the per-partition work into the processor callback, call MarkProcessed after durable processing, and let CommitPolicy handle revoke and shutdown commits.

Akka.Streams.Kafka​

RunPartitionedAsync maps most closely to partitioned sources where each assigned partition becomes an ordered substream. The processor callback is the substream body, partition.Messages is the ordered stream, and MarkProcessed is the point where a committable offset becomes eligible for commit.

If your Akka.Streams.Kafka flow commits offsets in a partition handler during revoke, use CommitCompletedOnRevoke or call CommitProcessedAsync inside the Dekaf processor. Keep per-partition ordering assumptions inside one processor invocation, not in shared mutable state.