Apache Kafka design guide: choosing partitions, consumer groups, and offsets
Apache Kafka is an event-streaming platform that records events in topic partition logs and lets consumers read them by advancing offsets. Records are not deleted just because they were consumed. Topic retention determines how long data remains, so Kafka design must account for partition keys, ordering scope, consumer groups, offset commits, and retention limits as well as throughput.
Apache Kafka is an event-streaming platform that records events in topic partition logs and lets consumers read them by advancing offsets. Records are not deleted just because they were consumed. Topic retention determines how long data remains, so Kafka design must account for partition keys, ordering scope, consumer groups, offset commits, and retention limits as well as throughput.
Kafka’s storage and reading model
A producer writes records with a key, value, timestamp, and optional headers to a topic. A topic is divided into partitions, and each record is appended to one partition log. Partitions enable distributed storage and parallel processing. Events with the same key can be routed to the same partition; ordering is guaranteed only within that partition, not globally across the topic.
Kafka does not delete a record when it is read. Topic retention settings eventually discard older records, while different consumers can independently read the same data again. Replay therefore depends on retention and records still available, not simply on whether another consumer read them.
| Need | Kafka design choice | Boundary to check |
|---|---|---|
| High throughput and horizontal parallelism | Split a topic into partitions | How do partition count and key distribution affect parallelism? |
| Order events for a customer or account | Use that business key as the record key | Ordering is only within a partition; skew can overload one partition |
| Several services independently read the same data | One consumer group per service | How are each group’s offsets, replay, and retention managed? |
| Several workers divide tasks | Use the same consumer group | Each partition is assigned to one group member; extra consumers beyond the partition count do not add parallelism |
| Resume after failure | Commit offsets in relation to processing results | Commit before processing can skip work; commit after processing can cause duplicates |
Design consumer groups and offsets together
Consumers with the same group.id divide partitions. Each partition is assigned to one consumer in that group, so the group acts like a work-sharing pool. Separate groups let separate services consume topic records independently. When consumers join, leave, or partition assignments change, the group rebalances, which can affect assignments and throughput.
Each partition has record offsets. A consumer has a current position for the next record and a committed position used after restart. Automatic commits store positions periodically; with manual commits, the application chooses when records count as processed.
If the application processes a record and then commits its offset, a crash between those steps can cause the record to be processed again after restart. If it commits first, then crashes before saving the result, the record can be skipped. For an external database update, consider storing the DB result and offset in one transaction; otherwise use idempotency or an outbox/inbox pattern.
Understand the boundary of “exactly once”
Kafka provides idempotent producers to suppress duplicate log entries during retries and transactions to atomically write output records and consumed offsets to Kafka. With the right configuration and processing pattern, these can make Kafka input-to-output flows atomic. They do not automatically make effects in an external database, payment API, or email system happen exactly once. Coordinate external state with offsets or handle duplicates safely.
Producer durability also depends on required acknowledgements, replication state, and other settings. Do not treat the phrase “Kafka is durable” as a blanket promise of zero loss during failures or successful processing by every consumer. Verify replication, producer acknowledgements, consumer commits, retention, and recovery procedures against the data’s importance.
Before adoption
**Define the event contract.** Specify topic, key, value schema, timestamp, compatibility rules, ownership, and whether personal data is included.
**Design partitions and keys.** Choose the required ordering scope, then assess key skew, hot partitions, target parallelism, and the effect of changing partition count.
**Separate groups by business responsibility.** Give independent subscribers separate groups; share a group only among workers dividing the same work.
**Set processing and commit order.** Choose automatic or manual commits and document how to avoid skips or duplicates during slow processing, rebalance, and failures.
**Calculate retention and replay.** Match the required replay window, maximum consumer outage, data size, and retention limits. Once a record expires, an offset cannot restore it.
**Review failure paths.** Stop a consumer before and after processing to inspect restart position and duplicates. Separately examine group rebalancing and external-store commit failures.
These are design steps, not a claim that a Kafka cluster was tested. Verify operational settings and guarantees against the deployed broker and client versions.
Sources and scope
Apache Kafka 4.1, [Introduction](https://kafka.apache.org/41/getting-started/introduction/)
Apache Kafka 4.1, [Design](https://kafka.apache.org/41/design/design/)
Apache Kafka 4.1.2, [KafkaConsumer API](https://kafka.apache.org/41/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html)
Topic, partition, retention, and delivery descriptions are limited to the Kafka 4.1 documentation and client 4.1.2 API read here. Ordering, replay, and delivery depend on configuration, partitions, and consumer behavior; do not generalize unchanged to other versions or distributions.