• Articles
  • Tutorials
  • Interview Questions

Serializability in DBMS

Serializability in DBMS

Table of content

Show More

In this blog, we will discuss the concept of serializability in DBMS, its importance, and how it works. We will also look into the different types of serializability, and their impact on the database operations.

Check out this SQL full course video to learn the SQL concepts:

Video Thumbnail

What is Serializability?

Serializability refers to the property that ensures transactions are executed in a way that maintains the integrity of the data. In simple terms, it means that even when multiple transactions are happening simultaneously, it appears as if they are running one after the other, in a serialized manner. 

This prevents conflicts and inconsistencies that may arise when transactions access and modify the same data concurrently. Serializability guarantees that the final result of executing multiple transactions will be the same as if they were executed sequentially. It is a fundamental concept in database management systems to ensure data accuracy and maintain the ACID (Atomicity, Consistency, Isolation, Durability) properties.

To understand serializability, it is essential to understand the concept of a serial schedule. A serial schedule is a sequence of transactions in which each transaction executes to completion before the next transaction begins. A serial schedule is easy to reason about because it is deterministic, and the order in which the transactions execute is clear.

However, in a concurrent system, transactions may execute concurrently, and it may lead to non-deterministic behavior and inconsistencies in the data. Therefore, we need to be sure that the concurrent execution of transactions is equivalent to a serial schedule to ensure data consistency. 

Serializability ensures that concurrent transactions are equivalent to some serial schedule by ensuring that the transactions do not interfere with each other. In other words, one transaction’s effects should not affect another transaction’s outcome.

Get 100% Hike!

Master Most in Demand Skills Now!

Importance of Serializability

Serializability is essential in database management systems because it ensures data consistency. Without serializability, concurrent execution of transactions could lead to inconsistencies in the data, such as lost updates, dirty reads, and non-repeatable reads. These inconsistencies can lead to incorrect results and can be challenging to debug.

For example, consider a bank account that has a balance of $100. Suppose two transactions are executed concurrently, one that deposits $50 into the account and another that withdraws $50 from the account. Without serializability, the outcome of these transactions could be non-deterministic. The final balance could be either $100 or $150, depending on the order in which the transactions are executed.

How Serializability Works?

Serializability works by ensuring that concurrent transactions do not interfere with each other. This is achieved through the use of locks and isolation levels.

Locks are used to prevent multiple transactions from accessing the same data simultaneously. When a transaction wants to access a piece of data, it acquires a lock on that data, preventing other transactions from accessing the same data until the lock is released.

Isolation levels define how transactions should behave when accessing data that is already locked by another transaction. 

There are four isolation levels in the ANSI SQL standard, which are as follows:

  • Read Uncommitted: In this isolation level, transactions can read uncommitted data, which means they can see data that has been modified but not yet committed by other transactions. This isolation level provides the least protection against concurrency issues.
  • Read Committed: At this isolation level, transactions can only access data that has already been committed by other transactions – providing additional protection from concurrency issues.
  • Repeatable Read: At this level of isolation, transactions are only able to read data that has already been committed by other transactions. Moreover, the data they read will be reliable during the transaction’s execution. This level of isolation blocks non-repeatable reads but still permits phantom reads.
  • Serializable: This isolation level provides the highest level of protection against concurrency issues. In this isolation level, the execution of transactions ensures they are equivalent to some serial schedule, even if they are executed concurrently. This isolation level prevents all concurrency issues, including non-repeatable reads and phantom reads. 

Types of Serializability

Types of Serializability

There are three types of serializability, which are as follows:

  • Conflict Serializability
    Conflict serializability is a type of serializability in which transactions are serializable if there is no conflict between them. In simpler terms, if two transactions do not conflict with each other, they can execute concurrently. Conflicts arise when two transactions access the same data item, and at least one of them is a write operation. 

    To determine if a concurrent schedule is conflict-serializable, we use a technique called precedence graph. 
Precedence Graph

A precedence graph is a directed graph that shows the dependencies between transactions. If a transaction T1 modifies a piece of data that another transaction T2 reads, there is a dependency between T1 and T2. The precedence graph shows these dependencies and can be used to determine if a schedule is conflict-serializable.

There are two types of conflicts in conflict serializability: read-write conflicts and write-write conflicts.

  1. In read-write conflicts, one transaction writes to a data item while another transaction reads from it. The read transaction must wait until the write transaction is complete before it can proceed to avoid inconsistency.
  2. In write-write conflicts, two transactions write to the same data item. To avoid inconsistency, one of the transactions must be aborted or rolled back.
  3. Conflict serializability is used in most commercial DBMS, as it provides a simple and efficient method of ensuring serializability.
  • View Serializability
    View serializability is a type of serializability in which transactions are serializable if they produce the same results as if they were executed sequentially, based on their logical reads and writes. It does not consider physical data access, but rather focuses on the logical structure of the transactions. In other words, view serializability ensures that the transactions produce the same logical result, irrespective of the order in which they are executed.

    To ensure view serializability, transactions must maintain a consistent view of the database, which means that the data accessed by a transaction must be the same as the data that exists at the time of its execution. If a transaction reads data that has been modified by another transaction, it must abort to avoid inconsistency.

    View serializability is less common than conflict serializability, but it is used in some specialized applications where the logical correctness of transactions is more important than their physical execution.
  • Timestamp Serializability
    Each transaction is assigned a unique timestamp, which indicates its execution order. Transactions are serialized in ascending order of their timestamps, and any transaction that violates this order is rolled back.

    Timestamp serializability is a highly efficient method of ensuring serializability, as it allows transactions to execute concurrently without the need for complex conflict detection mechanisms. However, it requires a precise and accurate timestamping mechanism to avoid inconsistencies.

