ConsumerConfig is the class in the Kafka Java client (org.apache.kafka.clients.consumer.ConsumerConfig) that defines, documents and validates every setting a KafkaConsumer accepts. It holds a constant for each property name, such as ConsumerConfig.GROUP_ID_CONFIG, plus the defaults, types and validation rules behind them. You normally use it as a source of safe property keys rather than creating instances yourself.
When you build a consumer, the KafkaConsumer constructor passes your Properties or Map into a ConsumerConfig, which parses and validates the values and fails fast with a ConfigException if something is wrong.
ConsumerConfig extends AbstractConfig, the base class Kafka uses for producer, consumer, admin and broker-side client settings. It has three jobs:
ConfigDef: name, type, default, valid values, importance and documentation."bootstrap.server" instead of "bootstrap.servers".ConfigException.You can list every key your client version supports with ConsumerConfig.configNames(). This is useful because defaults change between releases, so always check the documentation of the client version you ship.
The following example uses the constants instead of raw strings, subscribes to a topic and processes records in a poll loop.
The bootstrap servers, key deserializer and value deserializer are required. Everything else has a default. Here auto commit is turned off, so commitSync() records progress only after the batch was processed.
| Constant | Property name | Purpose | Default |
|---|---|---|---|
BOOTSTRAP_SERVERS_CONFIG | bootstrap.servers | Initial brokers used to discover the cluster | none (required) |
GROUP_ID_CONFIG | group.id | Consumer group this consumer belongs to | none |
KEY_DESERIALIZER_CLASS_CONFIG | key.deserializer | Converts key bytes to objects | none (required) |
VALUE_DESERIALIZER_CLASS_CONFIG | value.deserializer | Converts value bytes to objects | none (required) |
AUTO_OFFSET_RESET_CONFIG | auto.offset.reset | Where to start when no committed offset exists: latest, earliest or none | latest |
ENABLE_AUTO_COMMIT_CONFIG | enable.auto.commit | Commit offsets periodically in the background | true |
MAX_POLL_RECORDS_CONFIG | max.poll.records | Maximum records returned by one poll() | 500 |
MAX_POLL_INTERVAL_MS_CONFIG | max.poll.interval.ms | Longest gap between poll() calls before the consumer is considered failed | 300000 |
SESSION_TIMEOUT_MS_CONFIG | session.timeout.ms | Time without heartbeats before the broker removes the consumer | 45000 (since Kafka 3.0) |
ISOLATION_LEVEL_CONFIG | isolation.level | read_committed hides records from aborted transactions | read_uncommitted |
auto.offset.reset only applies when the group has no committed offset for a partition, or the committed offset no longer exists. With latest a new group skips existing history. With earliest it reads from the start. With none it throws an exception instead.
With enable.auto.commit=true, offsets are committed in the background, so a crash after a commit but before processing finishes can lose records. Disabling it and committing after processing gives at-least-once delivery. Exactly-once processing needs transactions or an idempotent sink.
fetch.min.bytes and fetch.max.wait.ms control how long the broker waits to batch data before answering a fetch. Larger values favour throughput, smaller values favour latency. max.poll.records limits the work done per loop iteration, which also helps stay within max.poll.interval.ms.
The consumer sends heartbeats from a background thread every heartbeat.interval.ms (3 seconds by default). If the broker sees none for session.timeout.ms, or your code does not call poll() within max.poll.interval.ms, the consumer leaves the group and a rebalance starts.
Setting group.instance.id enables static membership, so a restarting consumer can rejoin without triggering a rebalance. partition.assignment.strategy selects the assignor, and CooperativeStickyAssignor reduces disruption during rebalances. In Kafka 4.0 and later, the new consumer group protocol (KIP-848) can be enabled with group.protocol=consumer, which moves assignment to the broker.
Spring Kafka builds the same properties for you. You can set them in application.yml under spring.kafka.consumer, or build a DefaultKafkaConsumerFactory with a map keyed by the same ConsumerConfig constants:
Usually not. You pass properties to KafkaConsumer, which creates the ConsumerConfig internally. You use the class mainly for its constants and, occasionally, configNames().
An unknown key is only logged as a warning and the setting is ignored, which can hide mistakes. Using the ConsumerConfig constants avoids this class of bug.
The usual cause is slow processing that exceeds max.poll.interval.ms. Reduce max.poll.records, speed up processing, or raise the interval.
It is required when you use subscribe() or commit offsets. It is not needed when you assign partitions manually with assign() and do not commit.
ConsumerConfig is the single place where the Kafka consumer's settings are named, typed, defaulted and validated. Use its constants to avoid typos, set the required bootstrap servers and deserializers, and tune offset handling, fetch sizes and group timeouts to match your delivery and latency needs. Check defaults against the client version you run, because they change between releases.