Search⌘ K

Locking Strategies

Explore locking strategies used in database concurrency control to ensure data integrity during concurrent transactions. Understand two-phase locking with shared and exclusive locks, predicate locking that handles conditional locks, and index range locking that improves concurrency by locking existing keys only. This lesson explains how each strategy balances consistency and performance in complex database systems.

A lock on a database item ensures that only one transaction updates that item in the presence of concurrent transactions. A lock ensures that the database remains consistent before and after the transaction. We’ll discuss three different types of locking strategies in this lesson:

  • Two-phase locking

  • Predicate locking

  • Index range locking

Two-phase locking

Two-phase locking (2PL) is a type of concurrency control that provides a serializability isolation level. In two-phase locking, multiple transactions are allowed to read concurrently if no transaction wants to write the data. When a transaction wants to write the data, it takes an exclusive lock and blocks all readers and writers.

In a two-phase locking:

  • Writers block other writers and readers.

  • Readers don't block other readers.

Two-phase locking blocks readers and writers by taking a lock in the database. There are two types of locks:

  • Shared lock: A lock acquired by readers that allows concurrent transactions to read data independently without blocking each other.

  • Exclusive lock: A lock acquired by a writer that blocks all other readers and ...