Distributed Locks via Consensus
What you'll learn: How consensus systems like ZooKeeper and etcd provide reliable distributed locking to coordinate actions across multiple servers.
The Problem: Coordinating Access to Shared Resources
When multiple servers need to perform an operation that only one should do at a time—like migrating a database, processing a single job, or updating a shared configuration—you need a distributed lock. Without coordination, multiple servers might act simultaneously, causing corruption or duplicate work (a race condition).
A simple approach might use a database flag, but what if the server holding the lock crashes? The lock stays held forever. What if two servers check the flag at the exact same moment? Both might think they won the lock.
How Consensus Systems Solve This
Consensus systems like ZooKeeper and etcd (which you've already learned about) use Raft or similar algorithms to maintain a strongly consistent view of state across a cluster. This makes them perfect for distributed locks:
- Create an ephemeral resource: A client creates a special node/key with a session/lease
- Only one succeeds: Due to consensus, only one client can create a specific lock resource
- Automatic cleanup: If the lock holder crashes, its session expires and the lock is automatically released
- Ordering guarantees: Sequential nodes in ZooKeeper or versioned keys in etcd ensure fairness
Simple Mental Model
Think of it like a teacher handing out a single hall pass. When a student (server) wants to leave class (acquire the lock), they must physically hold the pass. If they don't return (crash), the teacher notices after a timeout and makes the pass available again. The consensus system is the trustworthy teacher who never forgets or makes mistakes.
Key Takeaway: Distributed locks built on consensus systems like ZooKeeper and etcd provide reliable, crash-resistant coordination by leveraging strong consistency guarantees and automatic session management to prevent race conditions across distributed servers.