1.When distributed coordination is actually needed
Distributed coordination — a lock held across multiple processes or machines — is needed only when the resource being protected is itself outside any single database that could enforce the invariant directly. If one Postgres instance already holds the data and can express "only one of these" as a transaction or a constraint, that is not a distributed-locking problem at all; it becomes one when multiple independent workers must serialize access to something a single ACID transaction cannot already cover — a shared external resource, a scheduled job that must run on exactly one of many replicas, or a critical section spanning several services and calls.
Key features
- If one transactional database already owns all the state involved, prefer its own transaction/constraint machinery over a distributed lock — it is simpler and already consistent
- Distributed coordination becomes necessary when multiple independent processes (often on different machines) must serialize access to a resource no single database transaction spans
- Common genuine use cases: leader election among replicas, ensuring a scheduled/cron job runs on exactly one worker, guarding a non-transactional external resource (a third-party API, a file on shared storage)
- A distributed lock adds a new failure mode (the lock service itself, plus network partitions between clients and it) that a single-database transaction does not have
- The question to ask first is not "how do I build the lock" but "does this invariant already live inside one system that can enforce it atomically" — most of the time it does
Deciding whether a problem needs a distributed lock
- Shared invariant — multiple processes involved
- leads to One DB already owns it (yes)
- on error, leads to Spans systems (no)
- One DB already owns it — use a transaction/constraint
- Spans systems — needs a distributed lock
Signature
distributed lock needed ⇔ invariant spans systems no single DB transaction covers
Ask "does one database already own this invariant?" before reaching for a distributed lock — if yes, use its transaction/constraint machinery; a distributed lock is for coordination that genuinely spans systems no single database transaction covers.