Implementing Serializability in DBMS

Implementing Serializability in DBMS

DBMS employs several methods to implement serializability, which are as follows:

  • Lock-Based Concurrency Control
    Lock-based concurrency control is a method of implementing serializability by using locks to control concurrent access to the database. In this method, each transaction must obtain a lock on a data item before accessing it. A lock can be in two states, shared or exclusive.

    A shared lock allows multiple transactions to read the data item, but only one transaction can hold an exclusive lock to write to it. If a transaction attempts to access a data item that is already locked by another transaction, it must wait until the lock is released.

    Lock-based concurrency control is simple and efficient, but it can lead to issues such as deadlock, where two or more transactions wait indefinitely for each other to release their locks.
  • Two-Phase Locking Protocol
    The two-phase locking protocol (2PL) is an extension of lock-based concurrency control that ensures serializability. In this protocol, transactions acquire locks in two phases, the growing phase and the shrinking phase.

    During the growing phase, a transaction acquires locks on the data items it needs to access. Once a lock is acquired, it cannot be released until the shrinking phase, where the transaction releases all the locks it has acquired. Once a lock is released, it cannot be reacquired.
    2PL guarantees serializability, but it suffers from the same issues as lock-based concurrency control, such as deadlock.
  • Optimistic Concurrency Control
    Optimistic concurrency control (OCC) is a method of implementing serializability that assumes that conflicts between transactions are rare. In this method, transactions are allowed to execute concurrently without acquiring locks on data items.

    Each transaction is assigned a unique timestamp, which is used to determine the order of execution. Before committing, each transaction checks whether any other transaction has modified the data items it accessed. If there is no conflict, the transaction is committed, otherwise, it is aborted and rolled back.

    OCC is highly efficient and does not suffer from the issues of lock-based concurrency control and 2PL. However, it is less commonly used in commercial DBMS as conflicts between transactions are not as rare as assumed.

Challenges in Implementing Serializability

  • Deadlocks
    Deadlocks are common in a serializability-implemented system. A deadlock occurs when two or more transactions are waiting for each other to release resources, and neither can proceed. In a database system, this typically happens when two or more transactions are trying to access the same resource simultaneously.

    For example, transaction A may be trying to update a record in a table while transaction B is trying to read the same record. If transaction A locks the record before transaction B can read it, then transaction B will have to wait until transaction A releases the lock. Similarly, if a transaction Blocks the record before transaction A can update it, then transaction A will have to wait for transaction B to release the lock. This creates a circular wait that results in a deadlock.

    One solution to prevent deadlocks is to use a locking mechanism. Locking ensures that transactions can access resources in a mutually exclusive manner, preventing conflicts between transactions. However, if locks are not used properly, they can lead to deadlocks. For example, if a transaction requests a lock on a resource that is already locked by another transaction, it will have to wait until the other transaction releases the lock. If the other transaction is waiting for a lock held by the first transaction, a deadlock will occur.
  • Starvation
    Starvation is another challenge that can arise in a system that implements serializability. Starvation occurs when a transaction is unable to proceed due to a lack of resources.

    In a database system, when a transaction is waiting for a resource that is continually being used by other transactions. For example, if a transaction is waiting for a lock on a record that is continually being updated by other transactions, it will not be able to proceed, resulting in starvation.

    One solution to prevent starvation is to use a priority-based scheduling mechanism. A priority-based scheduling mechanism ensures that transactions with a higher priority are given access to resources before transactions with a lower priority. This can prevent transactions from being blocked by lower-priority transactions that are continually accessing resources.
  • Performance Issues
    Performance issues are another challenge that can arise in a system that implements serializability.It occurs when a system is unable to process transactions efficiently due to the overhead of ensuring serializability.

    In a database system, this typically happens when a large number of transactions are trying to access the same resources simultaneously. This can result in a bottleneck that slows down the processing of transactions.

    One solution to improve performance is to use optimistic concurrency control. Optimistic concurrency control allows transactions to proceed without acquiring locks on resources. Instead, it assumes that conflicts between transactions are rare and allows transactions to proceed concurrently. If conflicts occur, the system detects them and resolves them by rolling back one of the transactions. This can improve performance by reducing the overhead of acquiring and releasing locks on resources.

Conclusion

Serializability is a vital concept in DBMS that ensures data consistency and integrity, allowing multiple users to access the database concurrently. While its implementation may present challenges, the benefits it provides in terms of data correctness and consistency far outweigh any difficulties encountered.

Course Schedule

Name Date Details
SQL Training 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Data Engineer

As a skilled Data Engineer, Sahil excels in SQL, NoSQL databases, Business Intelligence, and database management. He has contributed immensely to projects at companies like Bajaj and Tata. With a strong expertise in data engineering, he has architected numerous solutions for data pipelines, analytics, and software integration, driving insights and innovation.