Use this when only one job may change a resource at a time.
Resource Locking
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.
1. Locks serialise access to shared resources.
2. Local (flock) or distributed.
3. Timeouts and stale lock handling.
4. Always release.
5. Log lock waits.
task → acquire lock → work → release
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
- Stale locks after crashes with no timeout.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Use file locks, database locks, state locks or leases.
- Release locks even on failure.
- Handle stale locks carefully.
flock -n /var/lock/<job>.lock <cmd>— file lock- Distributed locks (Consul/etcd/Redis) — multi-host
- Terraform state locking — IaC
ReferenceWhy it matters1 section · depth on demand
What it is
Resource locking ensures only one job changes a resource at a time (file locks, Terraform state locks, API locks).
ReferenceHow it works1 section · depth on demand
How it works
A lock gives one job exclusive access; others wait or skip. Use locks with timeouts and owner information so stale locks after crashes can be detected and released safely.
ReferenceImportant terms & boundaries1 section · depth on demand
Example
flock -n /run/lock/sw01.lock ./change.sh sw01
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Parallelism & Concurrency > Resource Locking. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.python.org/3/library/concurrent.futures.html · confidence 94%