Topic 107 / Core
Producers and consumers
Chapter 7.1 · Asynchronous Foundations
What and why
Topic 106 introduced the queue; this topic fixes the two roles precisely. A producer is anything that publishes a message without knowing or caring who, if anyone, will consume it. A consumer is anything that reads and processes messages, tracking its own progress through the stream. Builds on: → 106.
Diagram
| Role | Responsible for |
|---|---|
| Producer | Constructing the message, choosing a key/destination, handling publish failures |
| Consumer | Pulling or receiving, processing, acknowledging, handling messages it can't process |
How it works
- The producer builds a message (payload plus metadata — key, timestamp, headers) and sends it, optionally waiting for a publish acknowledgement.
- The broker places the message according to its routing rule.
- The consumer pulls (poll) or receives (push) the message.
- The consumer processes it — the actual unit of work — and only then commits or acknowledges.
- The consumer tracks where it is (an offset or ack state) so a restart resumes roughly where it left off instead of from zero, or losing track entirely.
- Each side scales independently: more producers raise ingestion rate; more consumers (up to the partition limit, → 108) raise processing rate.
Practical example
(illustrative). An image-upload service runs 10 producer instances that each publish "resize me" messages, and 3 consumer instances that each resize 5 images/sec. Combined consumer throughput is capped at 15 images/sec regardless of how many more producers are added — only scaling the consumers raises that ceiling.
When to use it
- Use when producer rate and consumer rate need to float independently.
- Avoid introducing a broker for a single process doing both roles at the same latency budget — that's just a function call, or an in-memory queue (→ 2).
- Failure scenarios: a fast producer and a slow consumer with no backpressure (→ 97) grows the queue without bound; a producer that never checks its own publish acknowledgement can silently lose messages.
- Simpler alternative: a buffered in-process channel when everything runs in one process and no broker is needed at all.
Practice
(mechanism — build a tiny version; 15–20 min, lab Kafka). Write a producer that publishes 100 messages and a consumer that reads and prints them; then run the same producer against three consumer processes on the topic and confirm the 100 messages are split among the three, not each received by all three. Done when the total received across all consumers equals 100, and no consumer saw a message another consumer also handled.
Shared lab setupKey takeaway
Producers and consumers are independently scalable roles connected only through the broker. Done when you can: state what each side owns and what happens if one side disappears. Used next in → 108 (consumer groups, which govern how multiple consumers share the work).
References
None — this topic makes no external claims.
Source directory