Concurrency control techniques are essential for managing multiple transactions in programming. They ensure data integrity and consistency while allowing simultaneous access. Key methods include Two-Phase Locking, Timestamp Ordering, and Optimistic Concurrency Control, each with unique strengths and challenges.
-
Two-Phase Locking (2PL)
- Ensures serializability by requiring transactions to acquire locks before accessing data.
- Divided into two phases: the growing phase (acquiring locks) and the shrinking phase (releasing locks).
- Can lead to deadlocks if transactions wait indefinitely for each other to release locks.
-
Timestamp Ordering
- Assigns a unique timestamp to each transaction to determine the order of execution.
- Ensures that transactions are executed in the order of their timestamps, maintaining consistency.
- Can lead to transaction aborts if a transaction tries to access data that has been modified by a younger transaction.
-
Optimistic Concurrency Control
- Assumes that conflicts are rare and allows transactions to execute without locking resources.
- Consists of three phases: read, validate, and write; validation checks for conflicts before committing.
- Suitable for environments with low contention but can lead to increased abort rates under high contention.
-
Multiversion Concurrency Control (MVCC)
- Maintains multiple versions of data items to allow concurrent access without locking.
- Readers access the snapshot of the data at the time they started, while writers create new versions.
- Reduces contention and improves performance, especially in read-heavy workloads.
-
Serialization Graph Testing
- A method to check if a schedule of transactions is conflict-serializable by constructing a serialization graph.
- Nodes represent transactions, and edges represent conflicts between them.
- If the graph is acyclic, the schedule is conflict-serializable; otherwise, it is not.
-
Deadlock Prevention and Detection
- Prevention techniques include resource ordering and wait-die or wound-wait schemes to avoid circular wait conditions.
- Detection involves periodically checking for cycles in the wait-for graph to identify deadlocked transactions.
- Once detected, deadlocks can be resolved by aborting one or more transactions.
-
Isolation Levels
- Defines the degree to which the operations in one transaction are isolated from those in other transactions.
- Common levels include Read Uncommitted, Read Committed, Repeatable Read, and Serializable.
- Higher isolation levels provide more consistency but can lead to reduced concurrency and performance.
-
Lock Granularity
- Refers to the size of the data item being locked, which can range from a single row to an entire database.
- Fine-grained locking (e.g., row-level) allows for higher concurrency but increases overhead.
- Coarse-grained locking (e.g., table-level) reduces overhead but can lead to contention and lower concurrency.
-
Read-Write Locks
- Allows multiple transactions to read a data item simultaneously while ensuring exclusive access for writing.
- Promotes higher concurrency by differentiating between read and write operations.
- Writers may be blocked by readers, leading to potential starvation if not managed properly.
-
Conflict Serializability
- A schedule is conflict-serializable if it can be transformed into a serial schedule by swapping non-conflicting operations.
- Ensures that the final state of the database is the same as if the transactions were executed one after the other.
- Important for maintaining data integrity and consistency in concurrent transaction processing.