Home / Apache Kafka / Lesson 2

Topics, partitions, offsets

Fundamentals. A free Apache Kafka lesson in simple English (3:33 minutes).

Open in the academy (practice, progress and notes)

Chapters

Lesson text

0:00 What you will learn

Welcome to lesson two. Last time, producers wrote events to a topic. Today we look inside that topic. You will learn what a partition is, how keys help keep events in order, and what an offset tells you. This is an independent course. It is not made or approved by Confluent, or the Apache Software Foundation.

0:21 Partitions

Here is our topic, called orders. A topic is split into one or more parts. Each part is called a partition. Our topic has three. Why do we split a topic? Because its partitions can be placed on different Kafka servers. A Kafka server is called a broker. This helps share the work. More partitions can also let more consumers in the same group work at the same time. But each partition adds work for Kafka, so more is not always better.

0:48 Offsets

Now let's look inside partition zero. New events are added at the end. Each event gets a number in its partition. This number is called an offset. In a new partition, the offsets start at zero. Each partition has its own offsets. So partition zero can have an offset five, and partition one can have an offset five too. They are different events. To find one exact event, you need the topic, the partition, and the offset.

1:16 Keys and order

When events have the same key, Kafka's default method sends them to the same partition, as long as the number of partitions stays the same. For example, three events have the key order five zero one: created, paid, and sent. They all go to partition one. Kafka keeps order inside one partition. A consumer reads that partition's events in the order they were written. But Kafka does not promise one order across all the partitions in a topic. What if an event has no key? Then the producer can spread events across the partitions. So use a key when related events must stay in order.

1:52 How a consumer keeps its place

How does a consumer know where to continue? It can save its place, like a bookmark. This is called committing an offset. The committed offset is the next event to read. If a consumer finishes offsets zero, one and two, it commits three. After a restart, the group can continue from there. Later, we will study when to commit. If you commit too early, you can miss events. If you commit late, you can process an event again.

2:20 Practice question 1

Question one. Where does Kafka keep events in order? A, across all topics. B, across one whole topic. C, inside one partition. Or D, across all brokers.

The answer is C, inside one partition. When related events must stay in order, give them the same key.

2:45 Practice question 2

Question two. A consumer finishes the events at offsets zero, one, two, three and four. Which offset should it commit to continue with the next event? A, three. B, four. C, five. Or D, six.

The answer is C, five. The committed offset is the next event to read.

3:11 Summary

Let's review. A topic is split into partitions, and an offset is an event's number inside its partition. Order is kept inside one partition. The same key helps keep related events together. And the committed offset is the next one to read. In the next lesson, we will learn about brokers, and something called K Raft. Thank you for learning with us.

Practice questions

Where does Kafka keep events in order?

Answer: Inside one partition. Kafka keeps order inside a partition. Give related events the same key.

A consumer finishes offsets 0, 1, 2, 3 and 4. Which offset should it commit?

Answer: 5. The committed offset is the next event to read.