Knowledge Base updated: 2026-11-22
aiokafka knowledge base
aiokafka is an asyncio-native Python client for Apache Kafka. Its public producer, consumer, and admin clients share a metadata-aware network client, Kafka protocol codecs, and record encoding support.
Critical runtime state lives in producer accumulators and transaction management, consumer subscription/fetch/group-coordinator state, and the shared client's cluster metadata and broker connections.
System architecture
flowchart TD
App[Application] --> Producer[AIOKafkaProducer]
App --> Consumer[AIOKafkaConsumer]
App --> Admin[AIOKafkaAdminClient]
Producer --> Accumulator[MessageAccumulator]
Producer --> Sender[Sender]
Producer --> Txn[TransactionManager]
Consumer --> Fetcher[Fetcher]
Consumer --> Coordinator[GroupCoordinator]
Consumer --> Subscription[SubscriptionState]
Admin --> Client[AIOKafkaClient]
Sender --> Client
Fetcher --> Client
Coordinator --> Client
Client --> Cluster[ClusterMetadata]
Client --> Conn[Broker Connection]
Conn --> Kafka[Kafka brokers]
flowchart LR
Send[producer.send] --> Batch[Record batch accumulator]
Batch --> Sender[Sender]
Sender --> Request[Produce request]
Request --> Broker[Broker]
Broker --> Response[Produce response]
Response --> Result[Delivery future]
flowchart LR
Subscribe[subscribe or assign] --> State[SubscriptionState]
State --> Coord[GroupCoordinator]
Coord --> Fetcher[Fetcher]
Fetcher --> Records[Consumer records]
Records --> Position[Partition positions]
Position --> Commit[Offset commit]
Glossary
- Accumulator: per-topic-partition producer buffering that forms record batches before sending.
- SubscriptionState: consumer-owned state for subscriptions, assignment, seek positions, and partition fetchability.
- GroupCoordinator: consumer component that manages group membership, heartbeats, rebalances, and committed offsets.
- TransactionManager: producer state machine for idempotent sequencing and Kafka transactions.
- ClusterMetadata: the shared representation of brokers, topic partitions, leaders, and coordinator locations.
Codebase-wide invariants
- Broker-bound requests are routed through cluster metadata and connection correlation handling (aiokafka/client.py, aiokafka/conn.py).
- Producer delivery completion is driven by batch/request outcomes rather than direct caller I/O (aiokafka/…/message_accumulator.py, aiokafka/…/sender.py).
- Consumer record delivery depends on assigned partitions and maintained fetch positions (aiokafka/…/fetcher.py, aiokafka/…/subscription_state.py).
- Transactional producer state coordinates with record delivery and transactional protocol requests (aiokafka/…/transaction_manager.py, aiokafka/…/producer.py).
- Kafka wire messages use shared protocol schemas and record encoders/decoders (
aiokafka/protocol/**,aiokafka/record/**).
Routing table
- aiokafka/…/producer.py -> docs/producer-subsystem.md
- aiokafka/…/message_accumulator.py -> docs/producer-delivery-pipeline.md
- aiokafka/…/sender.py -> docs/producer-delivery-pipeline.md
- aiokafka/…/transaction_manager.py -> docs/producer-transactions.md
- aiokafka/…/consumer.py -> docs/consumer-subsystem.md
- aiokafka/…/fetcher.py -> docs/consumer-fetch-and-position.md
- aiokafka/…/subscription_state.py -> docs/consumer-fetch-and-position.md
- aiokafka/…/group_coordinator.py -> docs/consumer-group-coordination.md
- aiokafka/client.py -> docs/client-and-cluster-connectivity.md
- aiokafka/conn.py -> docs/connection-transport-and-authentication.md
- aiokafka/cluster.py -> docs/cluster-metadata-and-routing.md
aiokafka/admin/**-> docs/admin-client.md- aiokafka/…/config_resource.py -> docs/admin-resources-and-operations.md
- aiokafka/…/new_partitions.py -> docs/admin-resources-and-operations.md
aiokafka/protocol/**-> docs/kafka-protocol-model.md- aiokafka/…/produce.py -> docs/protocol-request-families.md
- aiokafka/…/fetch.py -> docs/protocol-request-families.md
- aiokafka/…/group.py -> docs/protocol-request-families.md
aiokafka/record/**-> docs/record-encoding-and-decoding.md- aiokafka/…/legacy_records.py -> docs/record-batch-formats.md
- aiokafka/…/default_records.py -> docs/record-batch-formats.md
aiokafka/coordinator/assignors/**-> docs/group-assignment-strategies.md- aiokafka/util.py -> docs/observability-and-client-utilities.md
aiokafka/metrics/**-> docs/metrics-instrumentation.md- aiokafka/…/producer.py -> docs/metrics-instrumentation.md
- aiokafka/…/consumer.py -> docs/metrics-instrumentation.md
- aiokafka/client.py -> docs/metrics-instrumentation.md