Kafka protocol request families
The Kafka protocol layer groups broker messages by the job they support. Producer delivery uses produce and transaction messages. Consumer retrieval uses metadata, offset, fetch, group, coordination, and commit messages. The admin client uses metadata, coordination, commit, and admin messages to inspect or change cluster state. See Kafka protocol model for the shared model behind these families.
Request family model
Each family presents a stable request builder to the library and maps it to a versioned wire structure. The builder keeps the caller's intent while the connection's negotiated API range selects a compatible request version. Each selected request structure also identifies the response type that can decode the broker reply.
The library consumes the families through long-lived client tasks rather than through direct application access to wire structures:
| Consumer of messages | Main protocol role | Result used by the library |
|---|---|---|
| Producer sender | Produce and transaction control | Delivery results and transaction state |
| Consumer fetcher | Fetch and offset lookup | Records and fetch positions |
| Group coordinator | Coordinator lookup, group membership, and commits | Assignment, liveness, and stored offsets |
| Admin client | Metadata, coordination, offset inspection, and cluster administration | Cluster descriptions and admin results |
The main path from an application action to a broker reply is shown below.
flowchart LR
A[Application action] --> B[Client subsystem]
B --> C[Request family builder]
C --> D[Negotiated API version]
D --> E[Versioned request structure]
E --> F[Broker]
F --> G[Matching response structure]
G --> B
Version selection
Version selection lets one logical operation work across broker versions. The request builder chooses the highest supported version within the broker's advertised range. If the operation needs a field that the chosen version cannot carry, the builder rejects it instead of sending a misleading request.
The API key identifies the family. The API version identifies the schema for that family. A response is decoded with the response type attached to the selected request version.
References: aiokafka/…/api.py• L67-142 aiokafka/…/api.py• L145-198 aiokafka/…/__init__.py• L1-46
Data movement families
These families carry records and the positions needed to read them. They connect the producer sender and consumer fetcher to partition leaders or to a broker selected for reading.
Produce messages
The produce family sends batches grouped by topic and partition. The producer sender routes each batch to the broker that leads its partition, then uses the partition result to mark the batch complete, retry it, or report an error. Transactional sends include the transaction identity when the producer uses transactions.
Produce responses return partition errors and the broker offset. Later response versions add timing, log-start, and record-level error information. The sender reads the response according to its selected version, so response fields remain tied to the request version.
References: aiokafka/…/produce.py• L9-24 aiokafka/…/produce.py• L171-217 aiokafka/…/sender.py• L277-292 aiokafka/…/sender.py• L751-890
Fetch messages
The fetch family reads records from assigned partitions. The consumer fetcher groups partition positions by broker and runs fetches in the background. It places successful records into a local queue for consumer calls to read later.
Fetch versions add the controls needed for modern consumption. These include isolation level for committed reads, incremental fetch sessions, leader checks, and rack-aware preferred replicas. The fetcher also uses response data such as aborted transactions and the last stable offset to hide records that a committed reader must not see.
References: aiokafka/…/fetch.py• L9-27 aiokafka/…/fetch.py• L68-93 aiokafka/…/fetch.py• L354-389 aiokafka/…/fetch.py• L457-509 aiokafka/…/fetcher.py• L474-580 aiokafka/…/fetcher.py• L742-912
Offset lookup messages
The offset family asks a partition leader for an offset by time or by an earliest/latest reset rule. The consumer uses it when a partition has no usable position or when the current position is outside the available log range. It also exposes offset lookup for consumer operations that search by timestamp.
Older versions use a list of offsets while newer versions return one offset with its timestamp. The request builder converts the common library input to the selected layout and refuses features that the selected broker version cannot support.
References: aiokafka/…/offset.py• L9-13 aiokafka/…/offset.py• L15-126 aiokafka/…/offset.py• L129-279 aiokafka/…/fetcher.py• L918-997 aiokafka/…/fetcher.py• L999-1161
Cluster and consumer state families
These families describe where data lives and maintain the state that lets consumers work as a group. Metadata supplies placement facts. Coordination supplies the broker that owns group or transaction state. Group messages manage membership. Commit messages store and read consumer progress.
Metadata messages
The metadata family describes brokers, topics, partitions, leaders, replicas, and controller state. Producers and consumers depend on this view to route requests. The admin client also uses it to list or describe cluster topics and to find controller or partition targets.
Metadata refresh is part of normal recovery. When a request reports stale leadership or an unknown partition, the owning subsystem asks for new metadata before it retries or rebuilds its route.
References: aiokafka/…/metadata.py• L7-32 aiokafka/…/metadata.py• L106-184 aiokafka/…/metadata.py• L187-293 aiokafka/…/fetcher.py• L582-668 aiokafka/…/client.py• L234-263
Coordinator lookup messages
The coordination family finds the broker that owns a consumer group or a transaction identity. A coordinator is the broker to which group and transaction state messages must be sent. The client caches that choice until an error or connection event shows that it is no longer valid.
The request key changes meaning by version. Newer versions can state whether the lookup is for a group or a transaction. Older versions only identify a consumer group, so transaction lookup is rejected when that distinction is required.
References: aiokafka/…/coordination.py• L9-69 aiokafka/…/group_coordinator.py• L550-589 aiokafka/…/sender.py• L193-267 aiokafka/…/client.py• L473-488
Group messages
The group family manages membership and partition assignment. A consumer joins a group, one member performs the assignment, members receive the assignment, and members send heartbeats while they work. A leave message closes a normal membership session.
The group protocol carries member metadata and assignments as bytes. The group coordinator interprets those payloads with the consumer protocol, then turns the broker's generation and member state into local assignment state. A failed heartbeat or a changed subscription can therefore trigger a new join cycle.
References: aiokafka/…/group.py• L8-18 aiokafka/…/group.py• L62-178 aiokafka/…/group.py• L181-293 aiokafka/…/group.py• L296-389 aiokafka/…/group_coordinator.py• L188-220 aiokafka/…/group_coordinator.py• L697-740 aiokafka/…/group_coordinator.py• L799-861
Commit messages
The commit family stores and reads the offsets that represent a group's progress. Offset commit writes partition positions with group generation and member identity. Offset fetch reads those positions when a new assignment needs an initial position or when the application asks for committed offsets.
The group coordinator sends these messages to the group coordinator. It refreshes stored offsets after assignment and commits consumed offsets on the configured schedule or at an explicit commit point. A missing stored offset is represented as an unknown position and is resolved by the consumer's reset policy.
References: aiokafka/…/commit.py• L9-163 aiokafka/…/commit.py• L166-303 aiokafka/…/group_coordinator.py• L944-1015 aiokafka/…/group_coordinator.py• L1018-1138 aiokafka/…/group_coordinator.py• L1140-1224
Administration families
Administration messages support cluster inspection and controlled changes outside the record data path. The admin client chooses the target broker from metadata: controller operations go to the controller, partition operations go to partition leaders, and broker-local queries go to the named broker.
Admin messages
The admin family covers API version discovery, topic and partition management, record deletion, group inspection and deletion, configuration inspection and changes, access control operations, authentication support, client quota inspection, and partition reassignment. These operations return broker results with per-resource or per-partition errors so the admin client can expose the outcome for each requested target.
The admin client combines these messages with metadata and coordinator lookup. For example, topic creation and deletion use the controller, group description first finds each group's coordinator when needed, and record deletion first maps each partition to its leader. Version-specific builders also reject options that an older broker cannot carry.
References: aiokafka/…/admin.py• L23-87 aiokafka/…/admin.py• L90-535 aiokafka/…/admin.py• L537-1274 aiokafka/…/admin.py• L1277-1595 aiokafka/…/admin.py• L1598-1747 aiokafka/…/client.py• L168-232 aiokafka/…/client.py• L265-387 aiokafka/…/client.py• L389-471 aiokafka/…/client.py• L543-582
Transaction families
Transaction messages connect producer delivery with atomic commit or abort state. The transaction manager tracks the producer identity, the partitions touched by the transaction, and any consumer group whose offsets must be included. The sender then performs the required protocol steps in order.
Transaction messages
The transaction family initializes a producer identity, adds touched partitions, adds a consumer group to the transaction, commits offsets for that group, and ends the transaction with a commit or abort result. These messages go to the transaction coordinator except transactional offset commit, which goes to the group coordinator.
The normal transaction sequence is shown below. The sender blocks conflicting work while it completes the final transaction step, which keeps produced records and included offsets under one transaction result.
sequenceDiagram
participant S as Producer sender
participant T as Transaction coordinator
participant G as Group coordinator
S->>T: InitProducerId
S->>T: AddPartitionsToTxn
S->>T: AddOffsetsToTxn
S->>G: TxnOffsetCommit
S->>T: EndTxn(commit or abort)
Transaction errors have different meanings. Coordinator errors cause discovery and retry. Authorization or state errors can make the transaction abortable. A fenced producer or fatal sender error stops safe continuation.
References: aiokafka/…/transaction.py• L7-197 aiokafka/…/transaction.py• L200-273 aiokafka/…/transaction_manager.py• L30-56 aiokafka/…/transaction_manager.py• L118-237 aiokafka/…/sender.py• L298-384 aiokafka/…/sender.py• L427-748
Request family relationships
The families form a small set of dependencies rather than independent APIs. Metadata supplies routes for produce, fetch, and offset messages. Coordinator lookup supplies the destination for group and transaction messages. Group state supplies the generation and member identity required by commit messages. Transaction state adds a second coordinator path around produce and commit messages.
The consumer lifecycle combines these relationships in a predictable order:
- Metadata identifies the partition leaders.
- Coordinator lookup identifies the group broker.
- Group messages create or refresh the assignment.
- Offset fetch finds the starting position for each assigned partition.
- Offset lookup resolves reset rules when no usable position exists.
- Fetch messages retrieve records.
- Offset commit records progress for later assignments.
This relationship explains why a single broker error can affect more than one family. A changed leader requires metadata refresh. A changed group coordinator requires coordinator lookup. A changed group generation requires a new group join before a commit can succeed.
References: aiokafka/…/group_coordinator.py• L550-589 aiokafka/…/group_coordinator.py• L914-1224 aiokafka/…/fetcher.py• L582-668 aiokafka/…/fetcher.py• L918-1161 aiokafka/…/sender.py• L193-267
Change checklist
When these files or sections are changed, remember to consider these:
- Keep each logical request's version union aligned with its builder's feature gates. A schema class that exists but is not selectable cannot support a broker feature.
- Keep each request structure's response type aligned with the response parsing branches in the producer, consumer, admin, or transaction caller. A response field change can silently shift tuple positions.
- When a metadata or coordinator error changes routing, preserve the refresh and retry path for every caller that sends that family.
- When group or commit fields change, check the generation, member identity, assignment, and stored-offset flow as one contract.
- When transaction messages change, keep the coordinator target split: transaction control goes to the transaction coordinator, while transactional offset commit goes to the group coordinator.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The broker's advertised API range has no supported request version | The request cannot be prepared and the operation fails before transmission | Add the version structure and its builder path before using the new feature |
| A caller asks an older version for a field such as transactional identity, isolation level, or coordinator type | The builder raises an incompatible-version error instead of sending an incomplete request | Keep feature checks in the family builder |
| Leadership or partition metadata is stale | Produce, fetch, or offset work reaches the wrong broker and must wait for a metadata refresh | Treat leader and unknown-partition errors as route invalidation |
| A group coordinator rejects the current generation or member | Commit or heartbeat work fails and the consumer must rejoin | Reset generation or request a rebalance before retrying group work |
| A transaction coordinator rejects the producer epoch or transaction state | The producer is fenced or the transaction becomes abortable or fatal | Stop unsafe continuation and let the transaction manager expose the state |
| A committed fetch response contains aborted transactional data | A read-committed consumer could return records that must not be visible | Use the fetch response's isolation fields and aborted transaction list |
Key files
| File | Why to read it |
|---|---|
| aiokafka/…/api.py | Defines the common request builder, version selection, headers, and response pairing |
| aiokafka/…/produce.py | Shows the record delivery family and its versioned response forms |
| aiokafka/…/fetch.py | Shows the record retrieval family and its isolation and session changes |
| aiokafka/…/metadata.py | Shows the cluster route data used by most data and admin flows |
| aiokafka/…/offset.py | Shows reset and timestamp-based offset lookup |
| aiokafka/…/group.py | Shows membership, heartbeat, assignment, and leave messages |
| aiokafka/…/commit.py | Shows stored offset write and read messages |
| aiokafka/…/coordination.py | Shows group and transaction coordinator lookup |
| aiokafka/…/admin.py | Shows cluster administration request families |
| aiokafka/…/transaction.py | Shows the messages that make producer transactions atomic |
| aiokafka/…/fetcher.py | Shows how fetch and offset messages become consumer records and positions |
| aiokafka/…/group_coordinator.py | Shows how group, coordination, and commit messages drive consumer membership |
| aiokafka/…/sender.py | Shows how produce and transaction messages are scheduled and consumed |
| aiokafka/…/client.py | Shows how admin operations select controllers, leaders, coordinators, and brokers |
| aiokafka/…/transaction_manager.py | Shows the transaction state that determines which transaction message comes next |