Sub topic 13
Log-based brokers and partitions
This chapter assumes you know producers, consumers, acknowledgements, competing consumers, and the ordering limits of a work queue. It explains how a log-based broker differs from a queue broker and what partitions do.
Queue versus log
A queue broker such as RabbitMQ removes a message once a consumer acknowledges it. The broker tracks which messages are still pending.
A log-based broker such as Apache Kafka works differently. It appends each message to the end of a log, which is an ordered, append-only sequence of records. Each record gets an increasing number called an offset. Consumers do not remove records. Each consumer keeps an offset that marks how far it has read, and the broker deletes old records by retention policy (for example, after seven days), not by acknowledgement.
| Property | Queue broker | Log-based broker |
|---|---|---|
| After consumption | Message is deleted | Record stays until retention expires |
| Consumer state | Broker tracks pending messages | Consumer tracks an offset |
| Replay | Not possible once acknowledged | Reset the offset and read again |
| Many independent readers | Needs one queue per reader | Each reader has its own offset |
Replay is the main benefit. A new service can read history from the start. A consumer with a bug can be fixed and rewound to reprocess the affected records.
Partitions
One log on one machine limits throughput. A log-based broker splits a topic into partitions. Each partition is its own log with its own offsets, and partitions can live on different brokers.
The producer picks the partition, usually by hashing a message key. Records with the same key always land in the same partition, so they stay in order relative to each other. Order across partitions is not guaranteed.
Consumer groups
Consumers that share work join a consumer group. The broker assigns each partition to exactly one consumer in the group. This is the log-based version of competing consumers.
Two rules follow from this:
- Parallelism is capped by the partition count. With three partitions, a fourth consumer in the group sits idle.
- Order is kept within a partition because only one consumer reads it at a time.
Different groups read the same topic independently. An email service and an analytics service can each consume every record, which gives you publish/subscribe behavior.
Committing offsets
Committing an offset is the log-based form of an acknowledgement. The consumer tells the broker how far it has processed. If the consumer crashes, it resumes from the last committed offset.
This reproduces the delivery trade-off from earlier chapters. If you commit before processing, a crash loses records (at-most-once). If you commit after processing, a crash causes reprocessing (at-least-once), so consumers still need to be idempotent.
from kafka import KafkaConsumer
def process(value):
print("handling", value)
consumer = KafkaConsumer(
"orders",
bootstrap_servers="localhost:9092",
group_id="billing",
enable_auto_commit=False,
auto_offset_reset="earliest",
)
for record in consumer:
process(record.value)
consumer.commit()This uses the kafka-python package. It disables auto-commit and commits after each record is processed. Committing every record is simple but slow. Real consumers often commit in batches.
When a log fits
Choose a log when you need replay, several independent consumers, or very high throughput with per-key ordering. Choose a queue when you need per-message routing, priorities, or simple task distribution where messages should disappear after they are handled.
Exercise
A topic has four partitions and a consumer group has six consumers. How many consumers read data, and why might records for one customer arrive out of order?
Show answer
Four consumers read data, because each partition goes to only one consumer in a group, so two sit idle. Records for one customer stay ordered only if they share a key and so land in the same partition. If the producer does not key by customer, they can spread across partitions and be read out of order.