Pcornel
Automation · Parallelism & Concurrency

Resource Locking

Cheat sheet · https://pcornel.dk/topic.php?topic=5992

Back to the Topic

Answer

Resource locking ensures only one job changes a resource at a time, using file locks (flock), database locks, Terraform state locking or lease-based locks in distributed systems.

Remember

  • Use file locks, database locks, state locks or leases.
  • Release locks even on failure.
  • Handle stale locks carefully.

First checks

  • flock -n /var/lock/<job>.lock <cmd> — file lock
  • Distributed locks (Consul/etcd/Redis) — multi-host
  • Terraform state locking — IaC

Verify in this order

task → acquire lock → work → release

Where it usually breaks

Configuration/input → graph/state refresh → plan → provider API request → provider terminal result → resource read-back → effective service state → workload.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5992

Confirmation

Confirm action

Continue?