Skip to content

1.15 Failure Injection and Crash Recovery for the Core: Exit Evidence

Evidence · Step: 1.15 Failure Injection and Crash Recovery for the Core

Step 1.15 Failure Injection and Crash Recovery for the Core
Date 2026-11-13
Database PostgreSQL 18.6 (postgres:18-alpine, 1 GB shared memory) in Docker Desktop on Windows 11
Result Passed
Deliverable File
Fault hooks FaultPoint and PostOptions::fault in crates/engine/src/concurrency.rs, only with the engine’s test-hooks feature
Fault tests crates/engine/tests/faults.rs
Chaos run scripts/chaos.sh and its client, crates/engine/examples/chaos_load.rs
Simulator v1 crates/sim (mezgeb-sim): workload.rs, pg.rs, rng.rs, main.rs
CI .github/workflows/ci.yml runs the simulator on the reference ledger and, with faults, on PostgreSQL
Documentation db-schema.md, Crash safety, spec.md 10

The work arrived in three pull requests: the fault hooks (#36), the chaos run (#37) and the simulator (#38).

Task Done
Fault hooks in the engine, behind a test-only feature: fail before lock, after lines, before commit, after commit Yes: FaultPoint::{BeforeLock, AfterLines, BeforeCommit, AfterCommit}. Every write commits through one helper (PostOptions::commit), so postings, reversals, adjustments, the four hold operations and the expiry worker all have all four points. Without the feature the hooks compile to nothing
A chaos script that kills PostgreSQL with docker kill during a load run, restarts it and runs verify Yes: scripts/chaos.sh uses docker compose kill -s SIGKILL, docker compose start, then chaos_load check and mezgeb-cli verify --full
mezgeb-sim v1: random workload and faults against MemLedger and PgStore, comparing them Yes: see the simulator

A change to step 1.11. Its single test hook, PostOptions::lose_response_after_commit, is replaced by FaultPoint::AfterCommit, which does the same: the transaction commits, then the caller gets an error as if the connection dropped. The step 1.11 lost-response test now uses it.

Every fault point: one entry, and a clean verify

Section titled “Every fault point: one entry, and a clean verify”

tests/faults.rs runs 11 writes at each of the 4 points, 44 cases:

  • a payment
  • an adjustment
  • a reversal
  • four hold placements
  • a capture
  • a release
  • an expiry
  • the expiry worker

Each write fails once at the point, then is retried with the same idempotency key and a fresh entry ID, as a client would. After each:

  • a crash before commit left nothing written; a lost response after commit left everything written
  • exactly one entry exists for the key
  • balances show the request took effect once
  • a full verification is clean
Test Result
a_crash_before_locking_is_retried_once Pass
a_crash_after_writing_the_lines_is_retried_once Pass
a_crash_before_commit_is_retried_once Pass
a_lost_response_after_commit_is_retried_once Pass
fault_points_are_in_order Pass

scripts/chaos.sh with its defaults: 20 cycles, 8 workers sending transfers between 10 wallets for 10 seconds per cycle. In each cycle PostgreSQL is killed with SIGKILL at a random moment between 2 and 8 seconds. There is no shutdown and no checkpoint, so the next start recovers from the WAL.

The client logs every request before sending it and every answer when it arrives. After the restart, chaos_load check compares that log with the journal:

  • every acknowledged request has exactly its entry
  • every rejected request has none
  • every request with an unknown outcome (sent, but the answer was lost in the crash) has one entry or none. It is then retried with the same key, as a client would: if it had committed, the retry returns the original entry; if not, the retry posts it once
  • money is conserved: the wallets’ total is unchanged

Then mezgeb-cli verify --full checks the whole ledger.

Cycle Killed after Sent Acked Rejected Unknown: committed / absent Recovery Check Verify
1 8 s 803 734 37 1 / 31 4.4 s pass clean
2 6 s 800 700 34 1 / 65 3.6 s pass clean
3 7 s 903 804 52 1 / 46 3.5 s pass clean
4 3 s 476 363 17 0 / 96 3.5 s pass clean
5 6 s 772 676 35 1 / 60 3.4 s pass clean
6 3 s 478 389 26 1 / 62 3.4 s pass clean
7 6 s 834 710 33 0 / 91 4.0 s pass clean
8 8 s 929 857 41 1 / 30 3.6 s pass clean
9 7 s 929 831 38 1 / 59 3.6 s pass clean
10 8 s 1066 987 40 0 / 39 3.6 s pass clean
11 2 s 413 292 14 1 / 106 3.5 s pass clean
12 3 s 522 405 13 0 / 104 3.6 s pass clean
13 6 s 790 677 27 1 / 85 3.4 s pass clean
14 7 s 880 773 45 1 / 61 3.4 s pass clean
15 3 s 484 382 20 0 / 82 3.5 s pass clean
16 8 s 989 904 47 1 / 37 3.4 s pass clean
17 2 s 423 309 14 2 / 98 3.6 s pass clean
18 4 s 614 511 19 0 / 84 3.4 s pass clean
19 3 s 504 398 16 1 / 89 3.6 s pass clean
20 4 s 623 540 27 0 / 56 3.6 s pass clean

Recovery is the time from docker compose start until PostgreSQL accepted connections again.

Totals over the 20 cycles:

Transfers sent 14,232
Acknowledged 13,569: every one found in the journal, exactly once
Rejected 663: none found in the journal
Unknown outcome 1,395
… committed, though the client never got the answer 14: each retry returned the original entry
… not committed 1,381: each retry posted it once
Violations 0
Verify after each restart clean, 20 of 20 (27,158 lines at the end)

The 14 committed-but-unknown requests are the case that matters most: the commit reached the WAL, but the client never heard back. The idempotency key makes the retry safe, so the client gets the same answer and the money moves once.

mezgeb-sim runs a random workload, the same for a given seed:

  • Operations: deposits, transfers, adjustments, reversals, and holds (place, capture, release, expire), plus time passing
  • Lanes: the work is split into independent lanes, each with its own accounts, client, clock, random numbers and reference ledger. Lanes share nothing, so they run at the same time on one database, and each operation still has exactly one right answer.
  • On PostgreSQL: every operation goes through the engine and through the reference ledger (MemLedger), and the two answers must be identical, including refusals and their reasons.
  • Faults: with --faults N, N in 1,000 attempts fail at a random FaultPoint. Each failed attempt is retried with the same key until one runs without a fault, and that answer must match the reference ledger’s.
  • Checks during the run: the reference ledger’s invariant checker runs every 2,000 operations in each lane.
  • Checks at the end: every account and every hold is compared, and so is each lane’s entry count, and then a full verification must be clean.

A failure prints its seed, and running that seed again repeats the same run.

Zero invariant violations across 20 kill cycles and 1M simulated operations.

20 kill cycles: 0 violations, and verify was clean after every restart. See the table above.

1M simulated operations on PostgreSQL with faults. A single run of a million operations ran out of memory on this machine at about 535,000, because the 32 reference ledgers grow with the run. That was not a ledger error, and the database it left behind verified clean (1,070,436 lines). The million was therefore run as 10 runs of 100,000 operations, each with its own seed and its own database. Every run used 32 lanes with faults on 10% of attempts.

Seed Entries written Refused by the rules Faults injected Accounts / holds / entries compared Verify Time
101 76,883 18,252 8,280 identical clean 519 s
102 76,695 18,135 8,203 identical clean 382 s
103 76,487 18,612 8,362 identical clean 311 s
104 76,669 18,170 8,278 identical clean 374 s
105 76,188 18,759 8,189 identical clean 377 s
106 76,183 18,535 8,241 identical clean 432 s
107 76,571 18,516 8,472 identical clean 405 s
108 76,712 18,315 8,084 identical clean 394 s
109 76,576 18,356 8,294 identical clean 420 s
110 76,344 18,591 8,153 identical clean 391 s
Total 765,308 184,241 82,556 10 of 10 10 of 10 67 min

The 82,556 faults by point:

Before lock After lines Before commit After commit
21,025 20,303 20,824 20,404

Every faulted operation was retried to the same answer as the reference ledger, and the reference ledger’s invariants held at every check (64 per run). For example, seed 101 compared 13,480 accounts, 13,126 holds (7,644 still active) and 76,883 entries.

The same million in one run against the reference ledger alone (seed 15, 32 lanes, 770,493 entries written) also passed.

Pass.

Gate: recorded chaos run results are committed to docs/evidence/.

They are in this file: the 20-cycle table and its totals above.

Result: passed.

  • A one-connection pool deadlocked the chaos setup. chaos_load setup held its only connection while opening accounts, which needs a connection of its own. It now opens the accounts first.
  • The simulator’s reference ledgers grow with the run. A million operations in one PostgreSQL run needs more memory than this machine had, so the million was split into ten runs. A later simulator can trim what each lane keeps.
  • Cargo merges features across the workspace. Building the whole workspace together turns on the engine’s test-hooks for every crate, because the simulator needs it. A release build of mezgeb-cli must be built on its own (cargo build -p mezgeb-cli).