Concurrency - Lock (Mutex)

Data System Architecture

About

A lock is a synchronization mechanism designed to enforce a mutual exclusion of threads.

A lock is also known as a mutex.

Type:

Most locking designs block the execution of the thread requesting the lock until it is allowed to access the locked resource.

Latches are used to guarantee physical consistency of data (write on disk) while locks are used to assure logical consistency of data (write in memory)

It enforce data consistency in a multi-user database environment with then multiple concurrent transactions.

Locks are simply names used by convention within the system to represent:

  • either physical items (e.g., disk pages)
  • or logical items (e.g., tuples, files, volumes)

that the application (database for instance) manages.

In general, multiuser databases use some form of data locking to solve the problems associated with:

Locks are mechanisms that prevent destructive interaction between transactions accessing the same resource.

This helps maintain the integrity of the data by ensuring that only one process at a time can modify the same data.

Such locks can be applied on:

  • a row level,
  • a page (a basic data block),
  • a extent (multiple array of pages)
  • on an entire table
  • on an entire file

The file system locking mechanism (files or folders) , only one lock at a time can be set, restricting the usage to one process only. Databases, on the other hand, can set and hold multiple locks at the same time on the different levels of the physical data structure.

The number, nature of locks and time the lock holds a data block can have a huge impact on the database performances. Bad locking can lead to disastrous performance response (usually the result of poor SQL requests, or inadequate database physical structure)

Mutex vs Latch

A mutex is similar to a latch, but whereas a latch typically protects a group of objects, a mutex protects a single object.

Mutexes provide several benefits:

  • A mutex can reduce the possibility of contention.

Because a latch protects multiple objects, it can become a bottleneck when processes attempt to access any of these objects concurrently. By serializing access to an individual object rather than a group, a mutex increases availability.

  • A mutex consumes less memory than a latch.
  • When in shared mode, a mutex permits concurrent reference by multiple sessions.

Manager

The lock manager provides a place to register and check for the lock.

Every lock is associated with a transaction and each transaction has a unique transaction ID.

The lock manager makes use of a deadlock detector that periodically examines the lock table to detect waits-for cycles (a cycle of workers where each is waiting for the next and a cycle is formed). Upon detection of a deadlock, the deadlock detector aborts one of the deadlocked transactions. A more detailed description of a lock manager implementation is given in Granularity of locks in a shared data base (Gray and Reuter).

Mutex vs Latch

A mutex is similar to a latch, but whereas a latch typically protects a group of objects, a mutex protects a single object.

Mutexes provide several benefits:

  • A mutex can reduce the possibility of contention.

Because a latch protects multiple objects, it can become a bottleneck when processes attempt to access any of these objects concurrently. By serializing access to an individual object rather than a group, a mutex increases availability.

  • A mutex consumes less memory than a latch.
  • When in shared mode, a mutex permits concurrent reference by multiple sessions.

Mode

Locks come in different lock modes and these modes are associated with a lock-mode compatibility table. In most systems, this logic is based on the well-known lock modes that are introduced in Granularity of locks in a shared data base (Gray and Reuter)

Most DBMS systems use :

  • shared
  • and exclusive

locks.

Exclusive

Exclusive locks mean that no other lock can acquire the current data object as long as the exclusive lock lasts. DBMSs usually set exclusive locks when the database needs to change data, as during an UPDATE or DELETE operation.

Shared

Shared Lock

Others Reasons

Databases can also be locked for other reasons, like access restrictions for given levels of user. Some DBAs also lock databases for routine maintenance, which prevents changes being made during the maintenance.

Isolation level

The isolation level of the data server enforces default locking behaviour. Changing the isolation level will affect how shared or exclusive locks must be set on the data for the entire database system. Default isolation is generally 1, where data can not be read while it is modified, forbidding the return of “ghost data” to end users.

Deadlock

At some point intensive or inappropriate exclusive locking can lead to a deadlock situation between two locks, where none of the locks can be released because they try to acquire resources mutually from each other.

Documentation / Reference





Discover More
Data System Architecture
Concurrency - Deadlock

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other due to a lock. At some point intensive or inappropriate exclusive locking can lead to a “deadlock”...
Data System Architecture
Concurrency - Latches (System Lock)

Latches are like semaphores. Latches are used to guarantee physical consistency of data, while locks are used to assure logical consistency of data. Latches are simple, low-level system lock (serialization...
Data System Architecture
Concurrency - Mutex (Mutual exclusion object)

A mutex is a mutual exclusion object that restricts access to a shared resource (e.g. a file) to a single thread instance. The mutual exclusion synchronization between concurrent thread means that: ...
Data System Architecture
Concurrency - Mutual Exclusion

Mutual Exclusion means that two threads or process cannot at the same time access the same resource (ie method, class, file, ...) in order to prevent race conditions. A lock (also known as mutex) is...
Data System Architecture
Concurrency - Synchronization

Synchronization insures thread safety by preventing the same code being run by two different threads at the same time. When a code (object, method) has a synchronized property, if a thread enter a synchronized...
Data System Architecture
Data Concurrency - Monitor

A Monitor is a property of a thread-safe object, or method that lock and give temporarily give access to a threads.
Data System Architecture
Data Property - Data Consistency - (Strong|Atomic) consistency - Linearizability)

In its most basic form, consistency refers to data values in one data set being consistent with values in another data set at the same point in time. In an other form, consistency, also known as atomic...
Data System Architecture
Database management system (DBMS)

A Database_management_systemDBMS is a set of software programs that controls the organization, storage, management, and retrieval of data. A database is a collection of permanently stored data used by...
Undraw File Manager Re Ms29
File Lock

A File lock is a lock on a file They are acquired from the operating system kernel file system. All open files have acquired a lock. Their implementation is OS depend. They are generally just flags....
Card Puncher Data Processing
Hive - Lock

lock in hive turn off the hive.support.concurrency conf parameters....



Share this page:
Follow us:
Task Runner