Skip to content

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:

  1. Mutual exclusion: plan, apply, destroy, import, and state edits acquire the backend lock. Locally on POSIX, this is an OS flock released automatically if the process dies; on Windows, a heartbeat-renewed lock file; on remote backends, the backend's native lock (releasable after a crash via nf state force-unlock <id> --app <app>).
  2. 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 serial the run read. Even if a lock lapses, a stale writer cannot overwrite a newer run's state.
  3. Saved-plan freshness: Saved plans record the serial, lineage, and state_fingerprint they were planned against; nf apply --plan-file refuses to run if state moved in the meantime.
  4. Periodic persistence and errored-state fallback: During an apply, completed changes flush every state_persist_interval seconds (default 20; 0 flushes every resource) and at completion. Local writes use an fsynced temp file rename and retain <state>.backup. If final storage fails, uncommitted records are written to <outdir>/errored.tfstate.
  5. Pre-apply checkpoints: Under the default CHECKPOINT recovery 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 with nf state recover.
  • Disjoint concurrent applies & affected planning: With --concurrent-applies and 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.tfstate still 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 dev and prod at 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.