This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: kafka-3-9-kraft-idempotent-producer-standards

Kafka 3.9 KRaft Metadata Quorum & Exactly-Once Idempotent Producer Invariants

Enforces strict configuration and runtime transactional semantics for Kafka 3.9 ZooKeeper-less (KRaft) metadata quorums. Guarantees message deduplication via idempotent producer settings and prevents split-brain partition states during controller leader elections and cooperative sticky consumer group rebalances.

Prohibited Anti-Pattern: Non-idempotent producer configurations with blind retries and non-transactional commits causing data duplication during KRaft controller failover

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kafka 3.9 KRaft Metadata Quorum & Exactly-Once Idempotent Producer Invariants - Enforces strict configuration and runtime transactional semantics for Kafka 3.9 ZooKeeper-less (KRaft) metadata quorums. Guarantees message deduplication via idempotent producer settings and prevents split-brain partition states during controller leader elections and cooperative sticky consumer group rebalances.
globs: src/**/*.kafka.ts, src/**/kafka*.ts, src/**/producers/**/*.ts, src/**/consumers/**/*.ts, src/**/event-bus/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/kafka-3-9-kraft-idempotent-producer-standards"
---

# Kafka 3.9 KRaft Metadata Quorum & Exactly-Once Idempotent Producer Invariants

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Non-idempotent producer configurations with blind retries and non-transactional commits causing data duplication during KRaft controller failover

## System Constraints
You are a Principal Distributed Systems Engineer enforcing Kafka 3.9 KRaft and Idempotent Producer standards. Mandate zero data loss, strict idempotence, proper metadata quorum bootstrap handling, and transactional boundaries on message commit.

## Strict Guidelines
- Engineers MUST ALWAYS enable 'enable.idempotence': true and set 'acks': 'all' (-1) on all Kafka producers to prevent message duplication and out-of-order writes during KRaft controller leader transitions.
- Engineers MUST configure 'max.in.flight.requests.per.connection' to less than or equal to 5 when idempotence is enabled to preserve strict per-partition ordering.
- Engineers MUST NOT rely on deprecated ZooKeeper configuration parameters (`zookeeper.connect`); all connection brokers must point to the unified KRaft broker/controller bootstrap servers.
- Services MUST implement CooperativeStickyAssignor (`partition.assignment.strategy`: 'org.apache.kafka.clients.consumer.CooperativeStickyAssignor') to prevent stop-the-world rebalance storms in high-throughput consumer groups.
- Transactional producers MUST wrap message batches and offset commits in explicit `beginTransaction()`, `sendOffsetsToTransaction()`, and `commitTransaction()` blocks, handling `ProducerFencedException` with immediate termination.

## Bad vs Good Implementations
### Configuring Idempotent Producer with KRaft Rebalance & Transactional Guardrails

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Configuring acks: 1 without enabling idempotent delivery guarantees duplicate records if network glitches occur or if a KRaft metadata election shifts partition leadership before replica synchronization.
```typescript
import { Kafka } from 'kafkajs';

const kafka = new Kafka({
  clientId: 'order-service',
  brokers: ['localhost:9092'],
});

export const publishOrderEvents = async (orders: Array<{ id: string; payload: string }>) => {
  // Anti-Pattern: Missing idempotency and explicit ACKs
  const producer = kafka.producer();
  await producer.connect();
  
  for (const order of orders) {
    await producer.send({
      topic: 'orders',
      messages: [{ key: order.id, value: order.payload }],
      acks: 1, // Broker ack without quorum replica confirmation; vulnerable to KRaft leader failover
    });
  }
};
```

