SystemExpertsSystemExperts
Pricing

Patterns

35 items

Horizontal Scaling Pattern

15mbeginner

Retry with Backoff Pattern

15mbeginner

Replication Pattern

25mintermediate

Caching Strategies Pattern

25mintermediate

Persistent Connections Pattern

20mintermediate

Load Balancing Pattern

20mintermediate

Fan-out Pattern

20mintermediate

Fan-in Pattern

20mintermediate

Circuit Breaker Pattern

20mintermediate

Eventual Consistency Pattern

25mintermediate

Queue-based Load Leveling Pattern

20mintermediate

Bloom Filters Pattern

20mintermediate

Time-Series Storage Pattern

20mintermediate

Bulkhead Pattern

20mintermediate

Batch Processing Pattern

20mintermediate

Write-Ahead Log Pattern

20mintermediate

API Gateway Pattern

20mintermediate

Backend for Frontend Pattern

20mintermediate

Sidecar Pattern

20mintermediate

Idempotency Pattern

20mintermediate

Rate Limiting Pattern

20mintermediate

Backpressure Pattern

20mintermediate

Pub/Sub Pattern

25mintermediate

Strong Consistency Pattern

30madvanced

Conflict Resolution Pattern

25madvanced

Leader Election Pattern

25madvanced

Consensus Protocols Pattern

30madvanced

CQRS Pattern

28madvanced

LSM Trees Pattern

25madvanced

Sharding Pattern

25madvanced

Event Sourcing Pattern

30madvanced

Stream Processing Pattern

25madvanced

Change Data Capture Pattern

25madvanced

Distributed Locking Pattern

25madvanced

Two-Phase Commit Pattern

25madvanced
System Design Pattern
Coordinationdistributed-lockmutexredlockcoordinationcritical-sectionadvanced

Distributed Locking Pattern

Mutual exclusion across distributed nodes

Used in: Redis, ZooKeeper, DynamoDB|25 min read

Summary

Distributed locking provides mutual exclusion across multiple processes or servers, ensuring only one client can access a resource at a time. Unlike local locks, distributed locks must handle network partitions, process crashes, and clock skew. Implementations include Redis (Redlock), ZooKeeper, etcd, and database-based locks. Key challenges are: ensuring lock release on failure (TTL/leases), preventing stale lock holders from operating (fencing tokens), and handling split-brain. Essential for coordinating access to shared resources in distributed systems.

Key Takeaways

Locks Must Auto-Expire

If lock holder crashes, lock must eventually release. Use TTL or lease-based expiration. But TTL creates timing issues - holder might still be working when lock expires.

Fencing Tokens Prevent Stale Operations

After lock expires, old holder might still send operations. Include monotonically increasing token with all operations. Storage rejects operations with old tokens.

Redlock is Controversial

Redis Redlock attempts fault-tolerant locking across N Redis instances. Requires majority. Criticized for clock dependency. Use with caution for critical sections.

Scenario: Two processes try to withdraw $100 from account with $150 balance.

Without locking: 1. Process A reads balance: $150 2. Process B reads balance: $150 3. Process A: $150 - $100 = $50, writes $50 4. Process B: $150 - $100 = $50, writes $50 5. Result: $50 balance, but $200 withdrawn!

With distributed lock: 1. Process A acquires lock 2. Process A reads $150, withdraws $100, writes $50 3. Process A releases lock 4. Process B acquires lock 5. Process B reads $50, insufficient funds!

Summary

Distributed locking provides mutual exclusion across multiple processes or servers, ensuring only one client can access a resource at a time. Unlike local locks, distributed locks must handle network partitions, process crashes, and clock skew. Implementations include Redis (Redlock), ZooKeeper, etcd, and database-based locks. Key challenges are: ensuring lock release on failure (TTL/leases), preventing stale lock holders from operating (fencing tokens), and handling split-brain. Essential for coordinating access to shared resources in distributed systems.

Key Takeaways

Locks Must Auto-Expire

If lock holder crashes, lock must eventually release. Use TTL or lease-based expiration. But TTL creates timing issues - holder might still be working when lock expires.

Fencing Tokens Prevent Stale Operations

After lock expires, old holder might still send operations. Include monotonically increasing token with all operations. Storage rejects operations with old tokens.

Redlock is Controversial

Redis Redlock attempts fault-tolerant locking across N Redis instances. Requires majority. Criticized for clock dependency. Use with caution for critical sections.

ZooKeeper/etcd Are Safer

Consensus-based systems (ZK, etcd) provide linearizable operations. More reliable than Redis for critical locks. Higher latency but stronger guarantees.

Lock Granularity Matters

Coarse locks (one lock for all resources) are simple but serialize everything. Fine-grained locks (per-resource) allow parallelism but increase complexity.

Deadlock Detection/Prevention

Multiple locks can deadlock. Use lock ordering, timeouts, or deadlock detection. In distributed systems, timeouts are most practical.

Pattern Details

Scenario: Two processes try to withdraw $100 from account with $150 balance.

Without locking: 1. Process A reads balance: $150 2. Process B reads balance: $150 3. Process A: $150 - $100 = $50, writes $50 4. Process B: $150 - $100 = $50, writes $50 5. Result: $50 balance, but $200 withdrawn!

With distributed lock: 1. Process A acquires lock 2. Process A reads $150, withdraws $100, writes $50 3. Process A releases lock 4. Process B acquires lock 5. Process B reads $50, insufficient funds!

Trade-offs

AspectAdvantageDisadvantage

Premium Content

Sign in to access this content or upgrade for full access.