# ConsumerConfig

**Kind:** Interface

**Source:** [`packages/microservices/external/kafka.interface.ts`](https://github.com/nestjs/nest/blob/master/packages/microservices/external/kafka.interface.ts#L164)

**Part of:** [Microservices](subsystem-packages-microservices)

`ConsumerConfig` defines configuration options for a Kafka consumer used by the microservices Kafka transport. It controls consumer group membership, partition assignment and rebalancing behavior, heartbeat/session timing, and message fetch batch limits.

## Properties

| Property | Type |
|---|---|
| `groupId` | `string` |
| `partitionAssigners` | `PartitionAssigner[]` |
| `metadataMaxAge` | `number` |
| `sessionTimeout` | `number` |
| `rebalanceTimeout` | `number` |
| `heartbeatInterval` | `number` |
| `maxBytesPerPartition` | `number` |
| `minBytes` | `number` |
| `maxBytes` | `number` |
| `maxWaitTimeInMs` | `number` |
| `retry` | `RetryOptions & { restartOnFailure?: (err: Error) => Promise<boolean>; }` |
| `allowAutoTopicCreation` | `boolean` |
| `maxInFlightRequests` | `number` |
| `readUncommitted` | `boolean` |
| `rackId` | `string` |

## Diagram

```mermaid
graph LR
  App[Microservice Consumer] --> Config[ConsumerConfig]
  Config --> Group[groupId]
  Config --> Assignment[partitionAssigners]
  Config --> Rebalancing[Session, rebalance, and heartbeat settings]
  Config --> Fetching[Fetch size and wait-time settings]
  Group --> Kafka[Kafka Consumer Group]
  Assignment --> Kafka
  Rebalancing --> Kafka
  Fetching --> Kafka
```

## Usage

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

const consumerConfig: ConsumerConfig = {
  groupId: 'billing-service',
  partitionAssigners: [],
  metadataMaxAge: 300_000,
  sessionTimeout: 30_000,
  rebalanceTimeout: 60_000,
  heartbeatInterval: 3_000,
  maxBytesPerPartition: 1_048_576,
  minBytes: 1,
  maxBytes: 10_485_760,
  maxWaitTimeInMs: 5_000,
};

// Pass consumerConfig to the Kafka transport/client initialization.
```

## AI Coding Instructions

- Use a stable, service-specific `groupId`; consumers sharing a group ID load-balance partitions.
- Keep `heartbeatInterval` meaningfully lower than `sessionTimeout` to prevent unintended consumer removal.
- Ensure `maxBytes` is large enough for the expected aggregate batch size and is compatible with broker fetch limits.
- Configure `partitionAssigners` consistently across consumers in the same group to avoid rebalance compatibility issues.
- Treat timeout and fetch-size changes as operational tuning values; validate them against message volume and processing duration.

## How it works

`ConsumerConfig` is an exported TypeScript interface representing the KafkaJS consumer-creation options in this repository’s Kafka type surface; the file explicitly states that it contains KafkaJS types only, not NestJS logic. [`packages/microservices/external/kafka.interface.ts:1-8`](packages/microservices/external/kafka.interface.ts#L1-L8) A `Kafka` instance accepts this type as the required argument to `consumer()` and returns a `Consumer`. [`packages/microservices/external/kafka.interface.ts:19-24`](packages/microservices/external/kafka.interface.ts#L19-L24)
