Primary Copy Two-Phase Locking

IT 위키

Primary Copy Two-Phase Locking (2PL) is a concurrency control protocol used in distributed database systems where a primary copy of each data item is designated for lock management. Transactions must obtain locks from the primary copy of the data item to ensure consistency and serializability across the system.

Key Concepts[편집 | 원본 편집]

  • Primary Copy: A specific node in the distributed system is designated as the authoritative source for managing locks for a particular data item.
  • Consistency: By managing locks through the primary copy, the system ensures that all transactions follow a serializable schedule.
  • Two-Phase Locking: The protocol adheres to the Two-Phase Locking rules:
    • Growing Phase: Transactions acquire locks but do not release them.
    • Shrinking Phase: Transactions release locks but do not acquire new ones.

How Primary Copy 2PL Works[편집 | 원본 편집]

The Primary Copy 2PL protocol operates as follows:

  1. Each data item is associated with a primary copy stored at a specific node.
  2. Transactions interact with the primary copy to acquire and release locks.
  3. The primary copy follows the standard Two-Phase Locking protocol to manage locks.
  4. Once a lock is granted by the primary copy, the transaction can read or write the data item at any replica site.

Advantages[편집 | 원본 편집]

  • Centralized Lock Management per Item: Simplifies lock management by delegating responsibility to the primary copy.
  • Consistency Guarantee: Ensures serializability by centralizing lock decisions at the primary copy.
  • Improved Performance: Reduces lock management overhead compared to fully distributed locking.

Limitations[편집 | 원본 편집]

  • Single Point of Failure: If the primary copy node fails, the data item becomes unavailable for transactions.
  • Communication Overhead: Lock requests and updates must be routed through the primary copy, increasing network traffic.
  • Limited Scalability: High contention for locks on frequently accessed data items can lead to bottlenecks.

Example of Primary Copy 2PL[편집 | 원본 편집]

Consider a distributed database where:

  • Node 1 hosts the primary copy of data item A.
  • Node 2 hosts the primary copy of data item B.

Scenario[편집 | 원본 편집]

Transaction T1 and T2 execute as follows:

Step Transaction T1 Transaction T2 Primary Copy Action
1 BEGIN TRANSACTION BEGIN TRANSACTION Both transactions start.
2 READ(A) - T1 requests a shared lock for A from Node 1 (primary copy for A).
3 - WRITE(A) T2 requests an exclusive lock for A from Node 1 but must wait as T1 holds a shared lock.
4 COMMIT - T1 releases the lock on A.
5 - WRITE(A) T2 acquires the exclusive lock on A and writes.
6 COMMIT - T2 releases the lock.

Applications[편집 | 원본 편집]

Primary Copy 2PL is used in distributed systems where:

  • Global Consistency: Strict transaction isolation and serializability are required.
  • Read-Heavy Workloads: Reduces contention for locks by allowing transactions to read from replicas while acquiring locks from the primary copy.
  • Simplified Locking: Centralizing lock management for each data item simplifies implementation.

Comparison with Primary Site 2PL[편집 | 원본 편집]

Feature Primary Copy 2PL Primary Site 2PL
Lock Management Delegated to primary copy for each data item Centralized for all operations on the primary site
Fault Tolerance Affected by failure of the primary copy Affected by failure of the primary site
Communication Overhead Moderate (per data item) Potentially higher for all transactions
Scalability High (per data item) Moderate (site-wide management)

Challenges[편집 | 원본 편집]

Primary Copy 2PL faces the following challenges:

  • Replication Coordination: Ensuring consistency between the primary copy and replicas requires additional communication.
  • Hotspots: Frequently accessed data items can create contention on their primary copy.
  • Recovery Complexity: Managing recovery after a primary copy failure requires synchronization with replicas.

See Also[편집 | 원본 편집]