Production failure recovery patterns
Reliable automation makes failure visible and restart behavior explicit. A checkpoint records a verified completed stage, not merely an attempt. A retry is suitable for a transient failure only when repetition is safe or a documented idempotency contract exists. Use a finite attempt budget, a deadline, capped backoff and jitter (variation of delays) to avoid synchronized retries. Distinguish permanent validation errors from temporary failures. The local example uses fixed synthetic failure statuses and prints the planned jitter range; it does not sleep for the production schedule.
Recovery and audit
Write candidates in a private directory on the destination filesystem. Verify content and only then replace the intended file. An atomic rename for a single file does not establish crash durability or a multi-file transaction. Advisory locks coordinate only cooperating processes. A checkpoint must be validated against its input identity before skipping work. On restart after a crash between effect and checkpoint, verify the existing effect or use idempotent steps; a checkpoint alone does not ensure exactly-once behavior. Keep the previous verified release available and define the rollback decision before applying changes. A rollback is itself an operation that can fail. Record stage, outcome and exit status; avoid logging secrets or raw untrusted multi-line values. The exercises use fixed safe names in a private directory, not actual production data. Calculate a fresh digest successfully before asserting that input changed. Compare candidate and active content byte-for-byte, distinguishing a genuine difference from a comparison error.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. jq, GNU coreutils, GNU tar and flock are external dependencies for selected examples. Use only synthetic files in a private directory.
Bound retries and distinguish failures
attempt=0probe() { ((attempt >= 3)) && return 0; return 75; }for ((attempt=1; attempt<=3; attempt++)); do if probe; then printf 'ready=%s\n' "$attempt"; exit "$?"; else rc=$?; fi (( rc == 75 && attempt < 3 )) || exit "$rc" cap=$((1 << (attempt-1))) printf 'retry=%s jitter-range=0..%s\n' "$attempt" "$cap" || exit 1doneexit 1Expected stdout:
retry=1 jitter-range=0..1retry=2 jitter-range=0..2ready=3The local contract defines 75 as transient. Production full jitter chooses a delay inside the printed bound and checks the overall deadline; here only the plan is printed.
Verify a checkpoint before skipping
printf 'input\n' > input.txt || exit 1sha256sum input.txt > checkpoint.txt || exit 1if sha256sum --status -c checkpoint.txt; then printf 'input unchanged\n'; else exit 1; fiprintf 'changed\n' > input.txt || exit 1actual=$(sha256sum input.txt) || exit 1IFS= read -r expected < checkpoint.txt || exit 1[[ $actual != "$expected" ]] || exit 1printf 'rebuild required\n'Expected stdout:
input unchangedrebuild requiredThe digest binds the checkpoint to input bytes. A real checkpoint must also bind relevant configuration, tool version and verified output.
Preserve a previous verified file
printf 'v1\n' > active.txt || exit 1cp -- active.txt previous.txt || exit 1printf 'v2\n' > candidate.txt || exit 1printf 'v2\n' > expected.txt || exit 1cmp -s candidate.txt expected.txt || exit 1mv -T -- candidate.txt active.txt || exit 1printf 'stage=apply status=ok\n'# Synthetic post-apply check fails; restore the known previous file.cmp -s active.txt previous.txtrc=$?[[ $rc == 1 ]] || exit 1cp -- previous.txt rollback.txt || exit 1mv -T -- rollback.txt active.txt || exit 1cmp -s active.txt previous.txt || exit 1printf 'stage=rollback status=ok\n'cat active.txtExpected stdout:
stage=apply status=okstage=rollback status=okv1A synthetic check triggers rollback, and cmp verifies the restored bytes. This is a private single-file exercise, not a crash-safe transaction.
Common mistakes
Do not write a success checkpoint before verification, or retry every failure indiscriminately. Do not claim a rename or flock makes an entire workflow transactional.
Practice
Modify the retry fixture to fail permanently after two attempts and verify no success line. Design a restart protocol that matches a checkpoint to the current input digest and a rollback that preserves the previous verified artifact.
Next: Production automation capstone