liveBook · Manning

Partition

As explained in Apache Kafka in Action

A Kafka partition is an ordered, append-only subdivision of a topic that stores records and enables parallel processing.

An online store may need every price change for a product in order, without needing a single ordering across all products. A Kafka partition provides that boundary: it is one sequential, append-only log within a topic. Topics use multiple partitions to spread storage and processing across brokers. section 4.2.1 section 4.5

Each record has an offset identifying its position within its partition. A producer’s partitioner chooses where to send a record; records with the same key go to the same partition. Use the product as the key when its price changes must stay together. Ordering is not guaranteed across partitions, and the stated ordering guarantee also depends on producer idempotence being enabled. section 3.3 section 6.1.1 section 4.5

Try two partitions

Create a local topic with `kafka-topics.sh --create --topic products.prices.changelog.multi-partitions --partitions 2 --replication-factor 1 --bootstrap-server localhost:9092`. Produce keyed prices with `kafka-console-producer.sh --topic products.prices.changelog.multi-partitions --property parse.key=true --property key.separator=: --bootstrap-server localhost:9092`, then enter lines such as `coffee pads:10` and `coffee pads:11`. The key, rather than the product name merely appearing in the value, keeps those updates together. section 4.2.1

# First create the topic products.prices.changelog.multi-partitions-keys as
above
$ kafka-console-producer.sh \
    --topic products.prices.changelog.multi-partitions-keys \
    --property parse.key=true \
    --property key.separator=":" \
    --bootstrap-server localhost:9092
> coffee pads:10
> cola:2
> cola:1
> energy drink:3
> coffee pads:11
> coffee pads:12
> energy drink:4
> coffee pads:10
# Press Ctrl-D to cancel

The console-producer listing shows the key-parsing options and a sequence of product-price updates to enter. section 4.2.1

Partitions are also units of consumer-group work: a group assigns each whole partition to one consumer, preserving its local order while allowing other partitions to be processed in parallel. More partitions can increase available parallelism, but they do not make inherently sequential work parallel. Independent consumers can read the same topic while tracking their own offsets. section 6.1.1 section 4.5

For reliability, a partition can have replicas on different brokers. Its leader handles reads and writes, while followers copy its data. On disk, an assigned partition has its own directory containing log and index files. Choose the partition count deliberately: it affects broker load and consumer parallelism, and a topic’s partition count cannot be decreased. section 1.3 section 8.3.2 section 3.3 section 6.1.1

How Apache Kafka in Action covers it

How deep it goes
Explains key-based placement, offsets, consumer assignment, replication, and partition files; also demonstrates creation and production with Kafka command-line tools.
Examples
Demonstrates product-price updates across two partitions using topic creation and keyed console-producer commands.
What it assumes
Starts with a running local Kafka cluster and introduces topic creation with one partition before using multiple partitions.
What you can do after
Operate/configure it with Kafka command-line tools and choose keys and partition counts with ordering and consumer parallelism in mind.
How it connects
Connects partitions to topics, distributed logs, offsets, message keys, consumer groups, brokers, and replication.

Also explained in

Compare how 4 books explain partition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Apache Kafka in Action. The section links point to the parts of the book it draws on.