Event Streaming LabPython · Bytewax

Chapter 5 · Explicit log model

Retain or compact a log

History and latest state answer different questions.

Prerequisite Stream live through Kafka.

What you’ll learn

Explain retention gaps, compaction, tombstones and bootstrap requirements.

The question

A new audit consumer starts a week late. The dashboard can reconstruct current customer values, but the original sequence of changes is gone. A latest-value view and a retained event history are different products.

Retention bounds what history a broker keeps. Compaction preserves a latest value per key over time, subject to broker policies; it does not preserve every intermediate update. A tombstone represents a key deletion. Our bounded helper first drops offsets below a configured floor, then optionally computes an idealized latest-per-key view. Surviving records keep their original offsets.

Predict the result

Offsets 0..5: a=10, b=8, a=12, c=5, b=tombstone, a=15.

After offsets 0 and 1 are unavailable, does compaction renumber the remaining offsets from zero?

Run the experiment

The editor contains the published example. Select Run with the code unchanged.

Python prepares in the background while you read. First use can download about 84 MiB. Your code runs only when you select Run; each run starts with clean experiment state.

Ctrl+Enter or ⌘+Enter runs the code.

Edits are saved in this browser when storage is available.

Troubleshooting

If Python stops responding, restart the environment and try again. Your code and last completed result are kept.

View the published result

The course build ran the published code to generate this output.

{
  "records": [
    {
      "offset": 3,
      "key": "c",
      "value": 5
    },
    {
      "offset": 4,
      "key": "b",
      "value": null
    },
    {
      "offset": 5,
      "key": "a",
      "value": 15
    }
  ],
  "earliest_available": 2,
  "next_position": 6,
  "note": "Compaction is an idealized latest-per-key pass. Tombstones remain visible."
}

Read the result

Read offset, key, and value in records. Compaction keeps the latest row for each key in the retained range. A null value marks a delete. Compare the requested start at 2 with the first surviving record at offset 3.

Change the experiment

Replace compact=True with compact=False in the editor. Select Run again. Compare the versions of key a in records. Then replace start=2 with start=0 and select Run again. Which earlier history becomes visible when both policies change?

Return to the editor
Runtime, source code and downloads

Runtime and reset details explain what stays ready and what is isolated on each run. Download Python packages the published helpers with the code in your editor.

Browse the sources for Bytewax windows and authored models, Kafka log and group exercises, live Kafka input, checkpoint recovery and processing patterns.

A committed position does not preserve data

A group can remember an offset whose record is no longer retained. A reset policy chooses what to do when a valid starting point is unavailable; choosing earliest cannot resurrect expired history. Treat replay horizon as a recovery requirement before selecting retention.

A log is not automatically an event-sourced system

An event-sourced application also defines event meaning, stable identity, version evolution and reconstruction rules. A topic configured for current-state distribution may be unsuitable for an audit trail. Choose the required questions first: rebuild the present, reproduce a past decision, or retain every accepted business event.

Work, state and recovery costs

Where used, n counts input events, k counts retained distinct keys, r counts overlapping windows per event and z counts output matches. Bounds assume the stated algorithm. Collecting, sorting and displaying results adds work.

QuestionMechanism and boundary
WorkO(n) filtering and expected hash updates; sorting surviving offsets adds O(k log k).
StateO(n) reference log/output and O(k) latest-key index.
MovementThe helper scans an in-memory list that stands in for one log partition.
ProgressThe helper removes offsets below the requested floor, then selects the latest record per key when compaction is on.
RecoveryReconstructing current values requires enough retained records and deletions, or a snapshot and the positions from which to continue.

Think it through

Can a new consumer rebuild a complete state snapshot if required tombstones have expired before it finishes reading?

Show hints
  1. Hint 1

    Consider a previously materialized value for a deleted key.

  2. Hint 2

    Absence of a value and an explicit deletion are different evidence.

  3. Hint 3

    A bootstrap procedure needs a history/retention contract.

Show explanation

Not necessarily. It needs an authoritative snapshot plus a consistent log boundary, or enough retained updates and deletions to reconstruct the intended state. Kafka compaction and tombstone retention have timing and configuration details; an idealized latest-per-key pass is not a proof that every slow bootstrap is safe.

ReferencesFunction documentation and helper source

References open in a new tab; course helpers are documented separately from library APIs.

  • Compaction is a log policy

    Compare the broker’s documented compaction contract with the authored retained_log function. The Python helper does not run a broker compactor.

  • Course helper implementation

    These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.

Experiment scope

This is one list-backed partition. Kafka segment deletion, asynchronous compaction, transaction visibility and timestamp-based retention are not executed.

Primary reference →
Reading marks track what you’ve read independently of your runs.