Unit IV - Concurrency Control Using Locks in Distributed Systems
Unit IV - Concurrency Control Using Locks in Distributed Systems
● Scenario:
○ User A initiates a withdrawal of $100.
○ User B simultaneously initiates a deposit of $100.
Step-by-Step Breakdown:
3. Order of Operations:
○ User A updates the balance to $900 ($1000 - $100).
○ Before this update is finalized, User B sets the balance to $1100 ($1000 + $100), overwriting User A’s update.
Final Outcome:
The account balance is incorrectly set to $1100 instead of the expected $1000.
● Definition: Assumes conflicts are likely and locks resources from the beginning until a transaction completes.
● Types:
○ Exclusive Lock (Write Lock): Only one transaction can write to the resource, and no other transactions can read or
write until the lock is released.
○ Shared Lock (Read Lock): Multiple transactions can hold a shared lock for reading, but no transaction can write until
all shared locks are released.
● Usage: Suitable for high-contention environments, where it’s essential to prevent conflicts actively.
Optimistic Locks
● Definition: Assumes conflicts are rare and does not restrict access upfront. Conflicts are checked at commit time.
● Implementation: Rather than using physical locks, a version number or timestamp is often used. If data has changed since
the read, the transaction is aborted.
● Usage: Preferred in low-contention systems where most operations do not conflict.
Pessimistic Locking
Optimistic Locking
Challenges and Considerations with Lock-Based
Concurrency Control
1. Deadlocks
○ Description: Occurs when two or more transactions are waiting indefinitely for each other to release locks.
○ Mitigation Techniques:
■ Deadlock Detection: Monitor the system for circular wait patterns and resolve deadlocks by rolling back one of
the transactions.
■ Deadlock Prevention: Use techniques such as setting a lock timeout or enforcing an ordering of resource
access to avoid circular waits.
2. Lock Granularity
○ Description: Defines the size of the data locked (e.g., entire database, table, row, or field).
○ Trade-offs:
■ Coarse-Grained Locks: (e.g., table-level locks) are easier to manage but reduce concurrency.
■ Fine-Grained Locks: (e.g., row-level locks) increase concurrency but add overhead and complexity.
3. Scalability and Performance
○ Centralized locking mechanisms may struggle under high demand, while distributed approaches increase complexity
and may suffer from latency due to consensus protocols.
○ Solution: Choose a locking granularity and approach based on workload characteristics. For instance, use fine-grained
distributed locks for high-concurrency applications like distributed databases.
Distributed Deadlocks
and Transaction Recovery
Distributed Deadlocks
Definition: A distributed deadlock occurs when two or more transactions in
a distributed system form a circular wait on resources, preventing progress.
Example:
2. Distributed Detection
○ Each node monitors its WFG and communicates with others to identify
cycles.
○ Scalable but more complex and prone to communication overhead.
Deadlock Recovery Strategies
1. Transaction Abortion
○ Abort one or more transactions in the deadlock cycle to release resources.
○ Criteria for selection:
■ Transaction priority.
■ Rollback cost.
■ Number of resources held.
2. Timeout-Based Recovery
○ Abort transactions that exceed a predefined wait time.
○ Pros: Easy to implement.
○ Cons: May abort non-deadlocked transactions unnecessarily.
3. Prevention-Based Recovery
○ Ensure no circular waits occur by:
■ Enforcing a resource acquisition order.
■ Limiting resource holding time.
Challenges in Distributed Deadlock Recovery
Latency: Communication delays can make cycle detection slow.
Transaction Dependencies: Rolling back one transaction can impact others, leading to cascading aborts.
Resource Contention: Resource wastage. Frequent deadlocks in high-contention environments require fine-tuned recovery policies.
Key Takeaways from Deadlock Detection & Recovery
● Detection vs. Prevention:
○ Detection: Identify deadlocks when they occur and resolve them.
○ Prevention: Avoid conditions leading to deadlocks.
● Balancing Recovery and Performance: Avoid frequent transaction rollbacks to ensure system efficiency.