Part III — Advanced Systems · Chapter 16 · Lesson 5 of 6 · 4 min read
Lesson 5: Lazy Execution and Dependency-Light Design
Lesson level: Advanced
Purpose: Separate transformation plans from execution and choose dependencies based on verified needs rather than assumptions about performance.
Learning objectives. Distinguish eager and deferred work; identify materialization boundaries; reason about safe operation ordering; compare plans through results and measurements; and keep optional acceleration separate from correctness.
Learning objectives
By the end of this lesson, you will be able to:
- explain the difference between eager and lazy execution
- build a lazy plan and identify when it is executed
- explain why filters and projections are pushed down
- keep pipelines dependency-light by relying on the built-in engine and optional adapters
- decide when to collect results into an eager DataFrame
5.1 Plans describe work; execution produces evidence
An eager operation produces its result when invoked. A deferred plan describes work to perform later. Writing a plan does not prove that its input was read, its transformations succeeded, or its output met the contract.
Conceptual plan: validated source → approved-order filter → derived fields → customer enrichment → region-and-currency aggregation → deterministic output. An execution boundary evaluates the plan. Confirm the actual PeaLang materialization method and supported plan operations before implementing this description.
Deferral can create opportunities to avoid unnecessary work, but a LazyFrame name alone does not establish optimizer features. Projection pushdown, predicate pushdown, streaming, parallelism, and backend acceleration are separate capabilities requiring documentation and evidence.
Some reorderings preserve a contract; others change it. Selecting required fields earlier may reduce retained data, but dropping a join key breaks enrichment. Moving a limit before aggregation changes population. Moving a filter across an outer join can remove unmatched records and alter completeness.
Repeated materialization may repeat work unless caching is documented. Keep a reusable plan separate from any cached result. If the source can change between executions, a repeated plan may produce different data even when its text is identical.
Establish an input snapshot or version identifier when reproducibility matters. Record the transformation configuration and reference-data version alongside it. A successful output without source identity is difficult to reproduce.
Guided practice. Mark the source-reading, validation, transformation, and materialization boundaries in the conceptual plan. Identify which errors could be delayed until execution.
Common mistake: treating a small final output as proof of bounded intermediate memory. Sorting, joining, or grouping can require substantial retained state.
Review prompt: Which evidence would demonstrate a claimed optimization rather than merely repeat its name?
5.2 Hands-on lab: Compare execution strategies honestly
Scenario. Implement the same logical report using an eager sequence and, where the installed runtime supports it, a deferred plan. If deferred execution is unavailable for an operation, document that limitation and compare the supported subset rather than inventing a fallback API.
Keep identical input fixtures, schema rules, duplicate policy, reference data, null treatment, and output ordering. Compare contractual results before measuring performance. A faster implementation that drops unmatched records or changes precision is not an optimization of the same task.
Measurement plan. Record source size, value distribution, join-key uniqueness, group cardinality, dependency versions, execution boundary, elapsed time, and peak-memory measurement method. Separate source-reading time from transformation time only when the instrumentation genuinely supports that separation.
Use repeated observations and distinguish warm from cold execution. Do not publish a universal speed claim from one convenient fixture. A high-cardinality group or a duplicate-heavy join can behave differently from the ordinary sample.
Dependency-light design. Keep the correctness contract independent of an optional accelerator. Introduce a dependency only when it satisfies a measured need, and test supported behavior when that dependency is absent. Record installation, licensing, portability, and operational costs alongside speed.
Failure tests. Include malformed input discovered at execution time, a missing optional backend, an exhausted resource budget, changed input between runs, and a computation invoked twice. Verify whether repeated invocation recomputes or reuses a documented result.
Acceptance criteria. Both supported strategies produce equivalent contractual outputs. Performance observations name their conditions. No unverified optimizer capability is claimed. Dependency absence has a clear outcome. Delayed failures are reported at the correct stage.
Review questions. What does materialization establish? When can a plan be reordered safely? Why is lazy execution not synonymous with streaming? How does an input snapshot improve reproducibility? What makes an acceleration comparison fair?
Applied challenge. Choose one proposed optimization and write its proof obligation: which result properties must remain identical, which workload it targets, and what measurements would justify retaining it.
Lesson summary. Treat plans as descriptions, results as evidence, and performance claims as conditional observations. Keep optional dependencies separate from the logical contract.
Common mistakes
- Collecting too early: materializing data before filtering cancels the benefit of laziness.
- Assuming a lazy plan has already run: errors in a lazy plan appear only when it is executed; test the executed result.
- Reusing a plan with changing inputs: record which input snapshot a plan ran against.
- Adding heavy optional dependencies to every project: install optional extras only where they are needed.
- Benchmarking with tiny data: differences between eager and lazy execution appear only at realistic sizes.
Review questions
- What does a lazy plan contain before execution?
- Which operations trigger execution?
- Why does pushing a filter down reduce work?
- When is eager execution the better choice?
- How does dependency-light design help deployment and security review?
Applied challenge
Write the same analytics pipeline in eager and lazy forms. Use a file large enough to measure. Compare run time and peak memory, show the optimized plan, and write a short recommendation on which form the team should use, with reasons.
Lesson summary
Lazy execution separates describing work from performing it, so the engine can push filters and projections down and avoid unnecessary work. Collect deliberately, test executed results, measure with realistic data, and keep optional dependencies out of the core.
Lesson status: Ready for review.