What must survive

Think of it when something would be lost if the server restarted right now.

In everyday life

A restaurant can reprint its recipes at any time, like code. The slips that say what each table ordered are state: if they blow off the counter, no recipe brings them back.

One case

A shop’s server keeps every shopping cart in memoryHeld by a running program only. It is gone when the program stops or the machine restarts.. At 03:00 the server restarts for an update. Every cart is now empty. Customers come back to an empty cart, and many of them leave.

The cart lived in memory, so the restart erased it
The case, cell by cell1 cell = 30 minutes
Items customers put in carts
12182124242426
Items the server still holds
12182124002
Items lost at the restart
24
Code lost
0
The cart items were state. The code survived the restart; the state did not.

Every case

A program holds two kinds of things. Code can be rebuilt from the repo at any time. State exists only where it was saved, and some of it, such as orders, must outlive every restart. A code bug costs you a fix and a redeploy. Lost state costs you the data itself, which is why engineers fear the words “data loss” more than any bug.

The word

Engineers call this

stateThe information a program holds at a given moment, such as which screen is open or what is in a cart. Some of it can vanish safely; the state that must survive a restart, such as orders and accounts, cannot be rebuilt from code once it is lost.

Know where your state lives. Keep the state that must survive somewhere durableSaved somewhere that survives a crash or a restart, such as a disk., and change it with more care than you change code.

Where this idea leads

Skills that encode it

  • principle-make-operations-idempotent pstackAsks “what happens if the previous run crashed halfway?” and derives each step from the state that exists.