Backends and state
State is the durable record of every object a Netherframe app manages: its address, provider attributes, private metadata, resource identity, retention rules, and the deployment inputs used to apply it. A backend is the storage system where that record lives across runs.
Understanding how Netherframe protects state explains why runs do not clobber each other, why moving backends is explicit, and when to step up from document state to PostgreSQL graph state.
Two state models in hybrid execution
Netherframe supports two state architectures:
| Model | Backends | How state is stored | Concurrency |
|---|---|---|---|
| Document state | Local file, S3, GCS, Azure Blob, Consul, PostgreSQL (document), HCP Terraform | One versioned JSON document per app (lineage + monotonic serial) |
One active writer at a time (state lock) |
| Graph state | PostgreSQLGraphBackend |
Normalized rows (nf_entries, nf_outputs, nf_operations, nf_versions, nf_edges) |
App-wide lease by default; resource-level grants with --concurrent-applies and a retained baseline |
How document state stays safe
Document backends enforce five layers of protection:
- Mutual exclusion:
plan,apply,destroy,import, and state edits acquire the backend lock. Locally on POSIX, this is an OSflockreleased automatically if the process dies; on Windows, a heartbeat-renewed lock file; on remote backends, the backend's native lock (releasable after a crash vianf state force-unlock <id> --app <app>). - Optimistic compare-and-swap writes: Every write carries the backend's native
precondition (conditional put, ETag/generation match, CAS) requiring that stored state
still matches the exact
serialthe run read. Even if a lock lapses, a stale writer cannot overwrite a newer run's state. - Saved-plan freshness: Saved plans record the
serial,lineage, andstate_fingerprintthey were planned against;nf apply --plan-filerefuses to run if state moved in the meantime. - Periodic persistence and errored-state fallback: During an apply, completed changes
flush every
state_persist_intervalseconds (default20;0flushes every resource) and at completion. Local writes use anfsynced temp file rename and retain<state>.backup. If final storage fails, uncommitted records are written to<outdir>/errored.tfstate. - Pre-apply checkpoints: Under the default
CHECKPOINTrecovery strategy, Netherframe retains up to 5 pre-apply snapshots beside remote state (<state>.checkpoints/or backend versions) or under<outdir>/checkpoints/for local state.
How PostgreSQL graph state goes further
When a single app grows large or multiple teams apply changes concurrently, document state
becomes a serialization bottleneck. PostgreSQLGraphBackend replaces the monolithic JSON
document with transactional rows:
- Per-resource commits: Every resource change commits immediately on completion with its own revision, incarnation, and version history.
- Fenced operations: Every mutating provider call is recorded in
nf_operations(reserved → dispatched → returned → committed/aborted) tied to a renewable lease. If a worker dies or loses its lease, provider processes are killed and any uncertain address is quarantined until resolved withnf state recover. - Disjoint concurrent applies & affected planning: With
--concurrent-appliesand a retained branch baseline, runs whose changes are provably disjoint acquire fine-grained resource grants and run simultaneously; unsupported resource profiles fall back to an app-wide lease automatically.
See PostgreSQL Graph State Backend for the full protocol and schema.
Choosing a backend
| Scenario | Recommended starting point |
|---|---|
| Learning locally or single-developer rehearsal | Local state (app.enable_hybrid_execution_local(...) or default .nf.out/nf.tfstate) |
| Shared team environment or CI/CD | Remote document backend (--backend-type s3 / [tool.nf.config] or explicit S3/GCS/Azure/PostgreSQL backend in code), one state per environment |
| Multi-team shared platform app or very large state | PostgreSQLGraphBackend with pinned baselines |
| Legacy Terraform/OpenTofu compatibility pipeline | Synthesized backend block or BackendChain managed by the external CLI |
State safety guardrails
- No silent state abandonment: If your settings point to an empty configured remote
state (
serial 0, no lineage) while.nf.out/nf.tfstatestill holds managed resources, Netherframe stops the run and explains how to migrate state first rather than planning duplicate creates. - One writer per state, one state per environment: Never point
devandprodat the same state file or key. - Explicit backend migrations: Changing a backend moves where Netherframe looks for state; always migrate existing state before applying against the new backend.