Topic design
Choose partitions and keys for order and scale, replication for durability, and retention or compaction for how long data stays.
A topic is cheap to create and awkward to reshape. Partitions can be added, and adding them changes which partition a key lands on. They cannot be removed without a new topic. Replication and retention are easier to change, and they are still worth deciding before the first producer.
This lab takes the payments topic from Kafka on OpenShift and sets the choices you would defend in a review: key, partition count, replication, and whether the log is deleted or compacted.
Prerequisites
- The
labcluster Ready, with the topic operator running oc, and permission to editKafkaTopicresources inkafka
Key and partitions
Kafka hashes the record key and assigns a partition. Every record with key a1 is appended to the same partition, in the order the producer sent them, as long as the producer sends them to one topic and you do not add partitions underneath it.
Use the entity the consumer must see in order. For payments that is the account, not the payment id. A payment id is unique, so it spreads records perfectly and destroys order. A null key spreads records too, and gives you no order at all.
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
name: payments
namespace: kafka
labels:
strimzi.io/cluster: lab
spec:
partitions: 6
replicas: 3
config:
retention.ms: "604800000"
min.insync.replicas: "2"
cleanup.policy: delete
unclean.leader.election.enable: "false"
Six partitions lets up to six consumers in one group read at once. If the posting service will run two pods, six is already more than those pods can use, and that is fine. Headroom is reasonable. A hundred partitions on a topic that peaks at one pod is not. Each partition has a leader, a set of replicas and open files on the broker.
Add partitions only when a consumer group is actually saturated. Existing records stay where they were. New records for key a1 may land on a different partition than old records for a1. Any consumer that assumed one partition for the whole life of an account has to be ready for that, or you accept a new topic and a cutover.
Replication
replicas: 3 keeps each partition on three brokers. min.insync.replicas: 2 means a producer with acks=all gets an acknowledgement only after the leader and at least one follower have the record. One broker can be down and produces still succeed. Two brokers down, and produces stop, which is the outcome you want. Losing acknowledged payments is worse than a pause.
unclean.leader.election.enable: false refuses to promote a replica that is not in sync. The alternative serves stale data and calls it availability. Leave it off for payments. The cluster-wide default is already set on the Kafka resource in the install lab. Setting it on the topic makes the decision visible next to the topic.
Retention and compaction
cleanup.policy: delete drops records older than retention.ms. Seven days (604800000) is a starting point for a payments log that is also loaded into a warehouse every night. A consumer that is down for longer than retention cannot catch up. Size the retention for the longest outage you will rebuild from, and for the replay the DC/DR lab depends on.
Compaction keeps the latest record for each key and drops the earlier ones. Use it for a changelog, where the current state of an account is the point, not the history. A compacted topic is the wrong place for a ledger. You cannot reconstruct Tuesday from it.
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
name: account-balance
namespace: kafka
labels:
strimzi.io/cluster: lab
spec:
partitions: 6
replicas: 3
config:
cleanup.policy: compact
min.insync.replicas: "2"
min.cleanable.dirty.ratio: "0.5"
The key is the account. The value is the latest balance. A consumer that starts from the beginning still sees one record per account after compaction has run, not the history of how it got there. Delete-and-compact (cleanup.policy: compact,delete) is for a changelog that should also forget keys you have tombstoned and then aged out.
A small review
| Decision | Payments | Account balance |
|---|---|---|
| Key | Account id | Account id |
| Partitions | Match real consumer parallelism, with some headroom | Same key space, so the same count is a fair start |
| Replicas / min ISR | 3 / 2 | 3 / 2 |
| Cleanup | Delete, seven days | Compact |
Write the key in the topic's description or in the producer's README. A topic whose key is "whatever the first developer sent" cannot be partitioned later with any confidence.
What's next
- Send records with that key from producers and consumers.
acks=allonly helps if the consumer is idempotent too, in delivery guarantees.- A week of disk you would rather not buy is the tiered-storage section of scaling and upgrades.