Rana Maninder Singh
Lesson 2 of 12Real-Time Data Engineering

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

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:

Delivery:

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.

  1. Producer writes the event with key FX-100 and pick id p-16.
  2. Consumer applies only pick ids it has not applied.
  3. Then it records the place.
  4. Validation: a second delivery of p-16 does 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 == 78

A 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) == 77

Guided 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:

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:

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

  1. Who sends, and who reads?
  2. Where is order guaranteed?
  3. What can at-most-once lose?
  4. What can at-least-once repeat?
  5. What do you store so a repeated pick does not deduct twice?

Answers:

  1. The producer sends. The consumer reads.
  2. Inside one partition, not across the topic.
  3. An event, if you record done before you apply.
  4. The effect, if you apply before you record done.
  5. 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.