Aller au contenu

How do Kafka topics and partitions work?

par Équipe NextData

0

I am starting with Apache Kafka and I am confused by topics, partitions, offsets and consumer groups. How do they fit together, and how many partitions should a topic have?

Signaler

1 réponse

0

A topic is a named stream of messages. It is split into partitions, and each partition is an append-only log.

  • Every message in a partition gets a sequential number, its offset.
  • Order is guaranteed inside a partition, not across the whole topic.
  • The producer chooses the partition: messages with the same key always go to the same partition, so all events for one customer stay in order.

On the reading side, consumers join a consumer group:

  • each partition is read by one consumer of the group at a time;
  • the group stores its position as an offset per partition, which lets it resume after a restart;
  • two different groups read the same topic independently, each at its own pace.

This is why partitions set the ceiling for parallelism: with 6 partitions, at most 6 consumers of a group work at once. A seventh would sit idle.

Choosing the number of partitions:

  • start from the consumer parallelism you expect at peak, with some margin;
  • more partitions means more open files and more work for the cluster during a failover, so do not create hundreds by default;
  • you can add partitions later, but existing keys may then map to different partitions, which breaks ordering per key. You cannot reduce the count.

Each partition is also copied to several brokers (the replication factor), so a broker failure does not lose data.

For the reviewer to check: partition count guidance depends on the Kafka version and on whether the cluster runs in KRaft mode.

Réponse rédigée par l'équipe NextData et relue par un expert.

Équipe NextData

Signaler

Votre réponse

Connectez-vous pour répondre