Represent dependencies and resume from saved, versioned state.
Section 1.1 1 of 7
Overview
Focus
Represent dependencies and resume from saved, versioned state.
Your route through the lesson
1.2 Task graphs, dependencies, and queues
1.3 Durable step states and checkpoints
1.4 Crash windows and operation reconciliation
1.5 Put it into practice
1.6 Guided comparison
1.7 An input version changed before resume
All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.
Section 1.2 2 of 7
Task graphs, dependencies, and queues
Focus
Represent task readiness using dependencies rather than message arrival order.
A task graph represents work as nodes and required ordering as directed edges. An edge from extract to calculate means calculate cannot start until the required extraction output is accepted. A graph with no directed cycle permits a dependency order. A cycle such as A waits for B while B waits for A has no initially ready task and requires redesign or an explicit iteration rule.
Consider extract, calculate, and archive. Both calculate and archive require extract; publish requires calculate and archive. Initially only extract is ready. After extraction is accepted, calculate and archive may run together. Publish remains blocked until both finish. Queue arrival order alone cannot represent these conditions: publish might be delivered before archive completes.
A scheduler asks which pending tasks have all required inputs at acceptable versions. It moves only those tasks to a ready queue. The queue holds eligible work; it does not define the entire dependency policy. Record task identifier, input versions, state, attempt count, and output identity. Distinguishing pending from ready makes a stalled graph explainable: a task may be waiting on evidence rather than waiting for available worker capacity.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 1.3 3 of 7
Durable step states and checkpoints
Focus
Persist step states and checkpoints with deliberate transactional boundaries.
A checkpoint is saved state sufficient to decide what can safely happen next. A printed progress message is not a checkpoint if it disappears during a crash or omits whether an effect committed. Durable state survives process restart. It should include task and operation identifiers, consumed input versions, current status, and the evidence needed for reconciliation.
The standard library sqlite3 connects Python to a local SQLite database, a disk-backed store that supports transactions. A transaction groups updates so they commit together or are rolled back together. The weekly lab uses explicit transaction control where competing workers need atomic changes. connect returns a connection; execute sends a statement; a question-mark placeholder receives a separate parameter value rather than interpolating text into the statement.
For a local calculation, store result and done status in one transaction. If the process dies before commit, neither becomes visible; after commit, both are durable. A remote service effect cannot normally share that local database transaction, so its operation identifier and uncertain status must also be represented. Checkpoints must distinguish requested, observed committed, and acknowledged rather than pretending every running task is safe to repeat. Python SQLite documentation describes connection and transaction behavior.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 1.4 4 of 7
Crash windows and operation reconciliation
Focus
Reconcile the effect-before-acknowledgment crash window without repeating effects.
A crash window is an interval where different parts of a workflow disagree about what happened. Suppose a worker reserves inventory, the reservation service commits, and the worker crashes before recording success. Local state still says running. Blindly retrying can reserve twice; blindly marking failed can hide a real reservation.
Use a stable operation identifier for the logical reservation. On restart, ask the effect store whether that identifier committed. If it did, record the result and continue. If the service supports idempotency, retrying the same identifier can return the original result instead of creating another effect. A fresh identifier on every retry defeats that protection because the receiver sees distinct logical operations.
The classroom database stores effects under a primary key, which makes duplicate identifiers invalid. Reopening the database finds one committed reservation even though its step lacks acknowledgment. The recovery code then marks that step done for the original input version. This proves reconciliation in one local durable store. Real remote systems require documented lookup or idempotency contracts, retention periods, and timeout handling. If outcome cannot be determined, record unknown and escalate; uncertainty is not evidence that nothing happened.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 1.5 5 of 7
Put it into practice
Focus
Run durable recovery after a committed effect and inspect changed-input handling.
Download week14.py, save it in an exercise folder, and run python week14.py --lesson 1. The processor-only standard-library run creates a temporary SQLite file, closes it after an effect, and opens it again for recovery. It should finish within seconds and uses no external service. The single practice is to inspect how the saved operation and input version determine the restart decision.
The script creates effects and steps tables. CREATE TABLE defines named columns; PRIMARY KEY requires unique operation or task identities. INSERT adds a record. SELECT reads records, and fetchone returns one row as a tuple. The tuple (1,) contains one value; the comma makes it a tuple rather than a parenthesized integer.
The fixture records order-7-reserve before its step is acknowledged. The second connection sees that committed effect, changes the matching version-1 step to done, and verifies there is exactly one effect. It also compares changed_input=2 with the saved version 1 and reports that replanning is required. Closing and reopening is real persistence evidence, while the crash is a controlled omission of acknowledgment rather than an actual power-loss test.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 1.6 6 of 7
Guided comparison
Focus
Explain the recovery trace and its precise guarantees.
The worked trace begins with reserve in running state for input version 1. A separate effects record with key order-7-reserve contains value 1. The connection closes before reserve becomes done. Recovery therefore encounters the deliberately inconsistent acknowledgment state but intact effect evidence.
The second connection queries by operation identifier and obtains (1,). It does not insert another reservation. It updates the matching step to done and verifies count(*) equals one in the effects table. This is the central invariant: a missing acknowledgment does not cause a second logical effect. The subsequent version comparison prevents this evidence from being automatically reused for input version 2.
The fixture simplifies the task graph to the recovery boundary. In a larger graph, done makes dependent tasks eligible only if their declared input versions still match. A graph scheduler should separately record why a task is ready and why a completed step was invalidated. Recovery is not simply replaying every pending-looking function. It is reconstructing the strongest supported state from durable evidence, then selecting the next valid transition. Preserve unknown outcomes when evidence is insufficient rather than turning every uncertainty into a retry.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Section 1.7 7 of 7
An input version changed before resume
Focus
Invalidate a recovered result when its upstream input changed.
A shipping quote was calculated for address version 3. The worker saved quote Q8 and then crashed. While it was down, the customer changed the address to version 4. On restart, Q8 is durable and may be numerically correct for version 3, but it is not a valid quote for the current request.
The worked recovery compares Q8's consumed_address_version=3 with current_address_version=4. It preserves Q8 as historical evidence, marks the dependent shipment preparation stale, and creates a new bounded quote task using version 4. If an actual reservation was already made for version 3, recovery must reconcile that effect and decide whether cancellation or compensation is required. Deleting the local old quote would not undo the remote reservation.
This case separates durability from validity. Durable means evidence survived; valid means evidence applies to the current requirements and input versions. A system can durably preserve a wrong or obsolete result. Checkpoint design should therefore include both content identity and dependency identity. Otherwise restart can confidently reproduce the wrong workflow, making persistence an amplifier of stale assumptions rather than a recovery mechanism.
// Quick review
Check your understanding.
Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.
Your personal notes
Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.