Skip to content

OffsetsByTopicPartition

reference
1 min readUpdated

Kind: Interface

Source: packages/microservices/external/kafka.interface.ts

Part of: Microservices

OffsetsByTopicPartition groups Kafka offset information by topic and partition. It is used when reading, committing, or reporting consumer positions across one or more Kafka topics in the microservices Kafka integration.

Properties

PropertyType
topicsTopicOffsets[]

Diagram

mermaid
graph LR
  A[OffsetsByTopicPartition] --> B[topics: TopicOffsets[]]
  B --> C[Topic offset entry]
  C --> D[Kafka topic]
  C --> E[Partition offsets]

Usage

ts
import type { OffsetsByTopicPartition } from './kafka.interface';

const offsets: OffsetsByTopicPartition = {
  topics: [
    {
      topic: 'orders',
      partitions: [
        { partition: 0, offset: '142' },
        { partition: 1, offset: '98' },
      ],
    },
    {
      topic: 'payments',
      partitions: [{ partition: 0, offset: '51' }],
    },
  ],
};

// Pass the grouped offsets to Kafka consumer offset handling logic.
await consumer.commitOffsets(offsets.topics.flatMap((topic) => topic.partitions));

AI Coding Instructions

  • Populate topics with TopicOffsets entries; do not place partition offsets directly on OffsetsByTopicPartition.
  • Preserve Kafka offsets as strings when required by the underlying client to avoid JavaScript integer precision issues.
  • Group offsets by their Kafka topic before constructing this interface.
  • Validate that each partition belongs to the topic named by its corresponding TopicOffsets entry.
  • Use this type at Kafka consumer integration boundaries for offset commit, seek, or lag-reporting workflows.

Was this page helpful?

Download as PDF
OffsetsByTopicPartition — NestJS head-to-head