apache/kafka · error · IllegalStateException
Tried to force a rebalance but consumer does not have a grou
Error message
Tried to force a rebalance but consumer does not have a group.
What it means
Thrown by KafkaConsumer.enforceRebalance(...) when the consumer has no group coordinator, i.e. group.id is not set (or coordinator was never created). enforceRebalance only makes sense for consumers participating in a group; without one there is nothing to rejoin, so the call is invalid. This is a client-side check before any network interaction.
Source
Thrown at clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java:1089
}
@Override
public ConsumerGroupMetadata groupMetadata() {
acquireAndEnsureOpen();
try {
throwIfGroupIdNotDefined();
return coordinator.groupMetadata();
} finally {
release();
}
}
@Override
public void enforceRebalance(final String reason) {
acquireAndEnsureOpen();
try {
if (coordinator == null) {
throw new IllegalStateException("Tried to force a rebalance but consumer does not have a group.");
}
coordinator.requestRejoin(reason == null || reason.isEmpty() ? DEFAULT_REASON : reason);
} finally {
release();
}
}
@Override
public void enforceRebalance() {
enforceRebalance(null);
}
@Override
public void close() {
close(CloseOptions.timeout(Duration.ofMillis(DEFAULT_CLOSE_TIMEOUT_MS)));
}
@DeprecatedView on GitHub (pinned to c31c9215e1)
Solutions
- Only call enforceRebalance() on consumers that use subscribe() with a group.id.
- Set group.id in consumer config if you intend to participate in a group and force rejoin.
- Remove the call from manual-assignment (assign()) code paths; manual assignment does not rebalance.
Example fix
// before (manual assignment)
consumer.assign(List.of(tp));
consumer.enforceRebalance(); // throws
// after - remove the call for assign()-based consumers
consumer.assign(List.of(tp));
// or, if you want rebalances, switch to subscribe:
// props.put(ConsumerConfig.GROUP_ID_CONFIG, "g1");
// consumer.subscribe(List.of("topic")); Defensive patterns
Strategy: validation
Validate before calling
// enforceRebalance() requires a group; skip it for assign() (no-group) consumers:
if (groupId != null && !groupId.isEmpty()) {
consumer.enforceRebalance("operator-triggered");
} else {
log.debug("Skipping enforceRebalance: consumer has no group (manual assignment)");
} Type guard
// Distinguish grouped vs standalone consumers at the type level:
sealed interface KafkaClient permits GroupedConsumer, StandaloneConsumer {}
final class GroupedConsumer {
final String groupId;
void enforceRebalance(String reason) { /* delegate */ }
}
final class StandaloneConsumer {
// no enforceRebalance method exists at all — the call site won't compile
} Try / catch
// Only swallow this specific misconfiguration; let other IllegalState exceptions propagate:
try {
consumer.enforceRebalance();
} catch (IllegalStateException e) {
if (!e.getMessage().contains("does not have a group")) throw e;
log.info("enforceRebalance ignored: standalone consumer");
} Prevention
- Gate every enforceRebalance() call behind the same condition that decided assign() vs subscribe() — if you don't have a group, you can't rebalance.
- Keep group membership a single, explicit flag in your consumer factory so call sites can branch on it without guessing.
- Don't call enforceRebalance() defensively in a shutdown hook; closing a standalone consumer will already do the right thing.
When it happens
Trigger: Calling consumer.enforceRebalance() on a consumer built with assign() (manual assignment, no group.id); calling it when group.id is null or empty; using enforceRebalance in a standalone consumer to 'refresh'.
Common situations: Generic utility code that always calls enforceRebalance regardless of subscribe vs assign; misconfigured deployments missing group.id; copying code from a subscribing consumer into an assigning one.
Related errors
- Telemetry is not enabled. Set config `{}` to `true`.
- The timeout cannot be negative.
- Invalid value null for configuration key.deserializer: must
- Invalid value null for configuration value.deserializer: mus
- enable.auto.commit cannot be set to true when default group
AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03).
Data as JSON: /data/errors/4c875c55281c1c7a.json.
Report an issue: GitHub.