Event Streaming LabPython · Bytewax

Chapter 29 · Reading

When is it safe to forget?

State, timers, expiry and the meaning of a result.

Prerequisites Keep state by key; Replay without double counting. Find chapters in the course contents.

What you’ll learn

Separate state lifetime from business-event lifetime and choose an explicit retention rule.

The situation

The shop remembers every order ID to reject duplicates. Memory use grows as the shop remembers more IDs. Deleting IDs frees state and limits which duplicates it can recognize.

State has several owners

Keyed state belongs to a logical key. Operator state belongs to an operator instance and needs its own redistribution scheme. Broadcast state can distribute small reference data to parallel tasks. These are ownership and redistribution contracts, not interchangeable names for a dictionary.

A state backend provides a way to keep and access state. Checkpoint storage holds recoverable snapshots. Local working state and durable checkpoint data have different lifecycles; losing one task’s local files need not mean losing a completed remote checkpoint.

A timer is a future obligation

An event-time timer fires when event-time progress reaches its target according to the framework. If watermarks stop, the timer may wait even while wall time passes. A processing-time timer follows a processing clock and therefore answers a different question.

Imagine an order at time 10 and a follow-up deadline at event time 70. A task can register a timer for 70 and retain the necessary order state until the callback. Recovery must preserve the state/timer relationship. A local Python callback scheduled with sleep does not demonstrate that property.

Worked case

A hypothetical 24-hour identity policy, not a measurement of backend cleanup.

Record or actionRetained identityIncoming deliveryDecision under this policy
Day 1 · order aa remembered for 24 hoursFirst aAccept
Day 1 · retrya still rememberedDuplicate aReject
Day 3 · replaya expiredOld a againAccept; retention has bounded the guarantee

Expiry changes semantics

A deduplication rule that remembers IDs for 24 hours accepts a repeated ID after expiry. A join that removes unmatched records after an hour cannot later match them without another source of history. Document whether the period is measured from creation, last access or last update, and whether it follows event or processing time.

Logical expiry and physical reclamation are separate. A backend may make expired state invisible before actually reclaiming all bytes. Flink state TTL has backend- and API-specific cleanup behavior; TTL is not a general promise that memory drops at an exact instant. Treat expiry policy as part of the result contract.

Think it through

Can state time-to-live (TTL), a rule for how long to keep state, guarantee that a join uses the address valid when an order happened?

Show hints
  1. Hint 1

    Expiry controls how long state remains, not which historical version it contains.

  2. Hint 2

    A temporal lookup needs a version-selection rule as well as retention.

Show explanation

No. You need historical versions, an event-time lookup rule and enough retained history for allowed lateness. TTL may remove needed versions or retain only the wrong one. Choose temporal semantics first, then a retention implementation that can support them.

How frameworks implement this

Flink offers managed state, timers and state TTL with specific guarantees and limitations. Spark stateful operators have their own state-store and timeout APIs. A Kafka log retains source data; it is not automatically a substitute for an operator’s indexed state.

ReferencesPrimary sources for this chapter’s design case

Use these guides to check the framework or protocol contract discussed in this chapter. Moving “stable” or “current” guides can describe a different release; check the version used by your deployment. These readings do not execute the distributed frameworks they compare.

This mark tracks what you’ve read.