Multiple Granularity Locking

From CS Wiki

Multiple Granularity Locking is a concurrency control mechanism used in database systems to improve performance and manage locks at various levels of granularity, such as tuples, pages, tables, or the entire database. By allowing transactions to lock data items at different granular levels, this method optimizes the balance between concurrency and locking overhead.

Key Concepts[edit | edit source]

  • Lock Granularity: Refers to the size of the data item on which a lock is applied, ranging from fine-grained (individual records) to coarse-grained (entire tables).
  • Intention Locks: Special locks used to indicate a transaction's intention to acquire more restrictive locks at a lower level.
  • Lock Modes: Different types of locks that specify the level of access, including shared locks (S), exclusive locks (X), intention shared (IS), intention exclusive (IX), shared and intention exclusive (SIX).

Lock Modes and Compatibility[edit | edit source]

Multiple granularity locking introduces additional lock modes beyond the standard shared and exclusive locks:

  • Intention Shared (IS): Indicates intention to acquire shared locks on lower-level nodes.
  • Intention Exclusive (IX): Indicates intention to acquire exclusive locks on lower-level nodes.
  • Shared and Intention Exclusive (SIX): A combination of a shared lock at the current level and intention exclusive locks on lower levels.
  • Shared (S): Allows reading but not writing of a data item.
  • Exclusive (X): Allows both reading and writing of a data item.

The compatibility matrix for these lock modes is as follows:

IS IX S SIX X
IS Yes Yes Yes Yes No
IX Yes Yes No No No
S Yes No Yes No No
SIX Yes No No No No
X No No No No No

How Multiple Granularity Locking Works[edit | edit source]

  1. Hierarchy Structure: The database is structured as a hierarchy, such as database → table → page → record.
  2. Intention Locks: Transactions acquire intention locks on higher-level nodes before acquiring actual locks on lower-level nodes.
  3. Locking Protocol: Transactions follow a protocol to request locks in a specific order to prevent deadlocks and ensure serializability.
  4. Lock Compatibility Checks: Before granting a lock, the system checks the compatibility matrix to ensure no conflicting locks are held by other transactions.

Example[edit | edit source]

Suppose a transaction wants to update a record in a table:

  1. The transaction acquires an IX lock on the database.
  2. Acquires an IX lock on the table.
  3. Acquires an X lock on the specific record.

Another transaction wanting to read the entire table would:

  1. Acquire an IS lock on the database.
  2. Acquire an S lock on the table.

Since IX and S locks are incompatible at the table level, the second transaction must wait until the first transaction releases its locks, preventing inconsistent reads.

Advantages[edit | edit source]

  • Improved Concurrency: Allows multiple transactions to access different levels of data without unnecessary blocking.
  • Reduced Locking Overhead: Minimizes the number of locks required by locking larger granules when appropriate.
  • Deadlock Prevention: Structured locking protocol reduces the likelihood of deadlocks.

Limitations[edit | edit source]

  • Complexity: Implementation and management of multiple lock modes and intention locks add complexity.
  • Overhead: Maintaining lock hierarchies and performing compatibility checks can introduce overhead.
  • Potential for Reduced Concurrency: Coarse-grained locks can still limit concurrency if not managed properly.

Applications[edit | edit source]

Multiple granularity locking is widely used in:

  • Database Management Systems: Relational databases like Oracle, SQL Server, and DB2 to manage transaction concurrency.
  • Distributed Systems: Coordinating access to shared resources in distributed computing environments.

Related Concepts and See Also[edit | edit source]