#### Production Standard (Correct)
> **Standard**: Enforces Kafka 3.9 idempotent guarantees with transactional boundaries, transactionalId, acks: -1 (all ISR replicas), and bounded in-flight requests, preventing duplication and split-brain phenomena.
```typescript
import { Kafka, Partitioners } from 'kafkajs';

const kafka = new Kafka({
  clientId: 'order-service-producer',
  brokers: (process.env.KAFKA_BROKERS || 'localhost:9092').split(','),
  retry: {
    initialRetryTime: 100,
    retries: 8,
    factor: 2,
  },
});

export const publishOrderEvents = async (orders: Array<{ id: string; payload: string }>) => {
  // Production Standard: Enforced idempotence, maxInFlight <= 5, and all acks
  const producer = kafka.producer({
    idempotent: true,
    maxInFlightRequests: 5,
    createPartitioner: Partitioners.DefaultPartitioner,
    transactionalId: 'order-tx-producer-01',
  });

  await producer.connect();
  const transaction = await producer.transaction();

  try {
    await transaction.send({
      topic: 'orders',
      acks: -1, // Wait for all in-sync replicas
      messages: orders.map(order => ({
        key: order.id,
        value: order.payload,
        headers: { 'kraft-quorum-sync': 'verified' },
      })),
    });
    await transaction.commit();
  } catch (error) {
    await transaction.abort();
    throw error;
  } finally {
    await producer.disconnect();
  }
};
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Kafka 3.9 KRaft Metadata Quorum & Exactly-Once Idempotent Producer Invariants

Kafka 3.9 KRaft Metadata Quorum & Exactly-Once Idempotent Producer Invariants defines an industrial-grade behavioral standard for backend engineering. Enforces strict configuration and runtime transactional semantics for Kafka 3.9 ZooKeeper-less (KRaft) metadata quorums. Guarantees message deduplication via idempotent producer settings and prevents split-brain partition states during controller leader elections and cooperative sticky consumer group rebalances. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Non-idempotent producer configurations with blind retries and non-transactional commits causing data duplication during KRaft controller failover Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftNon-idempotent producer configurations with blind retries and non-transactional commits causing data duplication during KRaft controller failoverCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS enable 'enable.idempotence'

Engineers MUST ALWAYS enable 'enable.idempotence': true and set 'acks': 'all' (-1) on all Kafka producers to prevent message duplication and out-of-order writes during KRaft controller leader transitions.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST configure 'max.in.flight.requests.per.connection' to less than or equal to 5 when idempotence is enabled to preserve strict per-partition ordering.

Engineers MUST configure 'max.in.flight.requests.per.connection' to less than or equal to 5 when idempotence is enabled to preserve strict per-partition ordering.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST NOT rely on deprecated ZooKeeper configuration parameters (`zookeeper.connect`)

Engineers MUST NOT rely on deprecated ZooKeeper configuration parameters (`zookeeper.connect`); all connection brokers must point to the unified KRaft broker/controller bootstrap servers.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST implement CooperativeStickyAssignor (`partition.assignment.strategy`

Services MUST implement CooperativeStickyAssignor (`partition.assignment.strategy`: 'org.apache.kafka.clients.consumer.CooperativeStickyAssignor') to prevent stop-the-world rebalance storms in high-throughput consumer groups.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Transactional producers MUST wrap message batches and offset commits in explicit `beginTransaction()`, `sendOffsetsToTransaction()`, and `commitTransaction()` blocks, handling `ProducerFencedException` with immediate termination.

Transactional producers MUST wrap message batches and offset commits in explicit `beginTransaction()`, `sendOffsetsToTransaction()`, and `commitTransaction()` blocks, handling `ProducerFencedException` with immediate termination.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Kafka 3.9 KRaft Metadata Quorum & Exactly-Once Idempotent Producer Invariants critical for AI coding assistants?

Enforces strict configuration and runtime transactional semantics for Kafka 3.9 ZooKeeper-less (KRaft) metadata quorums. Guarantees message deduplication via idempotent producer settings and prevents split-brain partition states during controller leader elections and cooperative sticky consumer group rebalances. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Non-idempotent producer configurations with blind retries and non-transactional commits causing data duplication during KRaft controller failover) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Kafka