Skip to content
Kafka · How it works

Designing Kafka topics and partitions you won't regret

Keys and ordering, how many partitions, replication and durability settings, retention versus compaction, schemas and naming: the topic decisions that are cheap on day one and expensive to change later.

Most Kafka decisions can be changed later. A few can't, or only by creating a new topic and migrating every producer and consumer to it. Those few are made on the day the topic is created, usually in a hurry, by whoever needed it first. This article covers the decisions worth ten minutes of thought before a topic goes to production.

1. The key decides the ordering

Kafka guarantees order within a partition, not across a topic. The producer picks a partition by hashing the message key, so all messages with the same key land in the same partition, in the order they were sent. That makes the key the most important design decision:

  • Key by the entity whose events must stay in order: the order id for order events, the account for ledger entries, the device for telemetry.
  • Watch for hot keys. One customer generating a large share of the traffic puts that share on one partition, and one consumer. If a key can be that hot, consider a more granular key, or accept and plan for the skew.
  • No key spreads messages across partitions for throughput, with no ordering at all. Fine for independent events such as clickstream; wrong for anything stateful.

2. How many partitions

Partitions are the unit of parallelism: each partition is read by at most one consumer in a consumer group (Kafka 4.x share groups relax this for queue-style consumption). Size for the throughput you expect in a year or two, divided by what one consumer can process, with headroom, rather than for today's traffic.

You can add partitions later, but it changes which partition each key hashes to, so for a keyed topic the ordering guarantee breaks for keys that move during the change. You cannot reduce partitions at all. Very large partition counts also have costs: more open files, slower leader elections and more memory on brokers and clients. Managed services often price partitions too.

3. Durability: replication, acks and in-sync replicas

SettingTypical production valueWhy
replication.factor3Survives losing a broker (or a zone, with rack awareness) without losing data.
min.insync.replicas2A write is only acknowledged once two replicas have it. The broker default is 1.
producer acksallWait for all in-sync replicas. The producer default since Kafka 3.0.
enable.idempotencetrueRetries can't create duplicates or reorder messages within a partition. On by default in current clients, but older ones and copied configs often turn it off.

With replication factor 3 and min.insync.replicas=2, the topic stays writable with one replica down, and refuses writes rather than accepting them unsafely if two are down. Check what your clients actually send: older client libraries and copied configurations often still set acks=1.

4. Retention or compaction

  • Delete (time or size retention) for event streams: keep seven days, or thirty, enough for consumers to recover from an outage and to replay. Decide it per topic; the default is rarely what anyone chose.
  • Compaction for state: Kafka keeps at least the latest message per key, so the topic becomes a table of current values (customer profiles, prices, configuration). Deleting a key means writing a tombstone, a message with that key and a null value.
  • Both (cleanup.policy=compact,delete) keeps the latest value per key, but only within the retention period.

5. Schemas and compatibility

A topic is an API. Register its schema (Avro, Protobuf or JSON Schema) in a schema registry, and set a compatibility rule so that a producer cannot publish a change that breaks its consumers. Schema Registry's default, BACKWARD, lets consumers on the new schema read old data; choose FULL or a transitive mode when producers and consumers upgrade independently over long periods.

Events, not table dumps. A topic whose messages are “the row after an update” ties every consumer to one database's schema. Where you can, publish business events with their own schema, for example through an outbox table; see real-time DB2 offload with Kafka for CDC in practice.

6. Names and ownership

Agree a naming convention before the tenth topic, not the thousandth. A common shape is <domain>.<dataset>.<event>.<version>, for example sales.orders.created.v1, with environment kept out of the name (it belongs to the cluster). Record an owner for every topic. Prefixes also make access control simpler: a team can be granted rights on every topic starting sales. with one prefixed ACL. The shared-platform use case shows how this works with many teams.

A worked sizing example

The arithmetic is simple; the inputs are what matter. Suppose, as an illustration, a team expects an order-events topic to peak at around 3,000 messages a second within the next year, and a load test shows one instance of its consumer handles about 400 a second with processing included. It needs at least eight consumers at peak, so eight partitions. With headroom for growth, for a slow downstream system and for rebalances, twelve is a reasonable choice. Doubling to twenty-four "to be safe" mostly adds broker overhead and cost.

Measure your own consumer rather than borrowing these figures: processing time per message varies enormously between a consumer that writes to a cache and one that calls an external API.

One topic or many?

A common question is whether each event type gets its own topic. The deciding factor is ordering. Events that must be read in order for the same entity, such as an order being placed, paid and cancelled, belong in one topic with the same key, because Kafka gives no ordering across topics. Unrelated events with different consumers, retention or access rules are better in separate topics. With one topic carrying several event types, use a schema approach that supports it, such as a union type or schema references.

Mistakes that are expensive to undo

  • Choosing a key for convenience (a random id, a timestamp) and finding out later that consumers needed per-customer ordering.
  • Creating topics through auto-creation, so partition count and replication come from broker defaults nobody chose. Turn off auto.create.topics.enable in production.
  • Publishing personal data to a long-retention or compacted topic without a plan for deletion requests.
  • Encoding the environment or a team member's name into topic names.

A topic checklist

  1. What is the key, and could any key become hot?
  2. How many partitions for next year's peak, and is anyone relying on ordering if we add more?
  3. Replication factor 3, min.insync.replicas 2, producers on acks=all and idempotence?
  4. Retention or compaction, and how long?
  5. Which schema, which compatibility mode?
  6. Does the name follow the convention, and who owns it?

Questions we are often asked

Can we change the key later?

Not in place. Messages already written stay in the partitions their old key chose. Changing the key usually means a new topic version (.v2), producers writing to it, and consumers moving over once they can handle both.

Is more partitions always faster?

No. Partitions allow more parallel consumers, but each one costs broker resources, and a consumer group with more partitions than it needs gains nothing. Parallelism beyond what consumers can use is overhead.

Should every topic have a schema?

Every topic that more than one team reads, yes. A schema is the contract that lets producers change safely; without one, the first change is found by the consumer that breaks.

Planning a Kafka migration or a platform clean-up?

The Kafka POD assesses, builds, migrates and hardens Kafka on Confluent, Amazon MSK, Redpanda or Apache Kafka, inside your environment, in a typical 12 weeks.

Kafka POD

Keep reading

Back to blog