Chapter 8

Persistent state, updates, and crash recovery

This chapter explains how a device changes what it has stored, including its own software, without being left broken when power fails at the worst moment. It starts from the meaning of persistent state §8.1 and three guarantees that are easy to confuse: durability §8.2, which says that a completed change survives a failure; atomicity §8.3, which says that a change happens completely or not at all; and crash consistency §8.4, which says that recovery always finds a valid state. It then presents the standard designs that provide these guarantees, such as transactions §8.7, two-phase commit §8.8, shadow paging §8.9, copy-on-write §8.11, write-ahead logging §8.12 and A/B updates §8.10, and the log-structured storage used on flash memory §6.6, with its costs in wear and extra writing.

The second half applies these ideas to software updates: the order of writes, the small record whose write makes an update take effect, the separate steps of staging, committing and activating, trial runs that must be confirmed, and the return to the last version that worked. It also covers protection against reinstalling old versions with known flaws, monotonic counters §8.28, operations that are safe to repeat, and the state machines §8.33 that tie the steps together. The final entries show how to test all of this by cutting the power or resetting the device at chosen moments.

In this chapter

  1. 8.1 Persistent state
  2. 8.2 Durability
  3. 8.3 Atomicity
  4. 8.4 Crash consistency
  5. 8.5 Failure atomicity
  6. 8.6 Linearization point
  7. 8.7 Transaction
  8. 8.8 Two-phase commit
  9. 8.9 Shadow paging
  10. 8.10 A/B update
  11. 8.11 Copy-on-write
  12. 8.12 Write-ahead logging
  13. 8.13 Append-only log
  14. 8.14 Circular log
  15. 8.15 NVS record
  16. 8.16 Garbage collection
  17. 8.17 Flash wear and endurance
  18. 8.18 Write amplification
  19. 8.19 Torn write
  20. 8.20 Write ordering
  21. 8.21 Commit record and selector
  22. 8.22 Staging versus commit versus activation
  23. 8.23 Commit-confirmed update
  24. 8.24 Confirmation window
  25. 8.25 Last-known-good state
  26. 8.26 Rollback versus anti-rollback
  27. 8.27 Version high-water mark
  28. 8.28 Monotonic counter
  29. 8.29 Recovery fallback
  30. 8.30 Boot validation versus arrival authentication
  31. 8.31 Idempotency
  32. 8.32 Exactly-once effect
  33. 8.33 Finite-state machine
  34. 8.34 State transition guard
  35. 8.35 Reachability after update
  36. 8.36 Schema migration
  37. 8.37 Reset injection
  38. 8.38 Power-loss injection