Events, Queues & Messaging Fundamentals
Lesson opener
In one line: A producer appends a message. A consumer reads it. The promise is about loss and duplicates, not about a brand name.
Why it matters: Northline's pick event has to reach the serving total. If the consumer crashes after the deduct and before it records its place, a restart can deduct again. If it records its place before the deduct, a crash can skip the pick. Figures are illustrative.
Before this lesson: Lesson 1. You can name an event.
What you will be able to do
- Point at a producer, a broker, a topic, a partition, and a consumer.
- Say what a message key is for.
- Choose at-most-once or at-least-once on purpose.
- Refuse "exactly once" as a slogan when the deduct is not idempotent.
Prerequisites
Apache Kafka's design notes are the log this course uses (https://kafka.apache.org/41/design/design/). A queue in another product can teach the same roles. This lesson does not install a broker.
The pieces
Plain English:
- A producer sends. The warehouse job that emits
pickedis a producer. - A broker stores and serves messages. It is not the system of record for the shelf.
- A topic is the named log, such as
stock.picked. - A partition is an ordered slice of that topic. Order is inside one partition, not across the topic.
- A consumer reads. The serving-total job is a consumer.
- A message has a key and a value. Use the SKU as the key when all events for FX-100 must stay in one partition.
Delivery:
- At most once. Record "done" before you apply. A crash can lose the event. You do not apply it twice.
- At least once. Apply, then record "done." A crash in between can apply twice.
- Exactly once is a specific extra design, not a checkbox on the word queue. This lesson does not claim it for the deduct. Make the deduct safe under at-least-once by applying a pick id only once.
Teacher walkthrough: deduct once
All figures are illustrative.
FX-100 starts at 80. One pick of 2 has id p-16. The consumer must end at 78 even if the event is seen twice.
- Producer writes the event with key
FX-100and pick idp-16. - Consumer applies only pick ids it has not applied.
- Then it records the place.
- Validation: a second delivery of
p-16does not change 78.
def apply_picks(start, events, seen):
total = start
for event in events:
if event["id"] in seen:
continue
seen.add(event["id"])
total -= event["qty"]
return total
seen = set()
once = apply_picks(80, [{"id": "p-16", "qty": 2}], seen)
twice = apply_picks(once, [{"id": "p-16", "qty": 2}], seen)
assert once == 78
assert twice == 78A second workplace case
A status page can drop a gauge event. Losing one gauge is acceptable. Losing a pick is not. The delivery model follows the damage, not the topic name.
def model(event_name):
if event_name == "gauge":
return "at_most_once"
if event_name == "picked":
return "at_least_once"
raise ValueError(event_name)
assert model("picked") == "at_least_once"
assert model("gauge") == "at_most_once"Failure example: the pick was deducted twice
Symptom: FX-100 shows 76 after one pick of 2 from 80.
Diagnosis: The consumer applied the pick, crashed, and applied p-16 again. Nothing stored the pick id. The root cause is a non-idempotent apply under at-least-once delivery.
Correction: Store applied pick ids. Skip an id you already have. Do not switch the pick path to at-most-once to hide the bug. That can skip a pick instead.
Validation: Deliver p-16 twice. The total stays 78. Deliver a new id p-17 for 1. The total becomes 77.
def apply_picks(start, events):
seen = set()
total = start
for event in events:
if event["id"] in seen:
continue
seen.add(event["id"])
total -= event["qty"]
return total
events = [
{"id": "p-16", "qty": 2},
{"id": "p-16", "qty": 2},
{"id": "p-17", "qty": 1},
]
assert apply_picks(80, events) == 77Guided practice: key and promise
Scenario: Two events, both SKU FX-100, picks p-1 and p-2. A gauge event may be dropped. Figures are illustrative.
Hints:
- What is the message key for the picks, and why?
- Which promise fits a pick?
- What do you store so a retry does not deduct twice?
Complete solution:
The key is FX-100, so both picks stay in one partition and keep their order there. The promise for a pick is at least once, with the pick id stored. The gauge may use at most once. A second p-1 does not deduct again.
def key_of(event):
return event["sku"]
assert key_of({"sku": "FX-100", "id": "p-1"}) == key_of({"sku": "FX-100", "id": "p-2"})Independent practice
Pick one message your team would send, even on paper.
Acceptance criteria:
- You name the producer, the topic, the consumer, and the key.
- You choose at most once or at least once and say what is lost or repeated.
- You show an idempotent apply if you chose at least once.
- You do not claim exactly once without naming the idempotent key.
- You do not promise order across two partitions.
Common mistakes
- Treating the broker as the owner of on-hand quantity.
- Using a null key and then expecting order.
- Deducting on every delivery of the same pick id.
- Calling every product "Kafka" when you have not chosen a log.
Quick check
- Who sends, and who reads?
- Where is order guaranteed?
- What can at-most-once lose?
- What can at-least-once repeat?
- What do you store so a repeated pick does not deduct twice?
Answers:
- The producer sends. The consumer reads.
- Inside one partition, not across the topic.
- An event, if you record done before you apply.
- The effect, if you apply before you record done.
- The pick id you already applied.
Quiz alignment
The quiz asks you to name roles, to keep one SKU in one partition, and to stop a duplicate deduct.
Lesson summary
Producers append. Consumers read. Topics split into partitions. The key places order. Say whether you would rather lose an event or repeat an effect, and make a repeated pick id harmless.
Bridge to the next lesson
The next lesson is Apache Kafka's own shape: brokers, replication, consumer groups, and offsets. Bring the topic name stock.picked.