logo
TrungTQ

Database Lock Modes: Ensuring data consistency and integrity

  • Author: Administrator
  • Published On: 11 Jul 2025
  • Category: Database

Database Lock Modes: Ensuring data consistency and integrity

In a multi-transaction environment, ensuring consistency and data integrity is extremely important. Database locking is a mechanism used to control concurrent access to data, preventing conflicts that can cause unwanted errors. This article will delve into common types of lock modes, lock granularity, common problems, and solutions.

What is Database Lock?

A database lock is a mechanism for controlling concurrent access to data in a database management system ( RDBMS ). When a transaction requests a lock on a piece of data, other transactions may be blocked until the lock is released. The main purpose is to prevent concurrent operations from causing data loss, information corruption, or consistency issues.

Why do we need Database Lock?

Imagine two transactions trying to update a record in a database at the same time. Without a lock, both transactions could read the same initial value, make changes based on that value, and then write back the results. The later transaction could overwrite the earlier transaction's changes, resulting in data loss. Database locks prevent this by ensuring that only one transaction can access and change the data at a given time.

Main Lock Mode Types

Shared Lock (S)

A shared lock, also known as a read lock, allows multiple transactions to read data concurrently. However, no transaction is allowed to modify the data that is held under a shared lock. A shared lock is used when a transaction only needs to read data and does not need to change it.

For example, multiple users view product information on an e-commerce website. Transactions that read product information can use a shared lock to ensure that the data is not changed during their viewing.

Exclusive Lock (X)

An exclusive lock, also known as a write lock, allows a single transaction to modify data. When a transaction holds an exclusive lock on a piece of data, no other transaction is allowed to read or modify that data until the exclusive lock is released.

For example, when a user updates personal information on a website, the update transaction uses an exclusive lock to ensure that no one else can change that information during the update.

Intent Locks (IS, IX, SIX)

Intent locks are used to establish a lock hierarchy. They indicate that a transaction intends to obtain a shared or exclusive lock on a resource at a more granular level.

  • Intent Shared (IS): Indicates that a transaction intends to acquire a shared lock on some resource within the scope of this lock.
  • Intent Exclusive (IX): Indicates that a transaction intends to obtain an exclusive lock on some resource within the scope of this lock.
  • Shared Intent Exclusive (SIX): Indicates that a transaction holds a shared lock on all resources within this lock scope, but intends to obtain an exclusive lock on some specific resources.

For example, a transaction wants to update some rows in a table. It can take an IX lock on the table to indicate this intention, and then take an exclusive lock on each specific row as it updates.

Lock Granularity

Lock granularity refers to the level of data locking, that is, at what level: row, page, table, or even the entire database. Choosing the right lock granularity has a big impact on the performance and parallelism of the system.

  • Row-level locking: Row -level locking allows multiple transactions to access and modify different rows in the same table concurrently, increasing parallelism. However, it can cause significant overhead if a transaction needs to lock multiple rows.
  • Page-level locking: Locking at the page level (a unit of data storage on disk). This level is a balance between row-level locking and table-level locking.
  • Table-level locking: Locks the entire table. Simple and low overhead, but significantly reduces parallelism since only one transaction can access the table at a time.

For example, an e-commerce system might use row-level locking for transactions that update product inventory quantities, to allow multiple users to purchase products at the same time without conflicts.

Common Problems: Deadlock and Lock Contention

Deadlock

Deadlock occurs when two or more transactions wait for each other to release their locks, forming an endless loop. For example, transaction A holds a lock on resource X and waits for a lock on resource Y, while transaction B holds a lock on resource Y and waits for a lock on resource X.

Solution:

  • Deadlock detection: The system periodically checks the waiting loops and aborts one of the transactions involved in the deadlock (usually the transaction with the lowest undo cost).
  • Lock timeout: Set the maximum timeout for a lock. If a transaction cannot obtain a lock within this time, it will be aborted.
  • Lock ordering: Requires transactions to acquire locks in a certain order to avoid creating waiting loops.

Lock Contention

Lock contention occurs when multiple transactions attempt to access the same resource and have to wait to acquire a lock. This can degrade system performance.

Solution:

  • Reduce lock holding time: Hold locks for as short a time as possible to reduce the chance of other transactions having to wait.
  • Use appropriate lock granularity: Choose the appropriate level of data locking to reduce conflicts. For example, use row-level locking instead of table-level locking where possible.
  • Query Optimization: Optimize database queries to reduce execution time and reduce lock holding time.
  • Sharding : Divide data into smaller parts and store them on different servers to reduce access conflicts.

Pessimistic Locking vs. Optimistic Concurrency Control

Pessimistic Locking

Pessimistic locking is a concurrency control method in which a transaction obtains a lock on data before performing any operations. This method assumes that conflicts are common and attempts to prevent them from occurring in the first place.

Advantages: Simple and easy to understand, ensures data consistency. Disadvantages: Can reduce system performance due to transactions having to wait to obtain locks.

Optimistic Concurrency Control (OCC)

Optimistic concurrency control is a method of concurrency control in which a transaction does not acquire a lock on data. Instead, it checks to see if the data has been changed when it commits. If the data has been changed, the transaction is aborted.

Advantages: Increases parallelism and system performance because transactions do not have to wait to acquire locks. Disadvantages: More complex than pessimistic locking, can lead to frequent aborts of transactions if conflicts are common.

For example, an airline ticketing system might use optimistic concurrency control to allow multiple users to book the same flight. When a user completes the booking process, the system checks to see if there are enough tickets left. If not, the transaction is canceled.

Transaction Isolation Levels and Lock Modes

Transaction isolation levels define the degree to which transactions are isolated from each other. The higher the isolation level, the more data consistency is guaranteed, but system performance may be reduced.

Common transaction isolation levels:

  • READ UNCOMMITTED: Allows a transaction to read data that has not been committed by another transaction ("dirty read"). Lowest isolation level, rarely used in practice.
  • READ COMMITTED: Allows a transaction to read only data that has been committed by another transaction. Prevents dirty reads, but "non-repeatable reads" (a transaction reads the same data twice but gets two different values) can occur.
  • REPEATABLE READ: Ensures that a transaction reading the same data multiple times will always get the same value. Prevents non-repeatable reads, but "phantom reads" can occur (one transaction executes a query and gets one result set, then another transaction inserts more data matching that query, and when the first transaction executes the query again, it gets a different result set).
  • SERIALIZABLE: The highest level of isolation, ensuring that transactions are executed serially as if they were executed independently. Prevents all concurrency issues, but can significantly reduce system performance.

Relationship between transaction isolation levels and lock modes:

  • Higher isolation levels typically use more locks and hold locks longer to ensure data consistency.
  • For example, SERIALIZABLE often uses range locks to prevent phantom reads.

For example, a banking system might use a SERIALIZABLE isolation level for money transfers to ensure correctness and prevent concurrency problems.

Conclude

Database lock modes are an important tool for ensuring data consistency and integrity in multi-transaction environments. Understanding the types of lock modes, lock granularity, common problems, and solutions is essential for designing and implementing efficient and reliable database systems. The choice between pessimistic locking and optimistic concurrency control, as well as the configuration of transaction isolation levels, requires careful consideration to achieve a balance between data consistency and system performance.

Hopefully this article has given you a good overview and details about database lock modes. To learn more about related topics, you can refer to other articles on our blog, such as ACID in Databases and Database Scaling .

  • Share On: