Apache Kafka Architecture
Lesson opener
In one line: Kafka is a replicated log. A consumer group remembers an offset per partition.
Why it matters: Northline can add a second serving-total reader and still deduct FX-100 twice if both readers sit in different groups. Or the second reader can sit idle if there is only one partition. Figures are illustrative. This lesson does not start a cluster.
Before this lesson: Lesson 2. You can say topic, partition, and key.
What you will be able to do
- Describe a broker, a leader, and a replica.
- Say how a consumer group shares partitions.
- Read an offset as a position, not as a row id.
- Refuse a second group when you wanted one shared deduct.
Prerequisites
The terms match Apache Kafka 4.1's introduction and design (https://kafka.apache.org/41/getting-started/introduction/ and https://kafka.apache.org/41/design/design/). Replication factor and in-sync replicas are topic settings you choose. This lesson does not pick a factor for every topic.
The log
Plain English: A broker is a Kafka server. A topic's partition has one leader and zero or more followers. Producers and consumers talk to the leader. Followers replicate. The in-sync replicas are the followers that are keeping up, plus the leader. If the replication factor is 1, one broker's disk is the only copy.
An offset is the position of a record in a partition. It increases. It is not the pick id. A consumer group splits the topic's partitions across its members. One partition is read by one member of that group. A second group reads the same records again. That is a second copy of the work, not a spare worker for the first deduct.
If you have more consumers in the group than partitions, the extra consumers get no partition. They sit idle. Adding consumers does not add order across partitions.
Teacher walkthrough: one deduct group
All figures are illustrative.
stock.picked has 3 partitions. The deduct job should be one group named serving-total.
- Count partitions and consumers in that group.
- Idle consumers are
max(0, consumers - partitions). - A second group named
debugmay read the same events and must not deduct. - Validation: 3 partitions and 2 consumers leave 0 idle and give one consumer two partitions. 3 partitions and 4 consumers leave 1 idle.
def group_shape(partitions, consumers):
active = min(partitions, consumers)
idle = max(0, consumers - partitions)
return active, idle
assert group_shape(3, 2) == (2, 0)
assert group_shape(3, 4) == (3, 1)A second workplace case
A replica plan is a choice. Replication factor 1 loses the only copy if that broker disk fails. Factor 3 keeps two more copies if they stay in sync. The lesson does not declare 3 as a law. It asks you to write the number down and say which failure it is meant to survive.
def copies_after_one_broker_loss(replication_factor):
return max(0, replication_factor - 1)
assert copies_after_one_broker_loss(1) == 0
assert copies_after_one_broker_loss(3) == 2Failure example: two groups deducted one pick
Symptom: FX-100 fell by 4 after one pick of 2.
Diagnosis: serving-total and serving-total-v2 were two groups. Each applied p-16. The root cause is a second group doing the same side effect. Offsets in one group do not stop the other group.
Correction: One group owns the deduct. Other readers use another group only for work that must not change the total, or they do not run. Keep the pick-id guard from lesson 2 anyway.
Validation: One group applies p-16 once. A monitor group may count the event and must not call the deduct.
def owners(groups):
return [name for name, effect in groups if effect == "deduct"]
assert owners([("serving-total", "deduct"), ("monitor", "count")]) == ["serving-total"]Guided practice: offsets and idle readers
Scenario: Partition 0 has offsets 0, 1, and 2. The group has committed offset 1, meaning the next record to read is offset 1. Two consumers, one partition. Figures are illustrative.
Hints:
- What is the next offset?
- How many consumers get that partition?
- How many sit idle?
Complete solution:
The next record is offset 1. Offsets 0 is already behind the commit. One consumer gets the only partition. One consumer is idle. You need more partitions, not a second group, if both machines should deduct different SKUs.
def next_offset(committed):
return committed
def idle(partitions, consumers):
return max(0, consumers - partitions)
assert next_offset(1) == 1
assert idle(1, 2) == 1Independent practice
Sketch one topic you would actually create.
Acceptance criteria:
- You name the topic, the partition count, and the consumer group that is allowed to change a total.
- You say what an offset is in that partition.
- You compute idle consumers for your counts.
- You write a replication factor and the failure it is meant to survive.
- You do not add a second group that applies the same pick.
Common mistakes
- Treating an offset as a business key.
- Starting a second group to "go faster" on the same side effect.
- Expecting extra consumers to help when they outnumber partitions.
- Setting replication factor 1 and calling the topic safe.
Quick check
- What is an offset?
- How many consumers in one group read one partition?
- What do extra consumers do when they outnumber partitions?
- Does a second group share the first group's offsets?
- What does replication factor 1 survive?
Answers:
- The position of a record in a partition.
- One.
- They stay idle.
- No. Each group has its own offsets.
- Not the loss of that one broker's copy.
Quiz alignment
The quiz asks you to place leaders and groups, to spot idle consumers, and to keep one group on the deduct.
Lesson summary
Brokers hold partitioned logs. Leaders take reads and writes. A consumer group splits partitions and stores offsets. A second group is a second pass. Match consumer count to partitions for the job that changes stock.
Bridge to the next lesson
The next lesson sends and reads records: keys, serializers, retries, and what a schema change does to an old consumer. Bring the topic stock.picked and the group serving-total.